[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:
However, this outputs a PNG file with a .jpg extension - with transparency and everything.
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);
Viewing 15 lines of 19 lines. View entire code block.
However, this outputs a PNG file with a .jpg extension - with transparency and everything.
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:
4) Use it like this:
From there, you can use it as you'd normally use the image helper. It's not the prettiest solution but it works.
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; }
Viewing 15 lines of 125 lines. View entire code block.
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.
Or something like that.