Page 1 of 1

Thumbnail quality

Posted: 2007-06-30T05:54:48-07:00
by zhenjas
Hello,

I'm creating a thumbnail using the folowing code:

Code: Select all

$r = NewMagickWand();
MagickReadImage($r, '15_light.gif');
MagickSetImageIndex($r, 0);
// MagickEnhanceImage($r, MW_LanczosFilter);
MagickSetImageType($r, MW_OptimizeType);
//MagickEnhanceImage($r);

$thumb_width = 135;
$thumb_height = 135;
list($width, $height) = getimagesize('15_light.gif');

if($thumb_width && ($width > $thumb_width))
{
	$height = round($height * $thumb_width / $width);
	$width = $thumb_width;
}

if($thumb_height && ($height > $thumb_height))
{
	$width = round($width * $thumb_height / $height);
	$height = $thumb_height;
}

//$r = MagickTransformImage($r, '0x0', '120x120' );
MagickResizeImage($r, $width, $height, MW_CubicFilter, 0.5);
MagickSetImagePage($r, 0, 0, 0, 0);
header('Content-Type: image/jpeg');
MagickEchoImageBlob($r);
But thumbnails created using this code are of a bad quality, so there are lost pixels in lines, etc.

Examples:
Original image:
Image
Created thumbnail:
Image

What should I do to create thumbnails of a good quality?

Thanks

Re: Thumbnail quality

Posted: 2007-07-01T21:09:53-07:00
by anthony
Fisrt don't set any filter. or use the defaults at a suport factor of 1

Second you don't need to calculate the smaller height or width. IM will do this automatically so as to preserve the images aspect ratio. just resize it directory.


Finally I suggest you 'strip' the image of any profiles, or you will find your thumbnail images are not very small.


See IM Examples (for command line) for examples to test out, then convert that to PHP.
http://www.imagemagick.org/Usage/thumbnails/

Re: Thumbnail quality

Posted: 2007-07-02T10:42:11-07:00
by zhenjas
anthony wrote:Fisrt don't set any filter. or use the defaults at a suport factor of 1

Second you don't need to calculate the smaller height or width. IM will do this automatically so as to preserve the images aspect ratio. just resize it directory.


Finally I suggest you 'strip' the image of any profiles, or you will find your thumbnail images are not very small.


See IM Examples (for command line) for examples to test out, then convert that to PHP.
http://www.imagemagick.org/Usage/thumbnails/
How can I not to use a filter?
The definition of the resize function is the folowing:
bool MagickResizeImage( MagickWand mgck_wand, float columns, float rows, int filter_type, float blur )
filter_type is obligate here and if I set to MW_UndefinedFilter then I get an error:
PHP Fatal error: magickresizeimage(): the parameter sent did not correspond to the required FilterTypes type in thumb.php...

What you mean by saying "use the defaults at a suport factor of 1"?

Re: Thumbnail quality

Posted: 2007-07-02T21:50:49-07:00
by anthony
The default filter is... 'Lanczos' for enlargements, and 'Mitchell' for shrinking.
The suport factor should be one.

For more info see IM Examples Resize Filters
http://www.imagemagick.org/Usage/resize/#filter

Re: Thumbnail quality

Posted: 2007-07-02T21:52:59-07:00
by anthony
The default filter is... 'Lanczos' for enlargements, and 'Mitchell' for shrinking.
The suport factor should be one.

For more info see IM Examples Resize Filters
http://www.imagemagick.org/Usage/resize/#filter

I believe there is a higherlevel function in Magick Core. try looking in "wand/mogrify.c", search for "resize" (with quotes) and trace the command line option.

Re: Thumbnail quality

Posted: 2007-07-03T08:22:19-07:00
by zhenjas
I've changed the filter to MW_LanczosFilter and suport factor to 1, but I get almost the same quality of image.

Code: Select all

$r = NewMagickWand();
MagickReadImage($r, '15_light.gif');
MagickSetImageIndex($r, 0);
MagickSetImageType($r, MW_TrueColorType);

$thumb_width = 135;
$thumb_height = 135;
list($width, $height) = getimagesize('15_light.gif');

if($thumb_width && ($width > $thumb_width))
{
	$height = round($height * $thumb_width / $width);
	$width = $thumb_width;
}

if($thumb_height && ($height > $thumb_height))
{
	$width = round($width * $thumb_height / $height);
	$height = $thumb_height;
}

MagickResizeImage($r, $width, $height, MW_LanczosFilter, 1);
MagickStripImage($r);
header('Content-Type: image/jpeg');
MagickEchoImageBlob($r);
Calculation of smaller width and height seems obligate here - when I tried to call MagickResizeImage($r, $thumb_width, $thumb_height, MW_LanczosFilter, 1); I got an image with incorrect proportions.

I found nothing useful in "wand/mogrify.c", there are some calls of resize functions which tells nothing new.

Also in http://www.imagemagick.org/Usage/thumbnails/ I found nothing special what I don't do in my script.

To compare here is the thumbnail created using PhotoShop CS2:
Image

Re: Thumbnail quality

Posted: 2007-07-03T20:12:29-07:00
by anthony
try using MagickAdaptiveResizeImage() seems to be a higher level function.

Also look at MagickTransformImage() though I am not sure how to turn of the crop part. Probably use the same geometry string as the resize part. I have seen this function mentioned as a resize method.

Re: Thumbnail quality

Posted: 2007-07-05T12:28:32-07:00
by zhenjas
MagickAdaptiveResizeImage() there are no such a function in MagickWand for PHP.

With MagickTransformImage() I get almost the same effect as shown in my first post.

Looks like I'm stuck with these thumbnails :(

Re: Thumbnail quality

Posted: 2007-07-05T16:45:08-07:00
by anthony
Perhaps you should experiment a bit with the command line.

If something is right with the command line, it should be possible from the API, If it is not possible than it is either a bug, or unimplemented in the API. In either case it should be reported.