2

I'm trying to determine what my image processing workflow should look like. I know WordPress automatically resizes and compresses images to various different sizes on upload. I'm curious if it does any compression on the original image.

Since it's doing compression (as it should) on the smaller images, I wouldn't want to upload an already compressed JPEG to have it re-compressed on the smaller sizes. But, if it doesn't compress the original, I'll have much larger image file than necessary when someone goes to view the original.

I'm thinking the ideal workflow would be to upload a quality 100 JPEG to WordPress, have it resize and compress all of the smaller images using the full quality image, and then compress the original as well. Does anyone know if this is possible (or if this is what is being done already)?

1 Answer 1

5

This is all I can get from reading the source code for the wp_handle_upload function in wp-admin/includes/file.php.

  • WordPress keeps the original uploaded file (usually) - see below...
  • WordPress does apply JPEG compression to resized images when the source is a JPEG.
  • The default JPEG compression level is 90.
  • You can adjust the JPEG compression level by adding a jpeg_quality filter:

    add_filter('jpeg_quality', 'ex46632_jpeg_quality');
    function ex46632_jpeg_quality($quality) {
        $quality = 95; // An example of setting the quality to a higher value.
        return $quality;
    }
    
  • You can force an uploaded image to be resized to the Max Width/Max Height specified in Settings > Media > Large size by adding a wp_upload_resize filter:

    add_filter('wp_upload_resize', 'ex46632_upload_resize');
    function ex46632_upload_resize($resize) {
        $resize = true; // Force resizing to the max sizes.
        return $resize;
    }
    
    • If there are any errors in the creation of the resized image the original uploaded file will be kept instead.
    • If resize is successful, original uploaded media is discarded.

Note the code samples here have not been tested and are just for reference.

Update As commented a change was made to the handling of resizing in the wp_handle_upload function. This change fixes a bug, in order to fix the bug the resize code was actually removed so images are not resized on upload and are kept at their original size. For your reference explicit code change and bug ticket. It should be noted that this change is still "Awaiting Review to 3.4", it's not released yet.

Note also that the ticket has the comment "let Plupload resize images (before uploading)", so perhaps there is another place to check for the resizing of images now since Plupload is included.

5
  • Thanks, that is pretty much exactly what I was looking for. :)
    – Dominic P
    Commented Mar 26, 2012 at 17:04
  • @DominicP no problem! :) Commented Mar 26, 2012 at 17:21
  • Well, it seems I was a little hasty to get excited about this solution. It looks like WordPress has axed the wp_upload_resize filter as of 3.4 Beta 4. Any ideas on what my workflow should be now? I'm thinking I need to upload a 100% quality JPEG and then somehow hook into the image resize process to make sure we only use the resized and compressed Large size. But, I'm not quite sure what the best approach to that would be.
    – Dominic P
    Commented May 8, 2012 at 23:39
  • I've added an update for your reference. Commented May 13, 2012 at 12:29
  • After a bit of checking, the JPEG quality in Plupload for WordPress seems to be hard-coded at 100. In case anyone's interested, I opened a Trac ticket about this issue. We'll see what the WordPress devs think.
    – Dominic P
    Commented Jun 4, 2012 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.