[5.7] Image to JPG when generating a thumbnail?
PermalinkWhen 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.
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.
Or something like that.