Drop_Table
Drop_Table

Reputation: 1

Imagemagick resizing control quality and extension

I am trying to learn Imagemagick, php.net docs are terrible T_T, and I cannot seem to find any answers to my questions. I am wanting to allow people to upload images then resize them and lose EXIF data.

Heres what I have currently.

$thumbnail = new Imagick("http://4.bp.blogspot.com/-hsypkqxCH6g/UGHEHIH43sI/AAAAAAAADGE/0JBu9izewQs/s1600/luna-llena1.jpg");
$thumbnail->thumbnailImage( 100, 100, true );
$thumbnail->writeImage( "avatar/thumbnail.jpg" ); 

Now how do I control the image file that it is being saved as? Lets say the user submits a gif/png/jpg how would I go about taking that image then saving it as the same input format or changing them all to .png?

Upvotes: 0

Views: 3347

Answers (2)

ManreeRist
ManreeRist

Reputation: 504

This IMO produces the best results for imagick thumbnails;

Load the picture

$img = new imagick( $_FILES['Picture']['tmp_name'] ); 

Trim an excess off the picture

$img->trimImage(0);

Create the thumbnail, in this case, I'm using 'cropThumbnailImage'

$img->cropThumbnailImage( 180, 180 );

Set the format so all pics can now be the same standard format

$img->setImageFormat( 'jpeg' );

Set the Image compression to that of a jpg

$img->setImageCompression(Imagick::COMPRESSION_JPEG); 

Set the quality to be 100

$img->setImageCompressionQuality(100); 

The resulting thumbnail is then a little bit blury IMO, so I add a slight sharpening effect to make it 'sharper'. . play around with these settings, but I like..

$img->unsharpMaskImage(0.5 , 1 , 1 , 0.05); 

Upvotes: 1

frostyterrier
frostyterrier

Reputation: 1912

I agree, the PHP.net docs are not very helpful. I've found that it's easiest to find how to do things using commands, then match the commands up with the PHP methods. I'm a little late replying so you might have figured it out by now, but if not, or for the benefit of anyone else:

If you want to change the image format before saving, add this before your writeImage line:

$thumbnail->setImageFormat('png');

Then change the extension in your writeImage line to match, e.g. thumbnail.png

To change the quality, write:

$thumbnail->setImageCompressionQuality(40); // Adjust the number 40

In some cases you might also want to set the compression type by writing:

$thumbnail->setImageCompression(Imagick::COMPRESSION_JPEG);

You can find the COMPRESSION constants here: http://www.php.net/manual/en/imagick.constants.php

Note: These are just examples. This compression would not actually work with a png file.

Upvotes: 0

Related Questions