Does Imagemagick create Uniform thumbnail correctly yet?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
JohnnyDoomo
Posts: 4
Joined: 2016-06-29T12:06:13-07:00
Authentication code: 1151

Does Imagemagick create Uniform thumbnail correctly yet?

Post by JohnnyDoomo »

I'm using "ImageMagick 6.9.0-0 Q16 x86_64 2016-05-05" on my server through a script called Photopost. It has the option to create "uniform" thumbnails.

The script notes that this feature is only available when using ImageMagick, vs GD1 or GD2. I'm assuming that it's a feature of ImageMagick, and not of the Photopost script. I do not believe the Photopost script to be at fault here.

The problem I see when turning on the "Uniform thumbnail", which makes the thumbnails the equal height and width (without squishing the image contents), is that if the native image is close to equal proportions for width and height, or taller than it is wider, these thumbnails look fine. However, if the native image has a much longer width than height, the thumbnails come out horrible, even with the quality set at 100.

I display the thumbnails at 150x150, and I've even tried generating higher resolution thumbnails of 300x300 and using CSS to shrink them back down to a 150x150 size, despite the increase in KB size that this may have, but the thumbnails that are sourced from a native image with more width, than height, it still looks horrible.

I have gallery after gallery, and this is the case. If the width of the original image is larger than the height, and I use this "uniform" setting, the resulting thumbnail is of horrible quality.

Here is an image that shows the results from ImageMagick and in comparison to manually performing the task via Photoshop: http://www.dumpt.com/img/viewer.php?fil ... kw19fk.png

I'm wondering if this has been fixed in the latest version of ImageMagick, so that I can know if upgrading my ImageMagick will fix this problem.

Thanks for any help you can provide me.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by Bonzo »

I would suggest you check the Photopost script to see what it is doing with Imagemagick and try Imagemagick directly rather than use the Photopost script and see what the differences are.

I have had a look at the script website but it looks like there is no way to download it without buying it. Have you reported the problem to them?

Looking at your examples you are actualy cropping and resizing them not just resizing. I get better quality images cropping and then resizing and so suspect the Photopost script.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by Bonzo »

Try this php code and see what it looks like, I can not try as you have not linked to the original image:

Code: Select all

$size = getimagesize("test.png");
exec("convert test.png -crop {$size[2]}x{$size[2]}+0+0 -resize 150x150 output.png " );
echo "<img src="output.png">
?>
It should work but I do very little php coding these days and have forgotten a lot!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by snibgo »

IM has many different methods for resizing. Pretty much an infinite number. If you want to match a Photoshop result, you'll have to test them. And you could tell us what you have found, if you want.
snibgo's IM pages: im.snibgo.com
JohnnyDoomo
Posts: 4
Joined: 2016-06-29T12:06:13-07:00
Authentication code: 1151

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by JohnnyDoomo »

Bonzo wrote:Try this php code and see what it looks like, I can not try as you have not linked to the original image:

Code: Select all

$size = getimagesize("test.png");
exec("convert test.png -crop {$size[2]}x{$size[2]}+0+0 -resize 150x150 output.png " );
echo "<img src="output.png">
?>
It should work but I do very little php coding these days and have forgotten a lot!
That doesn't seem to work properly. I don't know a whole lot of php myself. I fixed what I could see, here's what I added to the test.php file:

Code: Select all

<?
$size = getimagesize("test.png");
exec("convert test.png -crop {$size[2]}x{$size[2]}+0+0 -resize 150x150 output.png " );
echo "<img src=\"output.png\">";
?>
What that gives me is a 150x150 solid brown image.

The test.png file was brownish in color, but was 1920x1080. If somebody can give me exactly what I need to put into the test.php file, I can test again.

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by fmw42 »

<?
$size = getimagesize("test.png");
exec("convert test.png -crop {$size[2]}x{$size[2]}+0+0 -resize 150x150 output.png " );
echo "<img src=\"output.png\">";
?>
Perhaps I misunderstand, but you seem to be cropping to HxH of the test.png which is H=1080 and then resizing to 150x150. I do not see anything wrong with that since 1080 is the smaller of the two dimensions of the input. But you might want to crop in the center. You might also want to test if the height is always smaller than the width before cropping and use the smaller of the two. To get smaller thumbnail sizes, you may want to replace -resize with -thumbnail, but that will remove any profiles and most of the other meta data.

I see no reason that you should get a brownish color result.

What do you get returned from

Code: Select all

convert -version

I do not know php getimagesize, but from http://php.net/manual/en/function.getimagesize.php, I think you should be using index 0 or 1 for width and height. So change $size[2] to $size[1]. You should also get rid of the virtual canvas after cropping using +repage.

So try

Code: Select all

<?
$size = getimagesize("test.png");
exec("convert test.png -gravity center -crop {$size[1]}x{$size[1]}+0+0 +repage -resize 150x150 output.png " );
echo "<img src=\"output.png\">";
?>
or

Code: Select all

<?
$size = getimagesize("test.png");
exec("convert test.png -gravity center -crop {$size[1]}x{$size[1]}+0+0 +repage -thumbnail 150x150 output.png " );
echo "<img src=\"output.png\">";
?>
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by Bonzo »

You are right fmw42 it should have been $size[1].

I was only providing an example based on the OP's images to prove Imagemagick is not the problem. As I see it the OP will still be using their script as it does more than resize the image and so there was no point in explaining any more.
JohnnyDoomo
Posts: 4
Joined: 2016-06-29T12:06:13-07:00
Authentication code: 1151

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by JohnnyDoomo »

OK, the code

Code: Select all

<?
$size = getimagesize("test.png");
exec("convert test.png -gravity center -crop {$size[1]}x{$size[1]}+0+0 +repage -resize 150x150 output.png " );
echo "<img src=\"output.png\">";
?>
Produced a good looking thumbnail on my test.

I've searched through my script and here's the code that is calling mogrify.

Code: Select all

$syscmd = "\"{$Globals['mogrify_command']}\" -strip -resize \"{$newwidth}x\" -resize \"x{$newheight}<\" -gravity center -crop {$newwidth}x{$newheight}+0+0 -quality {$Globals['imgquality']} $rotaten $unsharpn $shellthumb";
I can see that Photopost is inserting some variables in there from the script, but I can't see that anything else is wrong. Is there anything in that line that helps? Not sure if I can take something out of that line to fix my problem or not.

Any further help would be greatly appreciated.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by Bonzo »

So your code is:
Removing all EXIF data etc. with -strip
Resizing to a set width and keeping the aspect ratio
Resizing to a set height if needs to and keeping the aspect ratio
Cropping out the square image from the centre
Setting the save quality with -quality ( I wonder what this is set to; if you are saving as a png it could be causing a problem? I would take it out and see what effect it is having).
I assume it is rotating the image with $rotaten but I am not sure
Sharpening the image with $unsharpn
Saving as $shellthumb

I do not see an input image and as I say I have no idea what $rotaten is doing.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by fmw42 »

why mogrify and not convert, if you are processing only one image at a time? It would help if your resolved the variables so we can see and true example of your code.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by Bonzo »

why mogrify and not convert, if you are processing only one image at a time?
I wondered that but it might be the way the code works when uploading multiple images?

I would be tempted to change the code to a version of the code I posted using the variables from the original. The original code could just be commented out and if the OP wanted to go back to the original it would be straight forward.

But as I say I can not see an input image in the code or anything to link to an upload directory if the code does it that way. The image could be in this array $Globals['mogrify_command'] as there is no mogrify command in the code either.

The OP could try

Code: Select all

echo $Globals['mogrify_command']; 
and see what is displayed on an upload

EDIT
Thinking about it if the OP adds this to the script it should display the contents of all the variables and we may be able to come up with some modified code to use:

Code: Select all

$arr = get_defined_vars();
print_r($arr);
JohnnyDoomo
Posts: 4
Joined: 2016-06-29T12:06:13-07:00
Authentication code: 1151

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by JohnnyDoomo »

I've searched the php files of the script and got this

Code: Select all

$mogrify_command = "/usr/local/bin/mogrify";
There are many variables, and many php files to photopost. I would not know where to put the $arr lines that Bonzo mentioned.

The script is built for large amounts of image uploads. My site is currently unlaunched, but I do have 1 gallery that processed over 3k of images. I do not know if that means that I'm stuck using mogrify, or if I can change that command to convert tho. The convert processed the test image perfectly, while the script (which apparently is using mogrify) is having troubles.

I'm not sure if this narrows things down, but it seems like the shorter the image, compared to the longer the width, the worse it generates the thumbnail.

Here's the worst case example the script has created:

(not the script I'm having difficulty with, just my image hosting service)
thumb: http://www.dumpt.com/img/viewer.php?fil ... upa7x5.jpg
full: http://www.dumpt.com/img/viewer.php?fil ... dwdcuo.jpg

The $Globals['imgquality'] setting is set through the scripts admin control. I currently have it set to 80. I've tested it at 90 and 100, and it doesn't fix my issue.

I've searched for the $rotaten variable and have seen lines related to "-rotate", so yes the variable rotates, but I do not currently have this feature enabled, nor do I plan to use it.

When I searched for "$unsharpn" I found lines indicating both "-unsharp 10" and "null".

The only code I saw relating to $shellthumb was this:

Code: Select all

if( !stristr(PHP_OS, "win") )
        {
            $shellthumb = escapeshellarg($outthumb);
        }
        else
        {
            $shellthumb = $outthumb;
        }
I think after all the above, I understand what Imagemagick is doing.

In the command:

Code: Select all

$syscmd = "\"{$Globals['mogrify_command']}\" -strip -resize \"{$newwidth}x\" -resize \"x{$newheight}<\" -gravity center -crop {$newwidth}x{$newheight}+0+0 -quality {$Globals['imgquality']} $rotaten $unsharpn $shellthumb";
It has the width set first. So if I take the test image (http://www.dumpt.com/img/viewer.php?fil ... dwdcuo.jpg) and give it my setting of 200px in photoshop, and click OK, I get this: http://www.dumpt.com/img/viewer.php?fil ... 0yxp5a.png

Now, after applying that size to the image, then I go back and tell the height of that image to be 200px, with keeping proportions I get this: http://www.dumpt.com/img/viewer.php?fil ... fvtekn.png

That looks like the same quality of what Imagemagick is giving me for my thumbnail, other than it being not being cropped at the center and 200px x 200px. It's performing the width command, then performing the height on the already shrunken width.

It appears that ImageMagick is not FIRST determining which side of the image is shorter, so as the shrink THAT side first to my 200px. So while it works fine for portrait images, in then performs the same function incorrectly for landscape images.

Is this still in the realm of ImageMagick or is this Photopost not handling ImageMagick switches in the proper order?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by snibgo »

Until we know what command is being executed, we can only guess. Why not dump the command to a text file or something?
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by Bonzo »

So we are looking at command something like:

Code: Select all

/usr/local/bin/mogrify -strip -resize 200x -resize x200< -gravity center -crop 200x200+0+0 -quality 80 -rotate 90 -unsharp 10 outthumb.jpg
Yes I did my modification in a slightly different order which may have an effect.
It appears that ImageMagick is not FIRST determining which side of the image is shorter, so as the shrink THAT side first to my 200px. So while it works fine for portrait images, in then performs the same function incorrectly for landscape images.
This is probably the problem as the panoramic image you linked to when the width is shrunk to say 200px wide the height will be a lot smaller than than 200px and it is then resized again! It is possible to detect which size is larger with php and Imagemagick. I suppose you need to decide if you want to adjust the script. You could comment out the current command and add some alternate code so you could change back if you wanted to. If you do decide to modify the script you will have to remember what you did if you installed a new version of it later
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Does Imagemagick create Uniform thumbnail correctly yet?

Post by Bonzo »

You could try replacing the current code with this:

Code: Select all

if ( $newwidth > $newheight ){
$syscmd = "\"{$Globals['mogrify_command']}\" -strip -resize \"x{$newheight}\" -resize \"{$newwidth}<x\" -gravity center -crop {$newwidth}x{$newheight}+0+0 -quality {$Globals['imgquality']} $rotaten $unsharpn $shellthumb";
}

else{
$syscmd = "\"{$Globals['mogrify_command']}\" -strip -resize \"{$newwidth}x\" -resize \"x{$newheight}<\" -gravity center -crop {$newwidth}x{$newheight}+0+0 -quality {$Globals['imgquality']} $rotaten $unsharpn $shellthumb";
}

//$syscmd = "\"{$Globals['mogrify_command']}\" -strip -resize \"{$newwidth}x\" -resize \"x{$newheight}<\" -gravity center -crop {$newwidth}x{$newheight}+0+0 -quality {$Globals['imgquality']} $rotaten $unsharpn $shellthumb";
Hopefully there are no errors this time but I do not do a lot of programming these days. This is keeping the code as close to the original; I doubt it would be a problem with some different code but you never know.
Post Reply