
Working on the image library, I'm wanting to have all my resources / pictures available via a web site ... but I really don't want to hold them at full resolution - for our needs, 800 x 600 is quite big enough and the storage and bandwidth when I call up lots of pictures would be something of an issue.
Using PHP and the GD library, I'm copying and resizing images that would be over a quarter of a megabyte to download, using the following code, which you'll note maintains the aspect ratio, and limits the image to a maximum of 800 pixels wide, and 600 pixels tall.
if (filesize($fx) > 256000) {
$in_img = imagecreatefromjpeg($fx);
$width = $in_width = imagesx( $in_img );
$height = $in_height = imagesy( $in_img );
if ($width > 800) {
$height = $height * 800 / $width;
$width = 800;
}
if ($height > 600) {
$width = $width * 600 / $height;
$height = 600;
}
$img = imagecreatetruecolor( $width, $height );
imagecopyresized( $img, $in_img, 0, 0, 0, 0, $width, $height, $in_width, $in_height );
imagejpeg($img, "thumbs/$fx" );
$instr = fopen("thumbs/$fx","rb");
$imagebytes = fread($instr,filesize("thumbs/$fx"));
} else {
$instr = fopen($fx,"rb");
$imagebytes = fread($instr,filesize($fx));
}
I have used a temporary directory for my reduced size picture (they're not really small enough to be called thumbnails), and the $imagebytes variable gets sent out later in the PHP script as the response to the http request for the image. We're also (a story I have told
elsewhere) saving the image into a database.
Some more images in the new library ...











(written 2010-01-17 10:01:15)
Associated topics are indexed under
H309 - PHP - Maps, Graphics and Geographics [2675] Redirecting to your main domain for correct security keys - (2010-03-13)
[2390] Dynamic / changing images on your web page - (2009-09-01)
[2361] Geocoding - converting address to latitude / Longitude with PHP via Google - (2009-08-14)
[2343] World Flags in your PHP pages - (2009-08-10)
[1956] Images for Christmas - (2008-12-21)
[1923] Making it all worthwhile - (2008-12-04)
[1756] Ever had One of THOSE mornings? - (2008-08-16)
[1752] Dynamic maps / geographics in PHP - (2008-08-13)
[1734] All around the world? - (2008-08-03)
[1724] addslashes v mysql_real_escape_string in PHP - (2008-07-27)
[1628] Gant charts - drawing them with a PHP script - (2008-05-03)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1104] Drawing dynamic graphs in PHP - (2007-03-09)
[937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
[665] PHP Image viewing application - (2006-04-01)
[563] Merging pictures using PHP and GD - (2006-01-13)
[320] Ordnance Survey - using a 'Get a map' - (2005-05-22)
Some other Articles
Bowerhill as a cycling communityAnd and Or illustrated by locksConsultation in Melksham, on Melkshams futureMelksham Food and Drink Festival, 2010Reducing image size on digital photos - PHPStarting to arrange the picture librarySnow scenes - and how snow effected our businessC course inspires new teaching examplesCreating, setting up and using objects in C++Where are your objects stored in C++?