andrew_biggart Posted October 26, 2010 Share Posted October 26, 2010 Im trying to write an image upload script which checks various elements of the image and then saves it and and a thumbnail into a folder. At the moment I am getting a white screen which makes me think I have a syntax error so I have come here to find some fresh eyes to help me out. Can anyone see any reason why this wouldn't be working? Form <form enctype='multipart/form-data' action='upload_image.php' method='POST'> <table class='uploads-form'> <tr> <td><input name='image' type='file' /><input type='hidden' name'MAX_FILE_SIZE' value='5000000' /></td> </tr> <tr> <td><input type='submit' name='upload_image' value='Upload' /></td> </tr> </table> </form> php <?php error_reporting(E_ALL); ini_set("display_errors", 1); $images_location = "project_files/"; $thumbs_location = "project_thumbs/"; $thumb_width = "100"; $maximum_size = "5000000"; //Auto Thumbnail Function function create_thumbnail($source,$destination, $thumb_width) { $size = getimagesize($source); $width = $size[0]; $height = $size[1]; $x = 0; $y = 0; if($width> $height) { $x = ceil(($width - $height) / 2 ); $width = $height; } elseif($height> $width) { $y = ceil(($height - $width) / 2); $height = $width; } $new_image = imagecreatetruecolor($thumb_width,$thumb_width) or die('Cannot initialize new GD image stream'); $extension = get_image_extension($source); if($extension=='jpg' || $extension=='jpeg') $image = imagecreatefromjpeg($source); if($extension=='gif') $image = imagecreatefromjpeg($source); if($extension=='png') $image = imagecreatefromjpeg($source); imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height); if($extension=='jpg' || $extension=='jpeg') imagejpeg($new_image,$destination); if($extension=='gif') $imagegif($new_image,$destination); if($extension=='png') imagepng($new_image,$destination); } //Get Extension Function function get_image_extension($name) { $name = strtolower($name); $i = strrpos($name,"."); if (!$i) { return ""; } $l = strlen($name) - $i; $extension = substr($name,$i+1,$l); return $extension; } //Random Name Function function random_name($length) { $characters = "abcdefghijklmnopqrstuvwxyz01234567890"; $name = ""; for ($i = 0; $i < $length; $i++) { $name .= $character[mt_rand(0, strlen($characters) - 1)]; } return "image-".$name; } //Check And Save Image if(isset($_POST['upload_image'])) { if($_FILES['image']['name'] == ""){ echo = "Please select the image you would like to upload by clicking browse"; } else{ $size=filesize($_FILES['image']['tap_name']); $filename = stripslashes($_FILES['image']['name']); $extension = get_image_extension($filename); if($size > $maximum_size) { echo = "Your file size exceeds the maximum file size!"; } else if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { echo = "Only the following extensions are allowed. 'jpg', 'jpeg', 'png', 'gif'."; } else { $image_random_name=random_name(15).".".$extension; $copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name); if (!$copy) { echo = "Error while uploadin your image! Please try again!"; } else{ create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumb_width); echo = "Your image has been uploaded!"; } } } } ?> Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 26, 2010 Author Share Posted October 26, 2010 Hmm anyone? I cant seem to spot anything wrong at all! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 this is invalid everywhere it occurs. echo = Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 26, 2010 Share Posted October 26, 2010 And you should have the error_reporting/display_errors settings turned on in your master php.ini or in a local php.ini or in your .htaccess file (the exact method that works is dependent on how php is running in your web server) so that the fatal parse errors will be reported and displayed. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 26, 2010 Author Share Posted October 26, 2010 whops I have changed that back to $results now, but its still not saving the images or displaying any errors grrr! Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 And you should have the error_reporting/display_errors settings turned on in your master php.ini or in a local php.ini or in your .htaccess file (the exact method that works is dependent on how php is running in your web server) so that the fatal parse errors will be reported and displayed. yes. if I had to "guess" if there are parse errors, i would immediately look into a different career. :-) Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 26, 2010 Author Share Posted October 26, 2010 Lol Mcdonalds it is! Im sure il solve it tomorrow my eyes are to tired to make any sense of it now to be honest! But thanks for your help! and if you spot anything please do let me know guys thanks! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 26, 2010 Share Posted October 26, 2010 So, if you select no image and submit the form, what do you get? You should get your "Please select the image you would like to upload by clicking browse" message. That would be your first test case. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 When I dont add an image I am getting a blank screen as well. This makes me think there is a problem with one of the functions I have written but I cannot figure out what the problem is at all. The script is not running what so ever. Can anyone see something I cant? Quote Link to comment Share on other sites More sharing options...
jimmyt1988 Posted October 28, 2010 Share Posted October 28, 2010 Lol Mcdonalds it is! hmm... Free burgers. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 On a serious note jimmy can you help me? Im stressing out! Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 Ok if no one is able to help me resolve my script that I have tried to write can anyone point me in the right direction towards any good tutorials to upload an image and automatically create a thumbnail please? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 28, 2010 Share Posted October 28, 2010 Without seeing your current code that produces the current symptom, no one can directly help with what it is doing. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 The code is at the top of the page! Both html and php! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 28, 2010 Share Posted October 28, 2010 No it's not. You have edited it since then to correct the syntax errors that were present due to the echo = statements. Who knows what else you have altered in it or even what you changed the echo = statements too. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 Ok I have re written a new script but im having trouble with it as well. It will upload the image but once it gets to 100% and loads again I am getting the blank screen. I think the issue is when it tries to save the image. My database has the following columns. image_id, file_url, thumb_url, project_name, project_text, project_type. My folders are called project_files and project_thumbs. This is the php : <?php $project=$_GET['project']; // Check project is selected if($project != '') { //Check a form has been submitted if(isset($_POST['upload_image'])) { // Fetch the image array sent by preupload.php $photos_uploaded = $_FILES['image']; // Fetch the image caption array $photo_captions = $_POST['caption']; // File type array $photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); while($counter <= count($photos_uploaded)) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $photo_types)) { $result_final .= 'File ' . ($counter + 1) . ' is not a photo<br />'; } else { // Save the image details to db $sql="INSERT INTO project_images (file_url, project_name, project_text, project_type) VALUES ('$photos_uploaded', '$project', '$photo_captions', 'image')"; $result=mysql_query($sql); $new_id = mysql_insert_id(image_id); // New Id generated // Get the filetype of the uploaded file $filetype = $photos_uploaded['type'][$counter]; // Get the extension for the new name $extension = $known_photo_types[$filetype]; // Generate a new name $filename = "$new_id.$extension"; // Update information $sql2="UPDATE project_images SET file_url = '$filename' WHERE image_id='$new_id'"; $result2=mysql_query($sql2); move_uploaded_file($photos_uploaded['tmp_name'][$counter], $images_dir . '/project_files/' . $filename); //Auto thumbnail start $size = GetImageSize($images_dir . "/project_files/" . $filename); // Wide Image if($size[0] > $size[1]) { $thumbnail_width = 100; $thumbnail_height = (int)(100 * $size[1] / $size[0]); } // Tall Image else { $thumbnail_width = (int)(100 * $size[0] / $size[1]); $thumbnail_height = 100; } $thumbnail_width = <Preset Width> $thumbnail_height = <Preset Width> * <height_dimension_of_original_image> / <width_dimension_of_original_image> $thumbnail_width = <Preset Height> * <width_dimension_of_original_image> / <height_dimension_of_original_image> $thumbnail_height = <Preset Height> $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = 'ImageCreateFrom' . $function_suffix; $function_to_write = 'Image' . $function_suffix; // Read the source file $source_handle = $function_to_read($images_dir . '/project_files/' . $filename ); if ($source_handle) { // Let's create a blank image for the thumbnail $destination_handle = ImageCreate($thumbnail_width, $thumbnail_height); // Now we resize it ImageCopyResized($destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1]); } // Let's save the thumbnail $function_to_write($destination_handle, $images_dir . '/project_thumbs/' . $filename); ImageDestroy($destination_handle); // Update information $sql3="UPDATE project_thumbs SET thumb_url = '$filename' WHERE image_id='$new_id'"; $result3=mysql_query($sql3); if($result3){ echo"Your image has been uploaded successfully!"; } else { echo "Your image has not been uploaded please try again!"; } } } } }// End check form submit } else { echo"Please select a project!"; } ?> Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 Sorry my bad. <?php error_reporting(E_ALL); ini_set("display_errors", 1); $images_location = "project_files/"; $thumbs_location = "project_thumbs/"; $thumb_width = "100"; $maximum_size = "5000000"; //Auto Thumbnail Function function create_thumbnail($source,$destination, $thumb_width) { $size = getimagesize($source); $width = $size[0]; $height = $size[1]; $x = 0; $y = 0; if($width> $height) { $x = ceil(($width - $height) / 2 ); $width = $height; } elseif($height> $width) { $y = ceil(($height - $width) / 2); $height = $width; } $new_image = imagecreatetruecolor($thumb_width,$thumb_width) or die('Cannot initialize new GD image stream'); $extension = get_image_extension($source); if($extension=='jpg' || $extension=='jpeg') $image = imagecreatefromjpeg($source); if($extension=='gif') $image = imagecreatefromjpeg($source); if($extension=='png') $image = imagecreatefromjpeg($source); imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height); if($extension=='jpg' || $extension=='jpeg') imagejpeg($new_image,$destination); if($extension=='gif') $imagegif($new_image,$destination); if($extension=='png') imagepng($new_image,$destination); } //Get Extension Function function get_image_extension($name) { $name = strtolower($name); $i = strrpos($name,"."); if (!$i) { return ""; } $l = strlen($name) - $i; $extension = substr($name,$i+1,$l); return $extension; } //Random Name Function function random_name($length) { $characters = "abcdefghijklmnopqrstuvwxyz01234567890"; $name = ""; for ($i = 0; $i < $length; $i++) { $name .= $character[mt_rand(0, strlen($characters) - 1)]; } return "image-".$name; } //Check And Save Image if(isset($_POST['upload_image'])) { if($_FILES['image']['name'] == ""){ echo = "Please select the image you would like to upload by clicking browse"; } else{ $size=filesize($_FILES['image']['tap_name']); $filename = stripslashes($_FILES['image']['name']); $extension = get_image_extension($filename); if($size > $maximum_size) { $result = "Your file size exceeds the maximum file size!"; } else if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) { $result = "Only the following extensions are allowed. 'jpg', 'jpeg', 'png', 'gif'."; } else { $image_random_name=random_name(15).".".$extension; $copy = @copy($_FILES['image']['tmp_name'], $images_location.$image_random_name); if (!$copy) { $result = "Error while uploadin your image! Please try again!"; } else{ create_thumbnail($images_location.$image_random_name,$thumbs_location.$image_random_name, $thumb_width); $result = "Your image has been uploaded!"; } } } } ?> I am also trying out another tutorial as well which isnt going to well so if this looks redundant let me know and I will post the latest script. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 28, 2010 Share Posted October 28, 2010 You never do anything with the error messages, so how would you expect your code to display anything? Computers only do exactly what their programs tell them to do. If you set a variable but don't do anything with it in your logic, you see no results. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 So where would I echo the $result ? Thanks again for your help. Quote Link to comment Share on other sites More sharing options...
andrew_biggart Posted October 28, 2010 Author Share Posted October 28, 2010 Anyone? 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.