iwyg / jitimage

Just In Time image manipulation (GD, Imagick, imagemagick) with integration for laravel 4
MIT License
95 stars 8 forks source link

Save thumb/image in another format than source #9

Closed heinzmuller closed 10 years ago

heinzmuller commented 10 years ago

Any chance it is possible to add the option to save the generated image in another format?

Sort of like this JitImage::source('path/to/myimage.tif')->scale(50)->convert('jpg');

And for recipes

'recipes' => [
    'small'     => '2/100/100/5, filter:gs, confer:jpg'
],
iwyg commented 10 years ago

Sure, you can write a custom filter.

For the Facade it would look like (given your filter is aliased to 'Convert' => 'cv' and format option is f)

JitImage::source('path/to/myimage.tif')->scale(50)->filter('cv', 'f=jpg');

In your filter's run() e.g. (Acme\Filters\Convert\ImagickCvFilter) method you would simple call

$this->driver->setOutputType('jpg');

You can register your own filter like:


Event::listen('jitimage.registerfitler', function ($driverName) {
   return [
     'Convert' => 'FilterClass'
   ];
});

Also see this part of the serviceprovider. Should give you a hint how filters are registered internally.

'jitimage.registerfitler' is fired once for each driver, so you can utilize $driverName to determine which filterclass to install for each driver.

This isn't really documented. I have to apologize. I'll add this to the docs asap.

Best, Thomas

heinzmuller commented 10 years ago

Thanks a bunch for the quick reply, i'll dive in right away :)

iwyg commented 10 years ago

Oh, I was wrong, there's a short example on how to register custom filters:

https://github.com/iwyg/jitimage#register-external-filter

heinzmuller commented 10 years ago

https://github.com/iwyg/jitimage/blob/master/src/Thapp/JitImage/JitImageServiceProvider.php#L303

Typo: jitimage.registerfitler

iwyg commented 10 years ago

oh yea, that one's awful. Thanks

iwyg commented 10 years ago

hm, gave this a quick shot but I'm unable to convert tiff to jpg (both imagemagick and imagick compiled against libtiff). Seems like when compiling imagemagick against libtiff jpeg support is dropped (at least in homebrew). So in theory I'd say it will work for im and imagick drivers.

Ok works for me with im and imagick drivers.

https://github.com/iwyg/jitimage/commit/1c39e6b1637e2faf706effd7d3fd042616b06fbd#diff-8c6999c6977539530459d4be9a3a5150R86

However, there's no GD support for tiff files