Alright I have some PHP code which does this:
So as far as I can understand it resizes an image and returns the resized image. So with my sample HTML file I did this:PHP Code:<?php
# Constants
define(IMAGE_BASE, '/home/psdesig1/public_html/PHP/');
define(MAX_WIDTH, 50);
define(MAX_HEIGHT, 50);
# Get image location
$image_file = str_replace('..', '', $_SERVER['QUERY_STRING']);
//$image_file = 'uploadsCreek.jpg';
$image_path = IMAGE_BASE . "/$image_file";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$new_width=50;
$new_height=50;
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
# Display the image
header("Content-type: image/jpeg");
imagejpeg($img);
?>
You can see an example of it running here:HTML Code:<img src="http://psdesignzone.co/PHP/thumbnailwv4.php?uploadsCreek.jpg">
http://psdesignzone.com/PHP/TestDisplay.html
I think it's working but my main question is it seems to me that the way i'm doing it, it is resizing the image each time you load the TestDisplay.html page. Is it doing that? If so is there anyway to detect if the image has already been resized and then not resize it ever again?
Thanks for any tips, looking for the best way to dynamically resize images on the Fly in PHP








Bookmarks