1

After testing I realized that Word Press after image upload will make medium and thumbnail sized images of original image without EXIF/IPTC info

I realized it is because GD image library is used on my host, and that is how GD library works by default, strip out all image info.

After research I found out that if ImageMagick and the Imagick PHP extension is used instead of GD library EXIF info will be retained (with ImageMagick default settings) in generated resized image files.


UPDATE 1

ImageMagick and the Imagick PHP extension is installed on my host now. Now all generated images keep EXIF/IPTC info (original, medium and thumbnail image, all of them are with EXIF/IPTC data).

I allow visitors to upload hi-res images on site, and in most cases visitors do not optimize/compress images before upload and sometimes image file size is quite big.

For example 12MP image can be 10 MB, but after 85% jpeg compression its file size can be 2-3 times smaller, almost without noticeable loss of quality.

I put this function to functions.php (my theme folder) to compress original image during upload, to save up disk space and speed up loading of hi-res image on page with it.

// Image compression on upload, compress original image
function wt_handle_upload_callback( $data ) {
    $image_quality = 85; // 85% compression
    $file_path = $data['file'];
    $image = false;

    switch ( $data['type'] ) {
        case 'image/jpeg': {
            $image = imagecreatefromjpeg( $file_path );
            imagejpeg( $image, $file_path, $image_quality );
            break;          
        }

        case 'image/png': {
            $image = imagecreatefrompng( $file_path );
            imagepng( $image, $file_path, $image_quality );
            break;          
        }

        case 'image/gif': {         
            // Nothing to do
            break;
        }
    }

    return $data;
}
add_filter( 'wp_handle_upload', 'wt_handle_upload_callback' );

It works fine, original image is compressed, BUT this function removes EXIF/IPTC data on all images (I need to keep EXIT, at least on original image). Without function above, original and all resized images are with EXIF/IPTC data (because ImageMagick is installed), but original image is not compresed than.

How to fix, adjust function above to keep EXIF?

UPDATE 2

WP 4.5 is reseased with some image hadling improvements, and also with option to preserve image EXIF data (in case ImageMagic is used on host, which is in my case). WP 4.5 introduced new filter "image_strip_meta" which can be used to keep or remove EXIF on generated resized images (medium and thumbnails).

Can this filter be used to keep EXIF in original image when function above for compressiong original image (which remove all EXIF data from original and all resized image version) is used?

Or, anything else which will result in compressing original image right after upload and keep EXIF in original image?

9
  • Any good reason to do that? I think you are worried about SEO but there is no reason to keep EXIF data in resized images. Resized versions are intended to improve load times and because of that EXIF data should be removed, they consume bandwidth while not being useful. When you embed a resized version in HTML, be sure that it is linked to the original image with the EXIF data, Google will index the original image with the EXIF data. When the image is on search results, the "Visit page" points to page where the resized version is embedded, so it is all OK.
    – cybmeta
    Commented Apr 11, 2016 at 15:34
  • It is not SEO aspect. I have image gallery site, with options for users to upload / share images in high resolution. Also, I display images in high resolution with image EXIF/IPTC data. Usually visitors do not optimize, compress images before upload, and sometimes image file size is 5-10 MB, or more. For example image with 10MB in size after JPEG compression (85% - 90%) can be a lot smaller (file size), almost without visible quality loss. I wish to save up some disk space, and speed up loading of images. I added function which compress original image, it works fine, but EXIF is removed. Commented Apr 12, 2016 at 9:36
  • ImageMagick and the Imagick PHP extension is installed now on my host, I have GD library too. But EXIF is removed, again. I am not sure which image editor is now used, IS there any test to figure out what Word Press use, ImageMagick or GD? And how to tell to use ImageMagick and to keep EXIF in geneated images? Commented Apr 12, 2016 at 9:37
  • 1
    Ok, now I get your point. You are compressing the original uploaded file and you are losing EXIF data in that process. Anyway, you lose EXIF data because you use imagejpeg(), so your problem has nothing to do with WordPress. See this (imagejpeg()/GD library) and this (ImageMagick).
    – cybmeta
    Commented Apr 12, 2016 at 11:16
  • 1
    Read again my previous comment: you are using funcions from GD library to manipulate images, not functions from WordPress. In the links I shared you can see how to enable EXIF data for GD library and also for ImageMagick. Unless you use WordPress API, the filter image_strip_meta doesn't apply to your code. Also, the image_strip_meta is only available if ImageMagick is installed.
    – cybmeta
    Commented Apr 14, 2016 at 13:19

2 Answers 2

2

The problem is that you are using functions from GD library to manipulate images, not functions from WordPress Image API (WP_Image_Editor class). So, WordPress things doesn't apply to the generated image by your code.

WordPress Image API uses ImageMagick if available, otherwise it uses GD library.

In order to keep EXIF data:

  1. If GD library is used, you need to install PHP wih the EXIF exension and configure PHP with --enable-exif flag.
  2. If ImageMagick is available, then WordPress will use it and EXIF and other meta data is preserved. Also, you have access to image_strip_meta filter (not availabe if GD library is used).

As you have ImageMagick installed, you could use it instead of GD library. Or, maybe better, use WordPress API:

add_filter( 'wp_handle_upload', 'wt_handle_upload_callback' );
function wt_handle_upload_callback( $data ) {

    // get instance of WP_Image_Editor class
    $image = wp_get_image_editor( $data['file'] );

    if( ! is_wp_error( $image ) ) {
        $image->set_quality( 85 );
        $image->save( $data['file'], $data['type'] );
    }

    return $data;
}

I've not tested the above code and I think you don't need it. If the only prupose of doing all of this is to compress the image, you must know that the image is already compressed by WordPress to 82% quality (90 before WP 4.5). If you need to change the compression quality just use jpeg_quality filter:

add_filter( 'jpeg_quality', 'cyb_set_jpeg_quality' );
function cyb_set_jpeg_quality() {
    return 85;
}
3
  • Thank you very much. Your function works great. I needed to compress original image (because some hi-res image files are with big file size) and to keep EXIF data. I just commented out 85% quality compression, because WordPress use 82% as default (since WP 4.5), and results are great, especially with ImageMagic, great image quality with smaller file size. Commented Apr 15, 2016 at 15:48
  • 1
    I used 85 as quality just because it was in your question ;). Glad to help.
    – cybmeta
    Commented Apr 15, 2016 at 21:54
  • Yes, it was, because when I started question default WP compression was 90%, so 85% was my optimization. Now with WP 4.5 with standard 82% compression and with ImageMagic quality is even better and file size even smaller, and I have EXIF. Again, thank you so much. Commented Apr 16, 2016 at 5:52
-1

You should not assume that image related metadata survives the wordpress image manipulation. As you found out it depends on the image manipulation library installed on the server, and actually in version 4.5 the aim would be to strip some of it as noted here https://make.wordpress.org/core/2016/03/12/performance-improvements-for-images-in-wordpress-4-5/.

If you need the metadata as part of the thumbnail (and I fail to see why would you need it), you should just directly call the php image manipulation API, and then update the wordpress DB to set the proper fields as if the wordpress API handled it (probably can be done with some filters without changing the overall process too much)

2
  • I do not need metadata as part of the medium or thumbnail image. I just noted that after ImageMagick and the Imagick PHP extension is installed on my host WP generate resized images with metadata. But, after I apply function above to compress original image, I lost EXIF in original image. What I need is to compress original image during upload and to keep Exif inside it. Commented Apr 12, 2016 at 12:28
  • @AdvancedSEO As said in the answer, don't trust WP API for it in that case, as they might not be backward compatible Commented Apr 12, 2016 at 12:35

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.