homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

Creating thumbnails from any image
I need a more stable-standard way to do it to replace my solution

 6:16 pm on Feb 22, 2011 (gmt 0)

Hi Webmasters, I need help with this, can't figure it out yet by myself.

I need to create thumbnails from *any* image. The original size could be anything but the final size will be user defined (almost anything). It could be square, a rectangle, etc. I need it to be centered.

After hours of trying I came up with this:

convert -define jpeg:size=$dm $filename -thumbnail $dm^ \ -gravity center -extent $dm $filenamemini

where $dm stores "200x200" or "100x60" whatever the size of the final image is. It works, but not on every server, I guess it has to do with the versions of immagemagick, in fact I bet that is the thing. I could dig into that but my script would supposed to be "install and use" so I ask for your help if there is any way to do the same but in some more "standard" way.

The final image shoud be the exact size one tells it to create. Any help would be appreciated, I tried and searched and there are many others trying to do the same.

Thanks in advance.



 8:42 pm on Feb 22, 2011 (gmt 0)

ImageMagick: Command-line Tools: Convert:
http://www.imagemagick.org/script/convert.php [imagemagick.org]

so yes, "convert" requires ImageMagick.

afaik there is no "standard" command line image manipulation tool available for (i'm assuming you're on) linux, so if it's not available in your distribution you'll have to install it.


 11:36 pm on Feb 22, 2011 (gmt 0)

Thanks phranque, I'm on two servers running cent os already running ImageMagick, the thing is, my approach is not bullet proof for any version of imagemagick and I need to find a way that just for having imagemagick on it, it will run.

My way to explain this would be like split vs explode on PHP. Explode works on the latest and previous version, but split does not. That's what I mean on "standard - safe way"


 8:08 am on Feb 23, 2011 (gmt 0)

ok i missed the "versions" mention.

perhaps this command will give more consistent behavior across versions.

ImageMagick: Command-line Tools: Mogrify:
http://www.imagemagick.org/www/mogrify.html [imagemagick.org]


 1:54 am on Feb 24, 2011 (gmt 0)

I just found ANOTHER way to deal with it. Please note that the code posted on the first comment IS VALID and it works on one of my servers, but not on the other one.

I just came up with this approach using common commands from ImageMagick, so, the two codes are valid but this one is more bullet proof:

$w = `identify -format '%w' $filename`;
$h = `identify -format '%h' $filename`;



if($w > $h){$mo="x$nw";}else{$mo="$nw";}
if($w > $h && $nw > $nh){$mo="x$nw";}
if($w > $h && $nw < $nh){$mo="x$nh";}
if($w < $h && $nw > $nh){$mo="$nw";}
if($w < $h && $nw < $nh){$mo="x$nh";}
if($w eq $h && $nw < $nh){$mo="$nh";}
if($w eq $h && $nw > $nh){$mo="$nw";}

system("convert $xfilename -resize $mo -quality 75 $xfilenamemini");
system("convert $xfilenamemini -gravity Center -crop $dest+0+0 -quality 50 $xfilenamemini");

I'm sure it could be improved


 5:03 pm on Feb 24, 2011 (gmt 0)

You know, you can use PerlMagick to do many of the same things but that difference is trivial.

First thing I'd check is if the environment is allowing you to execute system(), I've seen some react poorly to one or the other and I've had to use exec or backticks instead. Backticks might help,

$system_message = `convert....`;
print "response: $system_message<br>";

because it allows you to store the output from the command.

Generally if ImageMagick doesn't work but is present, it means the libraries for that function are not installed or are a different version, or it may even be a simple path issue. I know that sounds dumb but it's easy to miss; print out

print "orig: $xfilename resized: $xfilenamemini<br>";

and verify the full system path is being used and it's valid, and has permissions to write to the directory where $xfilenamemini is being stored.

If the libraries are not present it needs to be recompiled with the most recent libraries. I'm not a system admin so can't tell you how to determine that.


 5:57 pm on Feb 24, 2011 (gmt 0)

Thanks rockbnil, system and exec were only blocked on one server and only when using php, but never with perl, the problem was indeed on imagemagick side. Both --extent-- and --define-- didn't work on the first code on one of the servers, even it is valid code running fine on other server.

So far, both approaches work, but the last one will work with any imagemagic where convert and resize are available (very old commands).

BTW, $Zf=getcuentavars(); is only a subroutine I need to get some other info on my scripts, everything else can be freely used for any project.

When the script is only for my personal use I dig and play around with interesting options that I can install and configure, but when its for a client, I rather use conventional approaches so when the client needs to move to other servers, the app will go on without any extra work.



 8:20 pm on Feb 24, 2011 (gmt 0)

Another old programming trick is to check the version and alter the logic handling in your application accordingly, especially if you are getting better/desired results from a newer version of the API. You could check the output of
convert -version

to do so.


 1:51 pm on Feb 25, 2011 (gmt 0)

We use "convert" over on the pubcon bios to thumbnail speaker photos. We also use it here in a beta profile image routine.

When we switched from Rackspace over here to liquid web - I reinstalled Imagemagick to the latest version. I ran into a similar problem as you.

Taking "gravity" out of the cmd line fixed it. I never determined why.


 2:29 pm on Feb 25, 2011 (gmt 0)

weird, in my case it was extent and define that caused the 1 working script to stop doing its job. I'm thinking on implementing the two codes into one checking for a -e to see if the first created the file, if not, then going for the second code to try. Hope others find this useful, I found the web filled with questions on how to fix such weird thing.


 7:38 pm on Feb 25, 2011 (gmt 0)

I never determined why.

Bug? [imagemagick.org]


 8:10 pm on Feb 27, 2011 (gmt 0)

Could be coop. Gravity is such an esoteric command though, that it was easy to live without it.


 1:04 pm on Feb 28, 2011 (gmt 0)

Have you considered using GD graphics library. I'm a little rusty with Perl, so I don't know if GM.pm is a common thing, but GD is installed by default on most PHP servers, so there's a high chance the hosting provider already supports it.

Converting images using GD is like one line of code, so it isn't any more complicated than running ImageMagick.



 2:33 pm on Mar 1, 2011 (gmt 0)

Gravity has been very helpful to avoid manual cropping on heavy photo sites, anyway it can be set via perl measuring the image and scaling-cropping.

Thanks for the GDI suggestion, will look into that. I have my scripts working now but it is good to implement a plan B.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved