image resize script errors

Thread Solved

Join Date: Oct 2006
Posts: 198
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 1
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

image resize script errors

 
0
  #1
Oct 24th, 2008
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:
  1. <?php
  2. //get values for other required vars
  3. $filename = $_POST['uploadfile']; //origional filename
  4. $filetype = $_FILES['uploadfile']['type']; //uploaded filetype
  5. $filesize = $_FILES['uploadfile']['size']; //uploaded file size
  6. $filenametmp = $_FILES['uploadfile']['tmp_name']; //uploaded file's server side temporary name
  7.  
  8. $uploaddir = '../images/'; //upload directory
  9. $uploadfile = $uploaddir.$iid.basename($_FILES['uploadfile']['name']); //upload directory + rand + origional name
  10.  
  11. $size = getimagesize($filenametmp);
  12. $width = $size[0];
  13. $height = $size[1];
  14.  
  15. //get file extension for validation
  16. $ext = strrchr($file_name,'.');
  17. $ext = strtolower($ext);
  18. $limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
  19. $getExt = explode ('.', $filename);
  20. $file_ext = $getExt[count($getExt)-1];
  21.  
  22. //set width variables
  23. $ThumbWidth = 80;
  24.  
  25. if($filesize){
  26. //keep image type
  27. if($file_type == "image/pjpeg" || $filenametmp == "image/jpeg" || $filenametmp == "image/jpg")
  28. {$new_img = imagecreatefromjpeg($filenametmp);}
  29.  
  30. elseif($file_type == "image/x-png" || $filenametmp == "image/png")
  31. {$new_img = imagecreatefrompng($filenametmp);}
  32.  
  33. elseif($file_type == "image/gif")
  34. {$new_img = imagecreatefromgif($filenametmp);}
  35. }//if
  36.  
  37.  
  38. //make width and height array from getimagesize returned array
  39. list($width, $height) = getimagesize($filenametmp);
  40.  
  41. //calculate the image ratio
  42. $imgratio=$width/$height;
  43. if ($imgratio>1){
  44. $newwidth = $ThumbWidth;
  45. $newheight = $ThumbWidth/$imgratio;}
  46. else{
  47. $newheight = $ThumbWidth;
  48. $newwidth = $ThumbWidth*$imgratio;}
  49.  
  50. //resize image.
  51. $resized_img = imagecreatetruecolor($newwidth,$newheight);
  52.  
  53. //the resizing is going on here!
  54. //error 1 results from the line below:
  55. imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  56.  
  57. //finally, save the image
  58. ImageJpeg ($resized_img,"../thumbs/".$iid.$file_ext);
  59. ImageDestroy ($resized_img);
  60. //error 2 results form the line below:
  61. ImageDestroy ($new_img);
  62.  
  63. move_uploaded_file ($filenametmp, "../images/".$iid.$file_ext);
  64. ?>

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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 433
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: image resize script errors

 
0
  #2
Oct 24th, 2008
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 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:
  1. <?php
  2. header("Content-Type: text/plain");
  3.  
  4. // Validate upload
  5. if(!isset($_FILES['uploadfile'])){
  6. die("File was not uploaded");
  7. }
  8. if($_FILES['uploadfile']['error'] != 0){
  9. die("File upload failed. (Code #". $_FILES['uploadfile']['error'] .")");
  10. }
  11.  
  12. // Set and validate the file directory info
  13. $uploadDir = "/path/to/upload/dir/";
  14. $originalDir = $uploadDir . "originals/";
  15. $thumbDir = $uploadDir . "thumbs/";
  16. if(!is_writable($originalDir) || !is_writable($thumbDir)) {
  17. die("PHP does not have permission to write to the upload directories.");
  18. }
  19.  
  20. // Get and validate the uploaded image information
  21. $image = $_FILES['uploadfile'];
  22. $imageInfo = getimagesize($image['tmp_name']);
  23. $allowedMime = array("image/jpeg", "image/png", "image/gif");
  24. if(!in_array($imageInfo['mime'], $allowedMime)) {
  25. die("Image Mime type is not allowed (". $imageInfo['mime'] .")");
  26. }
  27.  
  28. // Move the original to it's new location
  29. $originalPath = $originalDir . $image['name'];
  30. if(!move_uploaded_file($image['tmp_name'], $originalPath)) {
  31. die("Failed to move the original image");
  32. }
  33.  
  34. // Load the original into a GD object
  35. switch ($imageInfo['mime']) {
  36. case "image/jpeg":
  37. $originalImage = imagecreatefromjpeg($originalPath);
  38. break;
  39. case "image/png":
  40. $originalImage = imagecreatefrompng($originalPath);
  41. break;
  42. case "image/gif":
  43. $originalImage = imagecreatefromgif($originalPath);
  44. break;
  45. // No default because the above mime check makes sure it isn't needed
  46. }
  47.  
  48. // Calculate the thumb size
  49. $thumbMaxSize = 80;
  50. $originalRatio = $imageInfo[1] / $imageInfo[0];
  51. if($originalRatio < 0) {
  52. $thumbWidth = $thumbMaxSize;
  53. $thumbHeight = $thumbMaxSize * $originalRatio;
  54. }
  55. else {
  56. $thumbHeight = $thumbMaxSize;
  57. $thumbWidth = $thumbMaxSize / $originalRatio;
  58. }
  59.  
  60. // Create the thumb
  61. $thumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
  62. if(!imagecopyresampled($thumbImage, $originalImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $imageInfo[0], $imageInfo[1])){
  63. die("Failed to create the thumb");
  64. }
  65.  
  66. // Save the thumb
  67. $thumbPath = $thumbDir . "thumb_" . $image['name'];
  68. switch ($imageInfo['mime']) {
  69. case "image/jpeg":
  70. imagejpeg($thumbImage, $thumbPath);
  71. break;
  72. case "image/png":
  73. imagepng($thumbImage, $thumbPath);
  74. break;
  75. case "image/gif":
  76. imagegif($thumbImage, $thumbPath);
  77. break;
  78. }
  79.  
  80. // Delete resources
  81. imagedestroy($thumbImage);
  82. imagedestroy($originalImage);
  83.  
  84. // Show success message
  85. echo "Upload complete";
  86. ?>
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 198
Reputation: MaxMumford is an unknown quantity at this point 
Solved Threads: 1
MaxMumford's Avatar
MaxMumford MaxMumford is offline Offline
Junior Poster

Re: image resize script errors

 
0
  #3
Oct 24th, 2008
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
Ill solve somebody's thread someday! xD
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC