Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Resizing Images with PHP and Mogrify
Mogrify to the Rescue
Mogrify is one of the programs included in the extremely powerful ImageMagick graphics manipulation suite. It is capable of changing colors, sizes, formats, and effects of an image in any of dozens of common formats. It was the perfect tool for this job.
The first step, after actually uploading the original image to the server, is to determine its current dimensions:
<xmp>
  $currentimagesize = getimagesize($UPLOADS_URL . $imagename);
</xmp>
This returns an array of information about the image. In this case, we're interested in the height and width. $UPLOADS_URL is a global variable we set which simply points at the location of the uploaded original image.
<xmp>
  $image_width = $currentimagesize[0];
  $image_height= $currentimagesize[1];
</xmp>
To resize the image, while retaining its original proportions, we need to determine the current dimensions and then calculate the new desired size (based on our maximum acceptable size of 200 pixels by 200 pixels).

<?php
  
if (($image_height &gt; $max) || ($image_width &gt; $max))
  {   
    if (
$image_height &gt; $image_width)
    {
      
$sizefactor = (double) ($max / $image_height);
    }
    else
    {
      
$sizefactor = (double) ($max / $image_width) ;
    }
  }    

  
$newwidth = (int) ($image_width * $sizefactor);
  
$newheight = (int) ($image_height * $sizefactor);
?>
In other words, if our uploaded image is 800 pixels wide by 200 pixels tall, the $sizefactor is 0.25 (200/800), and both dimensions are multiplied by this number to get a new image size of 200 pixels wide by 50 pixels high. The only thing left to do is the resizing itself:

<?php
  $newsize
= $newwidth . "x" . $newheight;

  
$cmd = "/usr/X11R6/bin/mogrify -resize $newsize ".
         
"$UPLOAD_PATH/$newname 2&gt;&amp;1";     

  
exec($cmd, $exec_output, $exec_retval);

  if(
$exec_retval &gt; 0)
  {
    print
"ERROR: exec() error: $exec_output[0]";
  }
  else
  {
    print
"Image was resized from " . $image_width . "x" .
          
$image_height . " to $newsize :)";
  }
?>
Note that that path to mogrify on your server will probably be different than the one on the Modwest.com server (/usr/X11R6/bin/mogrify). Also note that we redirect STDERR to STDOUT (2>&1) in the exec() line. This assures that we catch any error or warning message generated during the resizing process.
Having resized the image, all that remains is to insert it into a database or move it to the appropriate location.
Disaster Averted
Thanks to ImageMagick's mogrify program, a little bit of PHP code has prevented our client from slapping a huge image on the front page of their site, while requiring no technical graphic manipulation or other additional work on the part of their non-technical staff.

[Page 1]  [Page 2]  


Comments:
Professional class to deal with imagesYuriy Horobey10/15/05 06:30
PerlmagickWayne04/11/05 12:02
RE: Thumbnail-ing with mogrify ymhuu03/16/03 13:41
RE: White Spacematthew03/04/03 09:41
RE: It won't work.Del Langrish01/29/03 05:17
It won't work.Marian01/13/03 08:31
RE: imagemagickMichael Montero11/22/02 08:53
Clipping path - Possible?Kim Steinhaug11/07/02 19:57
RE: GD qualityRasmus Hansen10/16/02 10:44
GD qualityYuriy Horobey09/22/02 16:22
exec and IM does not workGune09/19/02 08:42
TIFF (CMYK) to JPG (RGB)Josep09/13/02 06:42
Temporary ImagesSam Mateosian09/09/02 12:42
RE: White SpaceSi Watts08/19/02 19:21
White SpaceLeon Letto08/16/02 21:20
RE: Uploading imagesSi Watts08/11/02 14:04
RE: Thumbnail-ing with mogrify Si Watts08/11/02 14:01
Uploading imagesPraggers08/09/02 05:56
RE: Thumbnail-ing with mogrify Stephen08/08/02 13:31
RE: Thumbnail-ing with mogrify Matthew Calthrop08/06/02 09:28
Thumbnail-ing with mogrify Thorsten clausen08/02/02 05:15
No librarythrive07/29/02 15:01
Did it with netpbm packageGabe Miano07/28/02 10:59
RE: imagemagickSi Watts07/25/02 18:30
RE: Flipping imageSi Watts07/25/02 18:25
RE: Follow up for Mogrify -geometrySi Watts07/25/02 18:21
GD&TruecolorsYuriy Horobey07/25/02 15:51
Flipping imageSmarone07/24/02 10:33
RE: imagemagickRandy07/24/02 10:01
RE: Follow up for Mogrify -geometryJimcurry07/24/02 02:29
Follow up for Mogrify -geometrySi Watts07/23/02 11:18
RE: I did it only with PHP/GD (on PUBLIC servYuriy Horobey07/23/02 03:52
Professional PHP4 Multimedia Programmingthe_phpfreak07/23/02 01:46
Did this alsoAlex07/22/02 16:32
RE: I did it only with PHP/GD (on PUBLIC servsquashee07/22/02 05:41
-geometrySi Watts07/21/02 19:48
RE: I did it only with PHP/GD (on PUBLIC server)Daniel Higgins07/19/02 11:36
RE: imagemagickbill griffith07/18/02 21:17
GD/GIF supportMichael07/18/02 17:12
RE: imagemagickBen Smith07/18/02 12:33
RE: RE: imagemagickTrebel07/18/02 06:09
RE: PHP JPGResizeryuriy horobey07/18/02 03:59
PHP JPGResizerBankHacker07/17/02 16:14
I did it only with PHP/GD (on PUBLIC server)Yuriy Horobey07/17/02 15:48
Little improvement...HaploZ07/17/02 14:10
RE: imagemagickJoystickit07/15/02 19:50
imagemagickgabe07/15/02 13:52
On a public server?Adam07/14/02 21:08
-geometryAndy07/12/02 14:40
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.