Helpful Information
 
 
Category: Post a PHP snippet
Dynamic Image Resizing

This script is usefull for dynamicaly thumbnailing images. Its pretty simple. One page that you run your images through. Example


<img src="thumbit.php?image=http://www.mysite.com/images/myphoto.jpg">

Here is the code.


<?php

// Input
$s_image = $_GET['image']; // Image url set in the URL. ex: thumbit.php?image=URL
$e_image = "error.jpg"; // If there is a problem using the file extension then load an error JPG.
$max_width = 100; // Max thumbnail width.
$max_height = 250; // Max thumbnail height.
$quality = 100; // Do not change this if you plan on using PNG images.

// Resizing and Output : Do not edit below this line unless you know what your doing.

if (preg_match("/.jpg/i", "$s_image")) {

header('Content-type: image/jpeg');
list($width, $height) = getimagesize($s_image);
$ratio = ($width > $height) ? $max_width/$width : $max_height/$height;
if($width > $max_width || $height > $max_height) {
$new_width = $width * $ratio;
$new_height = $height * $ratio;
} else {
$new_width = $width;
$new_height = $height;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($s_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, $quality);
imagedestroy($image_p);

}
elseif (preg_match("/.png/i", "$s_image")) {

header('Content-type: image/png');
list($width, $height) = getimagesize($s_image);
$ratio = ($width > $height) ? $max_width/$width : $max_height/$height;
if($width > $max_width || $height > $max_height) {
$new_width = $width * $ratio;
$new_height = $height * $ratio;
} else {
$new_width = $width;
$new_height = $height;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($s_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p, null, $quality);
imagedestroy($image_p);

}
elseif (preg_match("/.gif/i", "$s_image")) {

header('Content-type: image/gif');
list($width, $height) = getimagesize($s_image);
$ratio = ($width > $height) ? $max_width/$width : $max_height/$height;
if($width > $max_width || $height > $max_height) {
$new_width = $width * $ratio;
$new_height = $height * $ratio;
} else {
$new_width = $width;
$new_height = $height;
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromgif($s_image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($image_p, null, $quality);
imagedestroy($image_p);

}
else {

// Show the error JPG.
header('Content-type: image/jpeg');
imagejpeg($e_image, null, $quality);
imagedestroy($e_image);

}

?>

I hope this is usefull. If you want to have JPG and GIF images at different qualities and automatically changing PNG to 100% quality then replace the quality variable with the fallowing code.


if (!(preg_match("/.png/i", "$s_image"))) {
$quality = 75; // Set your quality
} else {
$quality = 100; // Do not change this or PNGs will not load correctly
}

nice, mine is a little faster with the generation but does not handle png.

nice, mine is a little faster with the generation but does not handle png.

Gerneration time is defined by compression. If the quality is lower it loads in a second for me, even on Dial-Up.

how would i lower the quality of a gif or jpeg image?



nevermind i see it

A few notes here.

The GD libraries use a proprietary format for manipulating the pixel colours which is quite bulky (basically about 9 times the size of a 80% quality jpeg - eg if you put a 20kb jpeg into GD the processor would be working on roughly 180kb of data during the resizing / transformation).

As such, it is never a good idea to create thumbnails at every page load. Instead, a server cached image should be created and the <img tag pointed at that. A script such as the one Element posted could be used just by changing the imagePng($image_p, null, $quality); calls to something like imagePng($image_p, '/home/site/imgs/mysavedimage.png', $quality);

May also like to note that reading the mime header to determine image type is considered more accurate, eg:


$f_type = getimagesize($base_image);
$created_image = ($f_type[2] < 4)
? ($f_type[2] < 3)
? ($f_type[2] < 2)
? ($f_type[2] < 1) ?
NULL
: imagecreatefromgif($base_image)
: imagecreatefromjpeg($base_image)
: imagecreatefrompng($base_image)
: NULL;
if($created_image !== NULL)
{ // ok to proceed as we have a created image

On an aside, the libraries only recognize ping compression of -1 through 9 as mentioned at gdImagePngEx (http://www.boutell.com/gd/manual2.0.33.html#gdImagePngEx) so I guess that an 85 would be re-evaluated to the nearest integer after division.

removed link as it semi goes against the forum ideals

The script worked great for me for the first couple of days...

All of a sudden I get errors now, and I didn't change anything. I have done some research on the errors, without any answers...maybe you guys can help me out.


<br />
<b>Warning</b>: getimagesize(http://www.mysite.com/photos/butterfly.gif): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
in <b>/home//public_html/reviews/thumbit.php</b> on line <b>53</b><br />
<br />
<b>Warning</b>: Division by zero in <b>/home//public_html/reviews/thumbit.php</b> on line <b>54</b><br />
<br />
<b>Warning</b>: imagecreatetruecolor(): Invalid image dimensions in <b>/home//public_html/reviews/thumbit.php</b> on line <b>62</b><br />
<br />
<b>Warning</b>: imagecreatefromgif(http://www.mysite.com/photos/butterfly.gif): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
in <b>/home//public_html/reviews/thumbit.php</b> on line <b>63</b><br />
<br />
<b>Warning</b>: imagecopyresampled(): supplied argument is not a valid Image resource in <b>/home//public_html/reviews/thumbit.php</b> on line <b>64</b><br />
<br />
<b>Warning</b>: imagegif(): supplied argument is not a valid Image resource in <b>/home//public_html/reviews/thumbit.php</b> on line <b>65</b><br />
<br />
<b>Warning</b>: imagedestroy(): supplied argument is not a valid Image resource in <b>/home//public_html/reviews/thumbit.php</b> on line <b>66</b><br />



(Directory structure was edited for privacy)

Thanks - besides this error, the script worked awesome

You shouldn't use URL's. It tries to connect from your server to your ferver which is the long way around. Your server gives you the 500 error and without the values getimagesize() sets you get the "division by zero" errors.
The false that is returned from imagecreatefrom***() gives all the other errors.

You shouldn't use URL's. It tries to connect from your server to your ferver which is the long way around. Your server gives you the 500 error and without the values getimagesize() sets you get the "division by zero" errors.
The false that is returned from imagecreatefrom***() gives all the other errors.
The URL should be contained in quotes if your just putting it in like $s_image = "URL";

And if the image is on your own server it is better to do something like thumbit.php?image=file/image.ext As for using mime types, I have been looking for better way to do it and that seems to work. As for cache images I was told not to use them and use this way, and it works fast and crisply for me so I see no reason to change it if someone wants a fast easy way of doing it with just a remote or local image.










privacy (GDPR)