wrathican Posted May 4, 2008 Share Posted May 4, 2008 hey im making an image gallery and using php as an uploader and thumbnail generator. i can make the file upload easily enough, rename it and save it in the desired directory. what confuses me next is how to make the thumbnail. ive searched on google for ages and all the tutorials seem to be the same. they give you example code without telling you whats really going on. i have the basic gist of whats going on. but all the examples i have seen use: imagejpeg($image); does this actually save the image to my webserver? this is the function i have so far. what should come after the imagecopyresampled? any ideas how i could improve the code? <?php function createThumb($oldFile, $userFile, $imgDir, $imgWidth) { //extensions array $validExt = array ('.jpg', '.jpeg', '.png'); //get the file ext $ext = strrchr($userFile, "."); $ext = strtolower($ext); //check the file ext against the array if(in_array($ext, $validExt)) { //needle is in haystack //rename the file $newName = md5(rand() * time()) . $ext; //new file dest $destFile = $imgDir . $newName; //create the thumbnail here //check file ext if($ext == '.jpg' || $ext == '.jpeg') { //image is a jpeg $srcImg = imagecreatefromjpeg($oldFile); }elseif($ext == '.png') { //image is a png $srcImg = imagecreatefrompng($oldFile); }elseif($ext == '.gif.') { //image is a gif $srcImg = imagecreatefromgif($oldFile); } $srcSize = getimagesize($srcImg); //set image dimensions $dest_x = $imgWidth; $dest_y = ($imgWidth / $srcSize[0]) * $srcSize[1]; //create truecolour image $dst_img = imagecreatetruecolor($dest_x, $dest_y); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]); }else{ //needle is not in haystack. exit return false; } } ?> thanks Link to comment https://forums.phpfreaks.com/topic/104060-help-with-gd/ Share on other sites More sharing options...
dooper3 Posted May 4, 2008 Share Posted May 4, 2008 You need to then make the image become real... imagejpeg, imagegif and imagepng do this for you, but you need to declare the header first, to tell the browser what it is you are sending to it, then you need to destroy the image after. To display the image without saving it you use imagejpeg($myimage), but if you want to save it (and not show it), add the directory to save to, like this - imagejpeg($myimage,$mysavepath). so the code to prepare and save the image will look like this: <?php header("Content-type: image/jpeg"); imagejpeg($myimage,$mysavepath); imagedestroy($myimage); ?> Obviously you can substitute jpeg, gif and png for each other in these examples. php.net has good examples of how to do this stuff. Link to comment https://forums.phpfreaks.com/topic/104060-help-with-gd/#findComment-532688 Share on other sites More sharing options...
wrathican Posted May 4, 2008 Author Share Posted May 4, 2008 hi, thanks for the advice. i have amended my code using your advice. the function returns false. the directory exists on my server, so thats not the problem. any ideas what it could be? this is the function i have ended up with: <?php $albumImg = '../images/album/'; $thumbWidth = 150; $tmpFile = $_FILES['image']['tmp_name']; $userFile = $_FILES['image']['name']; function createThumb($oldFile, $userFile, $imgDir, $imgWidth) { //extensions array $validExt = array ('.jpg', '.jpeg', '.png'); //get the file ext $ext = strrchr($userFile, "."); $ext = strtolower($ext); //check the file ext against the array if(in_array($ext, $validExt)) { //needle is in haystack //rename the file $newName = md5(rand() * time()) . $ext; //new file dest $destFile = $imgDir . $newName; //create the thumbnail here //check file ext if($ext == '.jpg' || $ext == '.jpeg') { //image is a jpeg $srcImg = imagecreatefromjpeg($oldFile); }elseif($ext == '.png') { //image is a png $srcImg = imagecreatefrompng($oldFile); }elseif($ext == '.gif.') { //image is a gif $srcImg = imagecreatefromgif($oldFile); } $srcSize = getimagesize($srcImg); //set image dimensions $dest_x = $imgWidth; $dest_y = ($imgWidth / $srcSize[0]) * $srcSize[1]; //create truecolour image $dst_img = imagecreatetruecolor($dest_x, $dest_y); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dest_x, $dest_y, $srcsize[0], $srcsize[1]); if($ext == '.jpg' || $ext == '.jpeg') { //image is a jpeg header("Content-type: image/jpeg"); $image = imagejpeg($dst_img, $destFile); $destroy = imagedestroy($dst_img); if($image == false || $destroy == false) { return false; exit; } }elseif($ext == '.png') { //image is a png header("Content-type: image/png"); $image = imagepng($dst_img, $destFile); $destroy = imagedestroy($dst_img); if($image == false || $destroy == false) { return false; exit; } }elseif($ext == '.gif.') { //image is a gif header("Content-type: image/gif"); $image = imagegif($dst_img, $destFile); $destroy = imagedestroy($dst_img); if($image == false || $destroy == false) { return false; exit; } } return $destFile; }else{ //needle is not in haystack. exit return false; } } $result = createThumb ($tmpFile, $userFile, $albumImg, $thumbWidth); ?> Link to comment https://forums.phpfreaks.com/topic/104060-help-with-gd/#findComment-532976 Share on other sites More sharing options...
dooper3 Posted May 5, 2008 Share Posted May 5, 2008 probably because the way you are getting the file extension is overly complicated, and if it isn't a valid one in your array it is designed to return false. Link to comment https://forums.phpfreaks.com/topic/104060-help-with-gd/#findComment-533300 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.