Hi,
I found the following script for resizing images on the fly and I'm trying to understand why it isn't working (or what it is that I'm doing wrong to cause the function not to work).
Any help appreciated.
I have the following folder structure...
index.php
images/test.jpg
The code for index.php is at the bottom of this post. You'll see i'm passing in as arguments the path to the existing text.jpg and also the path of the new image I'm expecting it to generate but I get the following error message...
PHP Code:Warning: imagecreatefromjpeg(images/newfile.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\Apache2\htdocs\test\index.php on line 14
Warning: imagesx() expects parameter 1 to be resource, boolean given in C:\Apache2\htdocs\test\index.php on line 23
Warning: imagesy() expects parameter 1 to be resource, boolean given in C:\Apache2\htdocs\test\index.php on line 24
Now I appreciate that this script probably isn't the best way of doing things but I'm just looking at proof of concept to help me understand how it is done.PHP Code:/**
* @filename - path to the image
* @tmpname - temporary path to thumbnail
* @xmax - max width
* @ymax - max height
*/
function resize_image($filename, $tmpname, $xmax, $ymax)
{
$ext = explode(".", $filename);
$ext = $ext[count($ext)-1];
if($ext == "jpg" || $ext == "jpeg") {
$im = imagecreatefromjpeg($tmpname);
}
elseif($ext == "png") {
$im = imagecreatefrompng($tmpname);
}
elseif($ext == "gif") {
$im = imagecreatefromgif($tmpname);
}
$x = imagesx($im);
$y = imagesy($im);
if($x <= $xmax && $y <= $ymax) {
return $im;
}
if($x >= $y) {
$newx = $xmax;
$newy = $newx * $y / $x;
}
else {
$newy = $ymax;
$newx = $x / $y * $newy;
}
$im2 = imagecreatetruecolor($newx, $newy);
imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
return $im2;
}
echo "<img src='".resize_image("images/test.jpg", "images/newfile.jpg", 100, 100)."'>";
Again, any help appreciated.
Kind regards,
M.





Bookmarks