No announcement yet.

Keep EXIF Data of Images and Compress Uploaded to Wordpress withour Plugins

  • Filter
  • Time
  • Show
Clear All
new posts

  • Keep EXIF Data of Images and Compress Uploaded to Wordpress withour Plugins

    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 = 50; // 50% compression
    $file_path = $data['file'];
    $image = false;
    switch ( $data['type'] ) {
    case 'image/jpeg': {
    $image = imagecreatefromjpeg( $file_path );
    imagejpeg( $image, $file_path, $image_quality );
    case 'image/png': {
    $image = imagecreatefrompng( $file_path );
    imagepng( $image, $file_path, $image_quality );
    case 'image/gif': {
    // Nothing to do
    return $data;
    add_filter( 'wp_handle_upload', 'wt_handle_upload_callback' );
    add_filter ('image_strip_meta', false);
    Similar Threads

  • #2
    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.