[ubuntu-art] PHP/MySQL tasks on the art.ubuntu.com website

Matthew Nuzum mattnuzum at gmail.com
Tue Sep 6 10:44:31 CDT 2005


On 9/6/05, Christian Bjälevik <nafallo at magicalforest.se> wrote:
> tis 2005-09-06 klockan 11:41 +0100 skrev Henrik Nilsen Omma:
> > Hi,
> >
> [snip]
> > * Automatically generated thumbnails (on upload). From backgrounds,
> > screenshots, etc. we could get automatically generated 96x72px
> > thumbnails with Imagemagik or GD, saving the uploader from making them.
> 
> Rather than having 96x72px we should have something that will generate
> the thumbnail based on aspect. Otherwise we will have trouble later when
> people start uploading in 16:9 (16:10?). Not everything is 4:3
> anymore ;-).
> 
> [snip]

Here are some useful ImageMagick commands i use in some of my PHP programs:
find the width and height of an image:
"identify -ping -format '%w~%h' $imagename"
Returns width and height in pixels separated by a tilde (~). From PHP
you can then split the output on the tilde and get the width and
height for use in scaling the image.

Resize an image:
"mogrify -geometry $width"."x".$height $file"
For example, mogrify -geometry 640x480 splash.png (note there are no
spaces in the 640x480)

Composite two images. This is useful in the case where your thumbnails
should keep the aspect ratio of the original image, but you want your
thumbnail to always be a certain size. You can have a little php code
that will use identify to get the dimensions, then do some math to
keep the aspect ratio the same but find the width and height so that
neither are larger than 96 px. Use the mogrify command to scale the
image. Then, you can use the following command to composite the scaled
image onto a clear (or white) 96x96 image so that your HTML can assume
a constant thumbnail size while avoiding image distortion:
"composite $image1 $image2 $output"

So, here is some psuedo code that resembles PHP that will grab an
uploaded image and create two thumbnails that are no larger than 96x96
and 200x200.

// preset the values for $image1_temp, $image2_temp, $white96x96 and
$white200x200

// $image_temp is the original image we'll be resizing
$dimensions = `/usr/bin/identify -ping -format '%w~%h' $image_temp`;
list($width, $height) = explode("~", $dimensions);

// find the dimensions for the new thumbnails
// $thumb1 will be max 96x96, $thumb2 will be max 200x200
if ($width > $height) {
    $width1 = 96;
    $width2 = 200;
    $height1 = round( (96*$height)/$width);
    $height2 = round( (200*$height)/$width);
}elseif($height > $width) {
    $height1 = 96;
    $height2 = 200;
    $width1 = round( (96*$width)/$height);
    $width2 = round( (200*$width)/$height);
} else {
    $height1 = $width1 = 96;
    $height2 = $width2 = 200;
}

// create temporary thumbnails with proportial dimensions
copy($image_temp, $thumb1_temp);
copy($image_temp, $thumb2_temp);
$ok = `/usr/bin/mogrify -geometry $width1"."x".$height1 $thumb1_temp";
$ok = `/usr/bin/mogrify -geometry $width2"."x".$height2 $thumb2_temp";

// create the final thumbnails
$ok = `/usr/bin/composite $thumb1_temp $white96x96 $thumb1`;
$ok = `/usr/bin/composite $thumb2_temp $white200x200 $thumb2`;

-- 
Matthew Nuzum
www.bearfruit.org



More information about the ubuntu-art mailing list