1

I cant see any settings and cant see much in the php api docs regarding this?

for instance if the original file upload is a png wordpress converts to jpg

2
  • Can you explain why you would want this?
    – Tom J Nowell
    Feb 10, 2015 at 18:23
  • I don't know why he wants this... I can speak for myself. I'm working on a Wordpress Theme that needs to work on Netscape Navigator 3.0 (Yeah, I know). It doesn't support PNG format. Oct 17, 2022 at 20:05

2 Answers 2

3

Here's something I wrote because my clients were using very high quality pngs, when they could be more lossy jpgs for web. Note this still preserves the original image, only changes the thumbnails. Add this to your functions.php

<?php

//Force PNG Thumbnails into JPGs
add_filter('wp_generate_attachment_metadata','force_png_to_jpg');

function force_png_to_jpg($image_data) {

  $sizes = array('thumbnail','medium','large');

  $upload_dir = wp_upload_dir();
  $file = $upload_dir['path'] . '/' . basename($image_data['file']);

  foreach($sizes as $size){

    if(isset($image_data['sizes'][$size]))
    {
      if( $image_data['sizes'][$size]['mime-type'] == "image/png" ){

        //change format and filename for jpg
        $dest_file = preg_replace('/\.png$/i', '.jpg', $image_data['sizes'][$size]['file']);
        $image_data['sizes'][$size]['file'] = $dest_file;
        $image_data['sizes'][$size]['mime-type'] = "image/jpg";

        //process image into jpg using standard gd lib
        $image = imagecreatefrompng($file);
        $bg = imagecreatetruecolor(imagesx($image), imagesy($image));
        imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
        imagealphablending($bg, TRUE);
        imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
        $bg = imagescale($bg, $image_data['sizes'][$size]['width'], $image_data['sizes'][$size]['height'] );
        imagedestroy($image);

        //set quality and save
        $quality = 80; // 0 = worst / smaller file, 100 = better / bigger file 
        imagejpeg($bg, $upload_dir['path'] . '/' . $dest_file, $quality);
        imagedestroy($bg);
      }
    }
  }

  return $image_data;
}
3
  • Nice one, altough I would suggest using get_intermediate_image_sizes() for $sizes source, to be more general :)
    – jave.web
    Jul 26, 2017 at 16:08
  • Also, in curr WP $file = ... has to be different, because $upload_dir['basedir'] contains path to current year/month upload directory - which is already contained in image_data['file']::::::::::::::::::::::::::::: $file = $upload_dir['basedir'] . '/' . $image_data['file'];
    – jave.web
    Jul 27, 2017 at 17:57
  • ...and lastly of course - imagejpeg($bg, dirname($file) . '/' . $dest_file, $quality);
    – jave.web
    Jul 27, 2017 at 18:29
1

The thumbnails will be saved using the mime type of the file.

You will need to create your own functions using a php extension or or library. WordPress uses GD but hopefully this will be ported over to ImageMagick by 3.5 Using the API available in the extension you can add hooks and filters to convert the images. A good starting place would be o look ate the media_handle_download and media_sideload_image functions.

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.