ukweb Posted October 12, 2007 Share Posted October 12, 2007 Hi, I'm making an app which uploads and image and creates a thumbnail, only when I run it I get the following error: Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/26403/domains/click.sjwright.co.uk/html/Connections/rental_click_sql.php on line 71 I cannot see why because the code (I think) is spot on. I'm hosted with media temple and GD library IS installed. The code is below, any help would be received with much thanks. Stephen // Configuration - Your Options $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); $max_filesize = 524288; $upload_path = 'user_files/uploaded/images/uploaded/'; $upload_thumb_path = $upload_path.'thumbs/'; $filename = $_FILES['userfile']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) { echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked. list($width_orig, $height_orig) = getimagesize($upload_path.$filename); $width = 100; $ratio = ($width / $width_orig) * 100; $height = ($ratio * $height_orig) / 100; if(imagecopyresampled($upload_thumb_path.$filename, $upload_path.$filename, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig)) echo 'Thumbnail created successfully'.$filename; else echo 'Thumbnail hit a wall!'; } else echo 'There was an error during the file upload. Please try again.'; Quote Link to comment Share on other sites More sharing options...
Barand Posted October 12, 2007 Share Posted October 12, 2007 The source and dest image arguments (first two) of imagecopyresampled() must be GD images, not files. Quote Link to comment Share on other sites More sharing options...
ukweb Posted October 12, 2007 Author Share Posted October 12, 2007 Right, I've got it using GD images now, and it says its successfully resized the thumb, but when I open the thumb image in safari its not been resized, its still the same size! This is really p***ing me off now!! I cannot see why it wont work! The code is below, any feedback would be received with much thanks. Stephen // Configuration - Your Options $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); $max_filesize = 524288; $upload_path = 'user_files/uploaded/images/uploaded/'; $upload_thumb_path = $upload_path.'thumbs/'; $filename = $_FILES['userfile']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) { echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked. if(copy($upload_path.$filename, $upload_thumb_path.$filename)) { echo 'Thumbnail file created successfully'.$filename; list($width, $height) = getimagesize($upload_path.$filename); $width_thumb = 100; $ratio = ($width_thumb / $width) * 100; $height_thumb = ($ratio * $height) / 100; $thumb = imagecreatetruecolor($width_thumb, $height_thumb); $source = imagecreatefromjpeg($upload_thumb_path.$filename); if(imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width_thumb, $height_thumb, $width, $height)) { echo 'Thumbnail successfully resized'; } else { echo 'Thumbnail resize process hit a wall!'; } } else { echo 'Thumbnail hit a wall!'; } } else echo 'There was an error during the file upload. Please try again.'; Quote Link to comment Share on other sites More sharing options...
Barand Posted October 12, 2007 Share Posted October 12, 2007 I don't see your code saving the resized thumb image Quote Link to comment Share on other sites More sharing options...
ukweb Posted October 12, 2007 Author Share Posted October 12, 2007 Urika! That's it! Its working now! here's the working code: // Configuration - Your Options $allowed_filetypes = array('.jpg','.gif','.bmp','.png'); $max_filesize = 524288; $upload_path = 'user_files/uploaded/images/uploaded/'; $upload_thumb_path = $upload_path.'thumbs/'; $filename = $_FILES['userfile']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) { echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked. if(copy($upload_path.$filename, $upload_thumb_path.$filename)) { echo 'Thumbnail file created successfully'.$filename; list($width, $height) = getimagesize($upload_path.$filename); $width_thumb = 100; $ratio = ($width_thumb / $width) * 100; $height_thumb = ($ratio * $height) / 100; $thumb = imagecreatetruecolor($width_thumb, $height_thumb); $source = imagecreatefromjpeg($upload_thumb_path.$filename); if(imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width_thumb, $height_thumb, $width, $height)) { imagejpeg($thumb, $upload_thumb_path.$filename, 100); echo 'Thumbnail successfully resized'; } else { echo 'Thumbnail resize process hit a wall!'; } } else { echo 'Thumbnail hit a wall!'; } } else echo 'There was an error during the file upload. Please try again.'; Quote Link to comment Share on other sites More sharing options...
ukweb Posted October 12, 2007 Author Share Posted October 12, 2007 Thanks for the help, Barand, much appreciated! Quote Link to comment Share on other sites More sharing options...
theredking Posted June 3, 2008 Share Posted June 3, 2008 Hi, I've been trying to do something very similar to this myself, and ended up just using this script because it was almost exactly what I needed. I am running into a couple of strange problems though, and I'm not exactly sure what is causing them. I am only attempting to upload jpeg images, and some images upload fine, but others result in the "There was an error during the file upload. Please try again." error. I cannot work out what it is about these images that make this happen. I'm pretty sure that if they are CMYK then that results in the error, but I have uploaded RGB images that have given the same error. I'm a little stuck. Here is my code, just slightly altered $allowed_filetypes = array('.jpg','.JPG','.gif','.bmp','.png'); $max_filesize = 33554432; $upload_path = '/home/myserver/www/uploads/'; $upload_thumb_path = $upload_path.'thumbs/'; $filename = $_FILES['userfile']['name']; $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); $filename = "{$_POST['last_name']}"."_"."{$_POST['first_name']}"."_".time() . "$ext"; if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) { print '<p>Your file has been successully uploaded, <a href="/uploads/' . $filename . '">click here</a> to view your file.</p>'; // It worked. if(copy($upload_path.$filename, $upload_thumb_path.$filename)) { print '<p>Thumbnail file created successfully. '; list($width, $height) = getimagesize($upload_path.$filename); $width_thumb = 160; $height_thumb = 200; $thumb = imagecreatetruecolor($width_thumb, $height_thumb); $source = imagecreatefromjpeg($upload_thumb_path.$filename); if(imagecopyresampled($thumb, $source, 0, 0, 0, 0, $width_thumb, $height_thumb, $width, $height)) { imagejpeg($thumb, $upload_thumb_path.$filename, 100); print 'Thumbnail successfully resized, <a href="/uploads/thumbs/' . $filename . '">click here</a> to view your file.</p>'; } else { print '<p>Thumbnail resize process hit a wall!</p>'; } } else { print '<p>Thumbnail hit a wall!</p>'; } } else print '<p>There was an error during the file upload. Please try again.</p>'; Any suggestions would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.