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




At the moment thumbnails are scales to a horizontal width of 96px, so the width is fixed, but the height is scaled. The easiest ImageMagick command for this is `mogrify -scale 96 filename.png`, but we also have some GD code in the screenshots the does appropriate scaling.

The GD library includes some useful methods for manipulating images, as well as determining height, width and image type. The getimagesize() function not only returns the image size, but also the image type. The interesting thing about this function is that unlike other GD functions, it takes a filename as an argument, so it doesn't require the whole image to be loaded into memory.

The disadvantage of using GD to scale images is that it requires the image to be loaded into memory, which can cause PHP try and allocate more memory than the max value set in the PHP configuration. I would suggest we use GD to determine information about the images, such as type and size, and then use Imagemagick to scale them.

I am hoping we can add support for SVG images too, but this would probably require having librsvg installed on the server. librsvg includes a command line application to export SVG files to raster.

-Thomas


Matthew Nuzum wrote:
On 9/6/05, Christian Bjälevik <nafallo 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`;





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]