Recommendation for compressing JPG files with ImageMagick

后端 未结 10 1315
有刺的猬
有刺的猬 2020-12-04 04:10

I want to compress a JPG image file with ImageMagick but can\'t get much difference in size. By default the output size is bigger than the input. I don\'t know why, but afte

10条回答
  •  佛祖请我去吃肉
    2020-12-04 04:53

    Here's a complete solution for those using Imagick in PHP:

    $im = new \Imagick($filePath);
    $im->setImageCompression(\Imagick::COMPRESSION_JPEG);
    $im->setImageCompressionQuality(85);
    $im->stripImage();
    $im->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
    
    // Try between 0 or 5 radius. If you find radius of 5 
    // produces too blurry  pictures decrease to 0 until you 
    // find a good balance between size and quality. 
    $im->gaussianBlurImage(0.05, 5);
    
    
    
    // Include this part if you also want to specify a maximum size for the images
    
    $size = $im->getImageGeometry();
    $maxWidth = 1920;
    $maxHeight = 1080;
    
    
    // ----------
    // |        |
    // ----------
    if($size['width'] >= $size['height']){
      if($size['width'] > $maxWidth){
        $im->resizeImage($maxWidth, 0, \Imagick::FILTER_LANCZOS, 1);
      }
    }
    
    
    // ------
    // |    |
    // |    |
    // |    |
    // |    |
    // ------
    else{
      if($size['height'] > $maxHeight){
        $im->resizeImage(0, $maxHeight, \Imagick::FILTER_LANCZOS, 1);
      }
    }
    

提交回复
热议问题