Thumbnail quality

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
zhenjas

Thumbnail quality

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Thumbnail quality

Post 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/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zhenjas

Re: Thumbnail quality

Post 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"?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Thumbnail quality

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Thumbnail quality

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zhenjas

Re: Thumbnail quality

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Thumbnail quality

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
zhenjas

Re: Thumbnail quality

Post 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 :(
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Thumbnail quality

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply