[5.7] Image to JPG when generating a thumbnail?

Permalink
Many of the images on my website are uploaded in PNG format.
When I am generating a thumbnail from them, I'd like to convert them to JPG.

I do have something like this:
<?php 
use \Imagine\Image\ImageInterface;
use \Concrete\Core\File\Image\Thumbnail\Thumbnail as Thumbnail;
use \Concrete\Core\File\Image\Thumbnail\Type\Type;
$type = new Type();
$type = $type->getByHandle('average_image');
if(!$type){
   $type = new Type();
   $type->setHeight(50);
   $type->setWidth(50);
   $type->setName('AverageImage');
   $type->setHandle('average_image');
   $type->save();
}
$f = File::getByID(1);


However, this outputs a PNG file with a .jpg extension - with transparency and everything.

VPenkov
 
PineCreativeLabs replied on at Permalink Reply
PineCreativeLabs
Somewhere in your output, you could use str_replace to drop the PNG extenstion. For example:

$image = 'path_thumbnail';
$search = 'png';
$trimmed = str_replace($search, "", $image);
echo $trimmed;

Or something like that.
VPenkov replied on at Permalink Best Answer Reply
VPenkov
Yeah, not sure if that solves anything. I've opened a GitHub issue (https://github.com/concrete5/concrete5/issues/2556), let's see what the core team thinks about this.

Meanwhile, what we did was copy the old image helper with some light modifications. Here's what we did:
1) Create folder: /application/src/
2) Create file: /application/src/JpegImageHelper.php with the following content:
<?php
namespace Application\Src;
use Config;
use Loader;
use Image;
use \Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use \Imagine\Image\Point\Center;
use \Concrete\Core\File\File;
class JpegImageHelper
{   
    function replace_extension($filename, $new_extension) {
        $info = pathinfo($filename);
        return $info['filename'] . '.' . $new_extension;
    }

4) Use it like this:
use Application\Src\JpegImageHelper as ImageCompressor;
$ih = new ImageCompressor();
$ih->setJpegCompression(79);


From there, you can use it as you'd normally use the image helper. It's not the prettiest solution but it works.

This website stores cookies on your computer. These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media. To find out more about the cookies we use, see our Privacy Policy.