wrathican Posted May 12, 2008 Share Posted May 12, 2008 hey peeps ive made an upload function and then a function to create a thumbnail from the uploaded file. the file uploads correctly but the thumbnail doesn't seem to get created. here is the upload function: function uploadFile ($oldFile, $userFile, $imgDir) { //extensions array $validExt = array ('.jpg', '.jpeg', '.png', '.gif'); //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 //process upload //rename the file $newName = md5(rand() * time()) . $ext; //new file dest $destFile = $imgDir . $newName; //move the file to tmpDir $result = move_uploaded_file($oldFile, $destFile); if($result == false) { //there was an error uploading return false; }else{ //file was uploaded return $destFile; } }else{ //needle is not in haystack. exit return false; } } and heres the thumbnail creation function: function createThumb($oldFile, $userFile, $imgDir, $picWidth) { $result = uploadFile($oldFile, $userFile, $imgDir); if ($result == false) { //file could not be uploaded return false; }else{ //file was uploaded. create thumbnail //pathinfo $file = pathinfo($result); //find the .ext $ext = $file['extension']; //strip .ext to lower $ext = strtolower($ext); //get image sizes $sizes = getimagesize($result); //ratio $ratio = $sizes[0]/$sizes[1]; //create new height $newHeight = round($picWidth/$aspect); //create true colour image $srcImg = imagecreatetruecolor($picWidth, $newHeight); if($ext == 'jpg' || $ext == 'jpeg') { $tmpImg = imagecreatefromjpeg($result); }elseif($ext == 'png') { $tmpImg = imagecreatefrompng($result); }elseif($ext == 'gif') { $tmpImg = imagecreatefromgif($result); } imagecopyresampled($srcImg, $tmpImg, 0, 0, 0, 0, $picWidth, $newHeight, $sizes[0], $sizes[1]); //save image if($ext == 'jpg' || $ext == 'jpeg') { imagejpeg($srcImg, $result, 100); }elseif($ext == 'png') { imagepng($scrImg, $result, 100); }elseif($ext == 'gif') { imagegif($srcImg, $result, 100); } imagedestroy($srcImg); imagedestroy($tmpImg); } } and heres how i call the function: $tmpFile = $_FILES['image']['tmp_name']; $userFile = $_FILES['image']['name']; $thumbWidth = 150; $albumImg = '../images/album/'; $result = createThumb ($tmpFile, $userFile, $albumImg, $thumbWidth); anyone have any idea why the script wont create the thumbnail? Link to comment https://forums.phpfreaks.com/topic/105256-image-thumbnail-creation/ Share on other sites More sharing options...
kev wood Posted May 12, 2008 Share Posted May 12, 2008 i cannot be bothered to look through your code because i have a headache from a heavy drinking weekend but if you would like i have made the code you are looking for before if you want me to email it to you. it uploads the images and creates the thumbnail image. it also stores them in to different files on the server. it would need alittle bit of tweaking so that it works for you i.e. changing where the files are stored and things like that but at least you no it already is being used on a site and it works as intended. Link to comment https://forums.phpfreaks.com/topic/105256-image-thumbnail-creation/#findComment-539040 Share on other sites More sharing options...
wrathican Posted May 12, 2008 Author Share Posted May 12, 2008 sent you a PM with my email Thanks Link to comment https://forums.phpfreaks.com/topic/105256-image-thumbnail-creation/#findComment-539042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.