imagick why in three or more image resizes one is blurred

Image Manipulation Add comments

My recent work on image manipulation(image re size) got into trouble.

I used imagick to resize images.

i want to resize a source images into three sizes.

here is the sample code i used

	$image	=	new Imagick($_FILES['pic']['tmp_name']);
	$image->setImageCompression(Imagick::COMPRESSION_JPEG);
	$image->setImageCompressionQuality(100);	
	$image->thumbnailImage(220, 220, true);
	$image->writeImage(DOC_ROOT."/images/pic/$id.jpg");
	$image->thumbnailImage(80, 80, true);
	$image->writeImage(DOC_ROOT."/images/pic/second/$id.jpg");
	$image->thumbnailImage(100, 100, true);
	$image->writeImage(DOC_ROOT."/images/pic/third/$id.jpg");	
	$image->destroy();

with the above code the second image got blurred. after checking for a while my team leader mentioned the fact with how imagick works.

An object is created and it is set to create jpeg files.

The first resize is done. So what happens here is the file from the temp folder is taken and resized into 220 x 220 and is saved in pic folder.
For the second resize instead of using the fresh copy from the temp folder the already resized image of 220×220 is used from the objects memory (not from the pic folder).
So 220×220 is again made a temp copy in the buffer and is resized to 80×80 and is saved in second folder. Now the object has the 80×80 image in its buffer.
So for the third time 80×80 is called from the objects buffer and is resized into 100×100.
Here what the function is doing is it increases 80×80 into 100×100 which makes the image blur.

What i thought was that each time we call the resize function it is using the original copy form the temp folder.

But what the function is doing is it resizes the original copy to 220×200 and it keeps in the temp folder.
so at the first resize the image got resized to 220×220 in the temp folder. and for the second resize it has to use the temp copy which is already resized.

What i thought it would do is make a copy of the original image to the folder we have specified and then it resizes. So what the original file is intact.
This is how i do in my projects using codeigniter but this function resizes the image keeping it all the time in the temp folder.

So every time we call the resize function the file in the temp folder is the one which is manipulated.

First it is in the original size and then it is resized and overwritten to 220×220 in the temp folder.
for the second resize we have only the 220×220 and not the original size.
and this goes for the third resize…. oops…

So what is supposed to be done?

That is have a fresh initialization each time or use it in descending order. that is first resize the image to 220 and then to 100 and then to 80 so it will retain the quality.
If you do it in the mixed order then if you specify a size which is greater than the current image size then you get a blurr output.

Leave a Reply

Entries RSS