图片压缩处理

  1. 注意

     # 要把php gd扩展库启用以下支持
     jpg,png,gif,wbmp,webp
     # 图片缩放并添加水印时,水印图片的资源也要释放,不要忘记
    
  2. 图片压缩、传输到浏览器并保存

     $width = 360;
     $height = 480;
     $quality = 100;
     $img_path = './test.jpg';
     $save_path = '/www/test.jpg';
    
     //获取原有图像长宽
     $img_size = getimagesize($img_path);
    
     //获取原有图像指针
     $finfo = new \finfo(FILEINFO_MIME_TYPE);
     $mime_type = $finfo->file($img_path);
     if(strstr($mime_type, 'jpg') || strstr($mime_type, 'jpeg')){
         $img_resource = imagecreatefromjpeg($img_path);
     }elseif (strstr($mime_type, 'png')){
         $img_resource = imagecreatefrompng($img_path);
     }elseif (strstr($mime_type, 'gif')){
         $img_resource = imagecreatefromgif($img_path);
     }elseif (strstr($mime_type, 'wbmp')){
         $img_resource = imagecreatefromwbmp($img_path);
     }else{
         $img_resource = imagecreatefromjpeg($img_path);
     }
    
     //创建画布返回指针
     $dst_resource = imagecreatetruecolor($width, $height);
    
     //原有图像绘入画布
     imagecopyresampled($dst_resource, $img_resource, 0, 0, 0, 0, $width, $height, $img_size[0], $img_size[1]);
    
     //画布图像另存为并输出到浏览器
     header("Content-type:image/jpeg");
     imagejpeg($dst_resource, $save_path, $quality);
    
     //关闭图片指针
     imagedestroy($img_resource);
     imagedestroy($dst_resource);
    
  3. 图片缩放为360x480并返回buffer

     $width = 360;
     $height = 480;
     $img_path = './test.jpg';
     $img_size = getimagesize($img_path);
     $finfo = new \finfo(FILEINFO_MIME_TYPE);
     $mime_type = $finfo->file($img_path);
     if(strstr($mime_type, 'jpg') || strstr($mime_type, 'jpeg')){
         $img_resource = imagecreatefromjpeg($img_path);
     }elseif (strstr($mime_type, 'png')){
         $img_resource = imagecreatefrompng($img_path);
     }elseif (strstr($mime_type, 'gif')){
         $img_resource = imagecreatefromgif($img_path);
     }elseif (strstr($mime_type, 'wbmp')){
         $img_resource = imagecreatefromwbmp($img_path);
     }else{
         $img_resource = imagecreatefromjpeg($img_path);
     }
     $dst_resource = imagecreatetruecolor($width, $height);
     imagecopyresampled($dst_resource, $img_resource, 0, 0, 0, 0, $width, $height, $img_size[0], $img_size[1]);
     ob_start();
     imagejpeg($dst_resource, null, 100);
     $img_buffer = ob_get_contents();
     ob_end_clean();
     imagedestroy($img_resource);
     imagedestroy($dst_resource);
     return $img_buffer;
    
  4. 图片缩放为360x480,添加文字并返回buffer

     $width = 360;
     $height = 480;
     $img_path = './test.jpg';
     $img_size = getimagesize($img_path);
     $finfo = new \finfo(FILEINFO_MIME_TYPE);
     $mime_type = $finfo->file($img_path);
     if(strstr($mime_type, 'jpg') || strstr($mime_type, 'jpeg')){
         $img_resource = imagecreatefromjpeg($img_path);
     }elseif (strstr($mime_type, 'png')){
         $img_resource = imagecreatefrompng($img_path);
     }elseif (strstr($mime_type, 'gif')){
         $img_resource = imagecreatefromgif($img_path);
     }elseif (strstr($mime_type, 'wbmp')){
         $img_resource = imagecreatefromwbmp($img_path);
     }else{
         $img_resource = imagecreatefromjpeg($img_path);
     }
     $dst_resource = imagecreatetruecolor($width, $height);
     imagecopyresampled($dst_resource, $img_resource, 0, 0, 0, 0, $width, $height, $img_size[0], $img_size[1]);
    
     //添加文字
     $r = 0;//0-255(红)
     $g = 0;//0-255(绿)
     $b = 0;//0-255(蓝)
     $a = 0;//0-127(127-全透明)
     $angle = 0;//角度
     $font_x = 0;//横坐标
     $font_y = 16;//纵坐标
     $font_size = 16;//字号
     $content = '文字';//内容
     $font_path = './ncover.ttf';//中文字体文件
     $new_point = imagecreatetruecolor($font_size,$font_size);
     $color = imagecolorallocatealpha($new_point, $r, $g, $b, $a);
     imagettftext($dst_resource, $font_size, $angle, $font_x, $font_y, $color, $font_path, $content);
    
     ob_start();
     imagejpeg($dst_resource, null, 100);
     $img_buffer = ob_get_contents();
     ob_end_clean();
     imagedestroy($img_resource);
     imagedestroy($dst_resource);
     return $img_buffer;
    
  5. imagecopyresampled 参数说明

     # 左上角为坐标原点(0,0)向右为x正坐标,向下为y正坐标
     imagecopyresampled($dst_resource, $img_resource, $dst_x, $dst_y, $img_x, $img_y, $new_width, $new_height, $cut_x, $cut_y);
    
     $dst_resource: 画布资源
     $img_resource: 图片资源
     $dst_x:        从画布哪个横坐标开始画
     $dst_y:        从画布哪个纵坐标开始画
     $img_x:        从图片哪个横坐标开始截
     $img_y:        从图片哪个纵坐标开始截
     $new_width:    画出来的宽度
     $new_height:   画出来的高度
     $cut_x:        从图像上切的宽度
     $cut_y:        从图像上切的高度
    
     # $dst_x $dst_y 决定从画布哪个点开始往右下方画
     # $img_x $img_y $cut_x $cut_y 决定原有图像怎样截
     # $new_width $new_height 决定截取出来的图像怎样缩放
    
  6. imagecopyresampled 同功能函数

     # 只做比例缩放(资源占用小)
     imagecopyresized
     # 按像素取色素点缩放(清晰)
     imagecopyresampled
     # 不进行缩放只切割(资源占用小,清晰)
     imagecopy
    
  7. imagecreatefromjpeg 同功能函数

     # 创建指定格式画布
     imagecreatefrompng
     imagecreatefromgif
     imagecreatefromwbmp
     imagecreatefromwebp
    
  8. imagejpeg 同功能函数

     # 输出指定格式图片
     # imagepng 没有图片质量参数
     imagepng
     imagegif
     imagewbmp
     imagewebp
    

results matching ""

    No results matching ""