php图片质量压缩imagecopyresampled

http://www.jb51.net/article/57580.htm

//用于对图片进行缩放
function thumb($filename,$newFilename,$quality=90,$width='',$height=''){
    //获取原图像$filename的宽度$width_orig和高度$height_orig
    list($width_orig,$height_orig) = getimagesize($filename);
 
    if($width=='' && $height==''){
      $width = $width_orig;
      $height = $height_orig;
    }else{
      //根据参数$width和$height值,换算出等比例缩放的高度和宽度
      if ($width && ($width_orig<$height_orig)){
          $width = ($height/$height_orig)*$width_orig;
      }else{
          $height = ($width / $width_orig)*$height_orig;
      }
    }
 
    //将原图缩放到这个新创建的图片资源中
    $image_p = imagecreatetruecolor($width, $height);
    //获取原图的图像资源
    $image = imagecreatefromjpeg($filename);
 
    //使用imagecopyresampled()函数进行缩放设置
    imagecopyresampled($image_p,$image,0,0,0,0,$width,$height,$width_orig,$height_orig);
 
    //将缩放后的图片$image_p保存,100(质量最佳,文件最大)
    imagejpeg($image_p,$newFilename,$quality);
 
    imagedestroy($image_p);
    imagedestroy($image);
}
thumb("1.jpg",'./Runtime/2.jpg');

Related posts:

Leave a Reply

Your email address will not be published.