envexlabs Posted August 28, 2007 Share Posted August 28, 2007 Hello, I have an image path stored in a database. ie. uploads/pics/picture.jpg How can i grab that image, and create a thumbnail from it? Thanks, envex Link to comment https://forums.phpfreaks.com/topic/67110-image-resize/ Share on other sites More sharing options...
php_tom Posted August 28, 2007 Share Posted August 28, 2007 You need the GD lib installed for this to work (look in phpinfo() to see if you do): function img_resize($image) { $filename = $image; header('Content-type: image/jpeg'); $percent = 0.22; list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb); } So you use it like so: img_resize("/images/large_pics/pic1028489.jpg"); Yeah and it only works on JPGs too, though you can fix this by using imagecreatefromgif($filename); and imagecreatefrompng($filename); if you want. Link to comment https://forums.phpfreaks.com/topic/67110-image-resize/#findComment-336564 Share on other sites More sharing options...
envexlabs Posted August 28, 2007 Author Share Posted August 28, 2007 Hey, So i tried the function and i'm getting this, and it's not working CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality Then a bunch of jibberish. any ideas? Link to comment https://forums.phpfreaks.com/topic/67110-image-resize/#findComment-336617 Share on other sites More sharing options...
php_tom Posted August 28, 2007 Share Posted August 28, 2007 did you print/echo anything out after the function call? that would break the page... what you need to do is something like this: "img_resize.php": img_resize("myImage.jpg"); function img_resize($image) { $filename = $image; header('Content-type: image/jpeg'); $percent = 0.22; list($width, $height) = getimagesize($filename); $newwidth = $width * $percent; $newheight = $height * $percent; $thumb = imagecreatetruecolor($newwidth, $newheight); $source = imagecreatefromjpeg($filename); imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($thumb); } Then in "myPage.php" something like: <img src='img_resize.php" /> Hope that helps. Link to comment https://forums.phpfreaks.com/topic/67110-image-resize/#findComment-336708 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.