|
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 > $max) || ($image_width > $max))
{
if ($image_height > $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>&1";
exec($cmd, $exec_output, $exec_retval);
if($exec_retval > 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.
| Comments: | ||
| Professional class to deal with images | Yuriy Horobey | 10/15/05 06:30 |
| Perlmagick | Wayne | 04/11/05 12:02 |
| RE: Thumbnail-ing with mogrify | ymhuu | 03/16/03 13:41 |
| RE: White Space | matthew | 03/04/03 09:41 |
| RE: It won't work. | Del Langrish | 01/29/03 05:17 |
| It won't work. | Marian | 01/13/03 08:31 |
| RE: imagemagick | Michael Montero | 11/22/02 08:53 |
| Clipping path - Possible? | Kim Steinhaug | 11/07/02 19:57 |
| RE: GD quality | Rasmus Hansen | 10/16/02 10:44 |
| GD quality | Yuriy Horobey | 09/22/02 16:22 |
| exec and IM does not work | Gune | 09/19/02 08:42 |
| TIFF (CMYK) to JPG (RGB) | Josep | 09/13/02 06:42 |
| Temporary Images | Sam Mateosian | 09/09/02 12:42 |
| RE: White Space | Si Watts | 08/19/02 19:21 |
| White Space | Leon Letto | 08/16/02 21:20 |
| RE: Uploading images | Si Watts | 08/11/02 14:04 |
| RE: Thumbnail-ing with mogrify | Si Watts | 08/11/02 14:01 |
| Uploading images | Praggers | 08/09/02 05:56 |
| RE: Thumbnail-ing with mogrify | Stephen | 08/08/02 13:31 |
| RE: Thumbnail-ing with mogrify | Matthew Calthrop | 08/06/02 09:28 |
| Thumbnail-ing with mogrify | Thorsten clausen | 08/02/02 05:15 |
| No library | thrive | 07/29/02 15:01 |
| Did it with netpbm package | Gabe Miano | 07/28/02 10:59 |
| RE: imagemagick | Si Watts | 07/25/02 18:30 |
| RE: Flipping image | Si Watts | 07/25/02 18:25 |
| RE: Follow up for Mogrify -geometry | Si Watts | 07/25/02 18:21 |
| GD&Truecolors | Yuriy Horobey | 07/25/02 15:51 |
| Flipping image | Smarone | 07/24/02 10:33 |
| RE: imagemagick | Randy | 07/24/02 10:01 |
| RE: Follow up for Mogrify -geometry | Jimcurry | 07/24/02 02:29 |
| Follow up for Mogrify -geometry | Si Watts | 07/23/02 11:18 |
| RE: I did it only with PHP/GD (on PUBLIC serv | Yuriy Horobey | 07/23/02 03:52 |
| Professional PHP4 Multimedia Programming | the_phpfreak | 07/23/02 01:46 |
| Did this also | Alex | 07/22/02 16:32 |
| RE: I did it only with PHP/GD (on PUBLIC serv | squashee | 07/22/02 05:41 |
| -geometry | Si Watts | 07/21/02 19:48 |
| RE: I did it only with PHP/GD (on PUBLIC server) | Daniel Higgins | 07/19/02 11:36 |
| RE: imagemagick | bill griffith | 07/18/02 21:17 |
| GD/GIF support | Michael | 07/18/02 17:12 |
| RE: imagemagick | Ben Smith | 07/18/02 12:33 |
| RE: RE: imagemagick | Trebel | 07/18/02 06:09 |
| RE: PHP JPGResizer | yuriy horobey | 07/18/02 03:59 |
| PHP JPGResizer | BankHacker | 07/17/02 16:14 |
| I did it only with PHP/GD (on PUBLIC server) | Yuriy Horobey | 07/17/02 15:48 |
| Little improvement... | HaploZ | 07/17/02 14:10 |
| RE: imagemagick | Joystickit | 07/15/02 19:50 |
| imagemagick | gabe | 07/15/02 13:52 |
| On a public server? | Adam | 07/14/02 21:08 |
| -geometry | Andy | 07/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. | ||


