Welcome to WebmasterWorld Guest from 54.162.250.227

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Mar 30, 2006
posts:1472
votes: 87


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)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11083
votes: 106


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Mar 30, 2006
posts:1472
votes: 87


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)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11083
votes: 106


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Mar 30, 2006
posts:1472
votes: 87


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`;
chomp($w);chomp($h);

$Zf=getcuentavars();

$nw=$fotow;$nh=$fotoh;
$dest=$nw."x".$nh;

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)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Mar 30, 2006
posts:1472
votes: 87


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.

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


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)

Administrator from US 

WebmasterWorld Administrator brett_tabke is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 21, 1999
posts:38079
votes: 28


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Mar 30, 2006
posts:1472
votes: 87


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)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


I never determined why.


Bug? [imagemagick.org]
8:10 pm on Feb 27, 2011 (gmt 0)

Administrator from US 

WebmasterWorld Administrator brett_tabke is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 21, 1999
posts:38079
votes: 28


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)

Junior Member

5+ Year Member

joined:Feb 25, 2011
posts: 91
votes: 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.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Mar 30, 2006
posts:1472
votes: 87


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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members