note 35146 deleted from function.imagecopyresampled by aidan

From: Date: Mon, 04 Oct 2004 09:44:31 +0000
Subject: note 35146 deleted from function.imagecopyresampled by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-77957@lists.php.net to get a copy of this message
Note Submitter: Suggestion based on a prior comment. ---- Using a SLIGHTLY modified version of a formula suggested (by webmaster at fredrickville dot com) in a comment above, you can easily get exact pixel sizes for a thumbnail that scales with no aspect ratio troubles. <?php $image_details = getimagesize("$dir_name/$image"); $imagesize_x = $image_details[0]; $imagesize_y = $image_details[1]; $thumbsize_x = 100; $thumbsize_y = 75; // The two lines beginning with (int) are the super important magic formula part. (int)$thumb_x = ($imagesize_x <= $imagesize_y) ? round(($imagesize_x * $thumbsize_y)/$imagesize_y) : $thumbsize_x; (int)$thumb_y = ($imagesize_x > $imagesize_y) ? round(($imagesize_y * $thumbsize_x)/$imagesize_x) : $thumbsize_y; echo "Projected Thumbnail Size to fit within $thumbsize_x x $thumbsize_y = $thumb_x x $thumb_y"; ?> Output when I feed it a 276 x 110 image: Projected Thumbnail Size to fit within 100 x 75 = 100 x 40 Output when I feed it a 78 x 100 image: Projected Thumbnail Size to fit within 100 x 75 = 59 x 75 Notice that it does not matter if the image is wider than it is tall, or taller than it is wide. The formula handles it and gives you nice thumbnail dimensions to work with. Probably the simplest use for this easy formula would be to plug it into a user 'function()' and call it by name. Personally, I use this formula to create perfect little rows and columns of clickable images on a page. Note: This code works perfectly under PHP 4.3.1 as written (assuming you pass the getimagesize() function a valid image filename) but no guarantees are made that it will output usable numbers if you fiddle with the formula too much beyond changing variable names. 8') Thanks go to webmaster at fredrickville dot com, who posted the original formula which gave me this handy idea further up this page.

« previous php.notes (#77957) next »