| | |
image resize script errors
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi all!
Im making an upload script that uploads an image file, saves it and saves a thumbnail copy in a different directory.
I have tried a couple of scripts and have found one which I think will work nicely but i cannot get it working.
The script is below:
The errors are:
error #1:
Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/sites/mister-style.com/public_html/photos/add/process.php on line 120
error#2:
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/sites/mister-style.com/public_html/photos/add/process.php on line 125
I hope somebody can help, iv spent aaages on trying to get something working xD
Thanks in advance,
Max
Im making an upload script that uploads an image file, saves it and saves a thumbnail copy in a different directory.
I have tried a couple of scripts and have found one which I think will work nicely but i cannot get it working.
The script is below:
PHP Syntax (Toggle Plain Text)
<?php //get values for other required vars $filename = $_POST['uploadfile']; //origional filename $filetype = $_FILES['uploadfile']['type']; //uploaded filetype $filesize = $_FILES['uploadfile']['size']; //uploaded file size $filenametmp = $_FILES['uploadfile']['tmp_name']; //uploaded file's server side temporary name $uploaddir = '../images/'; //upload directory $uploadfile = $uploaddir.$iid.basename($_FILES['uploadfile']['name']); //upload directory + rand + origional name $size = getimagesize($filenametmp); $width = $size[0]; $height = $size[1]; //get file extension for validation $ext = strrchr($file_name,'.'); $ext = strtolower($ext); $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp"); $getExt = explode ('.', $filename); $file_ext = $getExt[count($getExt)-1]; //set width variables $ThumbWidth = 80; if($filesize){ //keep image type if($file_type == "image/pjpeg" || $filenametmp == "image/jpeg" || $filenametmp == "image/jpg") {$new_img = imagecreatefromjpeg($filenametmp);} elseif($file_type == "image/x-png" || $filenametmp == "image/png") {$new_img = imagecreatefrompng($filenametmp);} elseif($file_type == "image/gif") {$new_img = imagecreatefromgif($filenametmp);} }//if //make width and height array from getimagesize returned array list($width, $height) = getimagesize($filenametmp); //calculate the image ratio $imgratio=$width/$height; if ($imgratio>1){ $newwidth = $ThumbWidth; $newheight = $ThumbWidth/$imgratio;} else{ $newheight = $ThumbWidth; $newwidth = $ThumbWidth*$imgratio;} //resize image. $resized_img = imagecreatetruecolor($newwidth,$newheight); //the resizing is going on here! //error 1 results from the line below: imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //finally, save the image ImageJpeg ($resized_img,"../thumbs/".$iid.$file_ext); ImageDestroy ($resized_img); //error 2 results form the line below: ImageDestroy ($new_img); move_uploaded_file ($filenametmp, "../images/".$iid.$file_ext); ?>
The errors are:
error #1:
Warning: imagecopyresized(): supplied argument is not a valid Image resource in /home/sites/mister-style.com/public_html/photos/add/process.php on line 120
error#2:
Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/sites/mister-style.com/public_html/photos/add/process.php on line 125
I hope somebody can help, iv spent aaages on trying to get something working xD
Thanks in advance,
Max
Ill solve somebody's thread someday! xD
Hi.
You seem to mix up your variables a lot. You define them using one name and then try to use them using another.
And you are validating the image based on the extension, which is pretty much useless. Validating the mime type returned by the
This is a modified version of code I have used in the past.
It should do pretty much the same thing yours is meant to do:
You seem to mix up your variables a lot. You define them using one name and then try to use them using another.
And you are validating the image based on the extension, which is pretty much useless. Validating the mime type returned by the
getimagesize function is a lot more reliable.This is a modified version of code I have used in the past.
It should do pretty much the same thing yours is meant to do:
php Syntax (Toggle Plain Text)
<?php header("Content-Type: text/plain"); // Validate upload if(!isset($_FILES['uploadfile'])){ die("File was not uploaded"); } if($_FILES['uploadfile']['error'] != 0){ die("File upload failed. (Code #". $_FILES['uploadfile']['error'] .")"); } // Set and validate the file directory info $uploadDir = "/path/to/upload/dir/"; $originalDir = $uploadDir . "originals/"; $thumbDir = $uploadDir . "thumbs/"; if(!is_writable($originalDir) || !is_writable($thumbDir)) { die("PHP does not have permission to write to the upload directories."); } // Get and validate the uploaded image information $image = $_FILES['uploadfile']; $imageInfo = getimagesize($image['tmp_name']); $allowedMime = array("image/jpeg", "image/png", "image/gif"); if(!in_array($imageInfo['mime'], $allowedMime)) { die("Image Mime type is not allowed (". $imageInfo['mime'] .")"); } // Move the original to it's new location $originalPath = $originalDir . $image['name']; if(!move_uploaded_file($image['tmp_name'], $originalPath)) { die("Failed to move the original image"); } // Load the original into a GD object switch ($imageInfo['mime']) { case "image/jpeg": $originalImage = imagecreatefromjpeg($originalPath); break; case "image/png": $originalImage = imagecreatefrompng($originalPath); break; case "image/gif": $originalImage = imagecreatefromgif($originalPath); break; // No default because the above mime check makes sure it isn't needed } // Calculate the thumb size $thumbMaxSize = 80; $originalRatio = $imageInfo[1] / $imageInfo[0]; if($originalRatio < 0) { $thumbWidth = $thumbMaxSize; $thumbHeight = $thumbMaxSize * $originalRatio; } else { $thumbHeight = $thumbMaxSize; $thumbWidth = $thumbMaxSize / $originalRatio; } // Create the thumb $thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight); if(!imagecopyresampled($thumbImage, $originalImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageInfo[0], $imageInfo[1])){ die("Failed to create the thumb"); } // Save the thumb $thumbPath = $thumbDir . "thumb_" . $image['name']; switch ($imageInfo['mime']) { case "image/jpeg": imagejpeg($thumbImage, $thumbPath); break; case "image/png": imagepng($thumbImage, $thumbPath); break; case "image/gif": imagegif($thumbImage, $thumbPath); break; } // Delete resources imagedestroy($thumbImage); imagedestroy($originalImage); // Show success message echo "Upload complete"; ?>
ahh fantastic, thanks for the script. I think I just dived in head first without trying to understand whats happening where first which is why my variables were so scrambled.
just incase anybody else wants to use the script I changed the permissions on my files to 755 and it worked great
Thanks again, much appreciated.
Max
just incase anybody else wants to use the script I changed the permissions on my files to 755 and it worked great

Thanks again, much appreciated.
Max
Ill solve somebody's thread someday! xD
![]() |
Similar Threads
- file upload (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: $root = $_SERVER
- Next Thread: upload
| Thread Tools | Search this Thread |
advanced apache api array basics beginner binary broken cakephp check checkbox class cms code codingproblem combobox cookies cron curl database date datepart display dynamic echo email error file files folder form forms function functions google head href htaccess html image include includingmysecondfileinthechain insert ip javascript job joomla js limit link login mail menu mlm mobile multiple mysql nodes oop outofmemmory paging parse password paypal pdf php problem procedure query radio random recursion remote script search server sessions smarty sms source space sql stored syntax system table traffic tutorial unicode up-to-date update upload url validation validator variable video web webapplications youtube





