fife Posted December 22, 2011 Share Posted December 22, 2011 OK. I have tried to make my own image upload script which checks if the image is bigger the 500px in height. If it is shrinks the image down then uploads it an saves it to a database. I got the upload an save to database working fine. Once I added the check size bits it just fails with no error. Does anybody know of any simple scripts or tuts out there that do this as Im fed up of banging my head against the wall. Ive tried to find one myself but they dont exist it would seem!! heres my code anyway just incase its fixable.. I get no error an no redirect, I dont think its even making it to the move upload file as its not appearing on my server <?php if(isset($_POST['submit'])){ $fileName9 = trim($_FILES['imagefile']['name']); $tmpName = $_FILES['imagefile']['tmp_name']; $fileType = $_FILES['imagefile']['type']; //re name the image $randName = md5(rand() * time()); $fileName = $randName.$fileName9; //where to save the image $folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/{$members['county']}/"; //check types $types = array('image/jpeg', 'image/gif', 'image/png', 'image/JPG', 'image/pjpeg', 'image/x-png'); if (in_array($fileType, $types)) { //get original width and height $size = @GetImageSize($fileName); $width = $size[0]; $height = $size[1]; // auto adjust width of image according to predetermined height $new_height = 500; $new_width = $width * ($new_height/$height); // Resample image $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $fileName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // New image $image = $image_resized; } // Your file handing script here if(move_uploaded_file($tmpName , $folder.$image)) { $qInsert = mysql_query("UPDATE `members` SET profilePic = '$image' WHERE userID = '".$members['userID']."'") or die (mysql_error()); } if ($qInsert){ $url = "/members/clubs/image.php?pic=$image"; header("Location: $url"); } } ?> <form action="" method="post" enctype="multipart/form-data" name="photo" id="photo"> <input name="imagefile" type="file"> <input name="submit" type="submit" id="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/ Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 Turn on errors. At the top of your script, error_reporting(-1); ini_set('display_errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300484 Share on other sites More sharing options...
fife Posted December 22, 2011 Author Share Posted December 22, 2011 ok here is the new script with my error from the error reporting. I dont understand any of them if(isset($_POST['submit'])){ $fileName = $_FILES['imagefile']['name']; $tmpName = $_FILES['imagefile']['tmp_name']; //where to save the image $folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/"; //get original width and height $size = @GetImageSize($fileName); $width = $size[0]; $height = $size[1]; // auto adjust width of image according to predetermined height $new_height = 500; $new_width = $width * ($new_height/$height); // Resample image $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $fileName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // New image $image = $image_resized; my errors are Division by zero in /home/sites/image.php on line 264 which is this line......... $new_width = $width * ($new_height/$height); Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/sites/image.php on line 267 which is this line....... $image_resized = imagecreatetruecolor($new_width, $new_height); finally Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/sites/image.php on line 268 which is this line....... imagecopyresampled($image_resized, $fileName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300489 Share on other sites More sharing options...
PFMaBiSmAd Posted December 22, 2011 Share Posted December 22, 2011 The @ error suppressor you have in your code is hiding errors that would help pin point what is wrong. Remove the @ in front of the GetImageSize statement. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300493 Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 Don't put @ on getimagesize(), and you would see another error. You are giving getimagesize() the image name, like, "image.jpg" but you aren't actually giving it the image - just a string containing its name. So you need to give it the tmp_name instead. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300497 Share on other sites More sharing options...
fife Posted December 22, 2011 Author Share Posted December 22, 2011 ok thanks guys made those changes and left with one error. Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/sites/image.php on line 268 which is now this line..... imagecopyresampled($image_resized, $tmpName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); Here is the ammended code. <?php error_reporting(-1); ini_set('display_errors', 1); if(isset($_POST['submit'])){ $fileName = $_FILES['imagefile']['name']; $tmpName = $_FILES['imagefile']['tmp_name']; //where to save the image $folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/"; //get original width and height $size = GetImageSize($tmpName); $width = $size[0]; $height = $size[1]; // auto adjust width of image according to predetermined height $new_height = 500; $new_width = $width * ($new_height/$height); // Resample image $image_resized = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($image_resized, $tmpName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // New image $tmpName= $image_resized; $move = move_uploaded_file($tmpName, $folder.$fileName); if($move) { $url = "/members/clubs/image.php?pic=$image"; header("Location: $url"); } } ?> Ive tried changing imagecopyresampled($image_resized, $tmpName, 0, 0, 0, 0, $new_width, $new_height, $width, $height); to $fileName but that didnt work either. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300505 Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 Nothing is sticking out. Make sure that $new_width and $new_height actually contain values, and make sure $image_resized is actually a resource. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300506 Share on other sites More sharing options...
PFMaBiSmAd Posted December 22, 2011 Share Posted December 22, 2011 The second parameter must be an GD image resource. Here is a link to the imagecopyresampled documentation. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300512 Share on other sites More sharing options...
fife Posted December 22, 2011 Author Share Posted December 22, 2011 Ok i have read that info but I really dont know what you mean by GD image resource. That info goes way over my head. Can someone explain please? Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300519 Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 Do $image = imagecreatefromjpeg($tmpName); If your image isn't a jpeg, then use one of the other imagecreatefrom functions Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300523 Share on other sites More sharing options...
fife Posted December 22, 2011 Author Share Posted December 22, 2011 ok added that line an all my errors have started coming back now. error_reporting(-1); ini_set('display_errors', 1); if(isset($_POST['submit'])){ $fileName = $_FILES['imagefile']['name']; $tmpName = $_FILES['imagefile']['tmp_name']; //where to save the image $folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/"; //get original width and height $size = GetImageSize($tmpName); $width = $size[0]; $height = $size[1]; // auto adjust width of image according to predetermined height $new_height = 500; $new_width = $width * ($new_height/$height); // Resample image $image_resized = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($tmpName); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //// New image $tmpName = $image_resized; $move = move_uploaded_file($tmpName , $folder.$fileName); if($move) { $url = "/members/clubs/image.php?pic=$image"; header("Location: $url"); } } my error list from entering that line. Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home/sites/image.php on line 255 Warning: Division by zero in /home/sites//image.php on line 261 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/sites/image.php on line 264 Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: Filename cannot be empty in /home/sites/image.php on line 266 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/sites/image.php on line 268 where as before there was only the other error. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300526 Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 getimagesize() thinks $tmpName is empty, so that makes me believe it didn't upload properly this time around. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300527 Share on other sites More sharing options...
fife Posted December 22, 2011 Author Share Posted December 22, 2011 Yes I agree but as soon as I remove the createimagefromjpeg function that error goes an I jus get the not a valid resource error. Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300531 Share on other sites More sharing options...
PFMaBiSmAd Posted December 23, 2011 Share Posted December 23, 2011 Your latest error(s) indicate that the upload failed (probably because the file was too large or you didn't select a file.) See this link for possible upload errors - http://us3.php.net/manual/en/features.file-upload.errors.php At a minimum, your code needs to check that $_FILES isset (exceeding the post_max_size setting will cause the $_FILES array and the $_POST array to be empty and errors in your form or uploads not enabled on the server will also cause the $_FILES array to be empty) and that $_FILES['imagefile']['error'] is equal to zero before you attempt to use any of the the uploaded file information in your logic. if(isset($_FILES) && $_FILES['imagefile']['error']==UPLOAD_ERR_OK){ // the upload worked, use the uploaded file information here... } else { // the upload failed due to an error, handle that condition here - output a user message indicating why the upload failed (some of them the user can correct by uploading a smaller file.) } Also, in the case of an upload form, since the $_POST array can be empty for one of the possible error conditions, you should test if the form has been submitted by using - if($_SERVER['REQUEST_METHOD']=='POST'){ Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300814 Share on other sites More sharing options...
fife Posted December 23, 2011 Author Share Posted December 23, 2011 ok thanks guys Ive done everything that has been said and addapted my code accordingly. Thanks for the help. Unfortunately however the upload is still not working. I have no errors with error reporting switched on, when I upload an image the page just refreshes and no image is on the server. Am I even doing this correctly. Im trying to save a resized image to the server (for obvious space saving reasons) I will then store its name in the database. Ive googled every possible solution but none seem to do what I need. Here is the code as it stands. I cant see any errors myself and none output but no file gets uploaded to the server or anything. <?php error_reporting(-1); ini_set('display_errors', 1); if($_SERVER['REQUEST_METHOD']=='POST'){ if(isset($_FILES) && $_FILES['file']['error']==UPLOAD_ERR_OK){ // the upload worked, use the uploaded file information here... $fileName = $_FILES['file']['name']; $tmpName = $_FILES['file']['tmp_name']; //where to save the image $folder = "{$_SERVER['DOCUMENT_ROOT']}/members/images/Merseyside/"; //get original width and height $size = GetImageSize($tmpName); $width = $size[0]; $height = $size[1]; // auto adjust width of image according to predetermined height $new_height = 500; $new_width = $width * ($new_height/$height); // Resample image $image_resized = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($tmpName); imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); //// New image $tmpName = $image_resized; $move = move_uploaded_file($tmpName , $folder.$fileName); if($move) { $url = "/members/clubs/image.php?pic=$image"; header("Location: $url"); } } else { echo "Error"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1300846 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2011 Share Posted December 24, 2011 when I upload an image the page just refreshes What page just refreshes? We need to know exactly what occurs in front of you in order to help. For all we know there's something you are doing in your form that is causing the problem or that your header() statement in the code you posted is redirecting to the page in question or that you have opened the form directly in your browser through the file system and not through a URL on your web server. Edit: I just noticed that you changed the name of the uploaded file field from 'imagefile' to 'file'. Are you sure that is correct? Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1301178 Share on other sites More sharing options...
PFMaBiSmAd Posted December 25, 2011 Share Posted December 25, 2011 I tried your posted code using a valid and matching upload form and you will get an error at the move_uploaded_file statement because you have set the first parameter to be a GD image resource. move_uploaded_file expects the first parameter to be the original tmp uploaded file (kind of why they named it move_uploaded_file.) If your intent is to save the resized image in $image_resized to the $folder.$fileName location, you would need to use an appropriate GD output function - imagejpeg($image_resized, $folder.$fileName); Quote Link to comment https://forums.phpfreaks.com/topic/253677-image-upload-issues/#findComment-1301252 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.