andrew_biggart Posted July 18, 2012 Share Posted July 18, 2012 Apologies for another thread about this, but i have completely re-written my previous code so it's no longer relevant to my previous post, hence why i am creating a new one. I am trying to create an image upload script which resizes the uploaded image to a set parameter and then saves it to an upload folder as well as the database. At the minute the image is being saved but not resized can anyone see what I am doing wrong?. <?php $upload_dir = 'uploads/'; $thumbs_dir = 'thumbs/'; $max_width = 570; // Check if the upload and thumbs directories exist. // If they don't then we create them. if(!file_exists($upload_dir)) { if(mkdir($upload_dir)) { if(!file_exists($upload_dir . $thumbs_dir)) { if(mkdir($upload_dir . $thumbs_dir)) { } else { exit_status($upload_dir . 'directory could not be created! Please check your folder permissions.'); } } } else { exit_status($upload_dir . 'directory could not be created! Please check your folder permissions.'); } } $allowed_ext = array('jpg','jpeg', 'png','gif'); $unique_id = date("YmdHis"); $unique_id = $unique_id . rand(0,999999999); if(isset($_POST['upload_file'])){ $file_tmp = $_FILES['pic']['tmp_name']; $file_name = $_FILES['pic']['name']; $file_size = $_FILES['pic']['size']; $file_ext = get_extension($_FILES['pic']['name']); $file_new = $unique_id . "." . $file_ext; $createdby = "Andrew"; if(in_array(get_extension($file_name),$allowed_ext)){ if($file_size < 2000000) { // Check the width of the uploaded image list($file_width,$file_height) = getimagesize($file_tmp); if($file_width > $max_width) { if($file_ext == "jpg" || $file_ext == "jpeg" ){ $src = imagecreatefromjpeg($file_tmp); } else if($file_ext == "png"){ $src = imagecreatefrompng($file_tmp); } else if($file_ext == "gif"){ $src = imagecreatefromgif($file_tmp); } $file_height_new = ($height/$width) * $max_width; $tmp = imagecreatetruecolor($max_width,$file_height_new); imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height); imagejpeg($tmp,$upload_dir . $file_new,100); if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){ connect(); $link = get_option('admin_url'); $sql = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) "; $result = mysql_query($sql); if($result) { echo "Success! <i>$file_name has been added to your media library!</i>"; } else { echo "Error! <i>File couldn't be saved to the database.</i>"; } } else { echo "Error! <i>File couldn't be saved to $upload_dir.</i>"; } imagedestroy($src); imagedestroy($tmp); } else { if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){ connect(); $link = get_option('admin_url'); $sql = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) "; $result = mysql_query($sql); if($result) { echo "Success! <i>$file_name has been added to your media library!</i>"; } else { echo "Error! <i>File couldn't be saved to the database.</i>"; } } else { echo "Error! <i>File couldn't be saved to $upload_dir.</i>"; } } } else { echo "Error! <i>Filesize is to large. Please upload a file under 2MB.</i>"; } } else { echo "Error! <i>Only the following file types are allowed ".implode(',',$allowed_ext).".</i>"; } } else { echo "Select a file to upload.<br /> <i>(With a maximum filesize of 2MB)</i>"; } function get_extension($file_name){ $ext = explode('.', $file_name); $ext = array_pop($ext); return strtolower($ext); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/ Share on other sites More sharing options...
litebearer Posted July 18, 2012 Share Posted July 18, 2012 A quick look seems to indicate the problem can be found in these four lines... $tmp = imagecreatetruecolor($max_width,$file_height_new); imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height); imagejpeg($tmp,$upload_dir . $file_new,100); if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){ it appears that you are saving the resized image, then overwriting it with the original. Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362464 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 Apologies, I had tried to modify the code as from my orginal which is posted below. I am still getting the same issue with that as well tho. The image is being saved in it's orginal state, and not being resized. I'd imagine it should be something quite simple, but I cannot for the life of me spot what the issue is. <?php ini_set(?display_errors?,1); error_reporting(E_ALL); $upload_dir = 'uploads/'; $max_width = 570; // Check if the upload and thumbs directories exist. // If they don't then we create them. if(!file_exists($upload_dir)) { if(mkdir($upload_dir)) { } else { exit_status($upload_dir . 'directory could not be created! Please check your folder permissions.'); } } $allowed_ext = array('jpg','jpeg', 'png','gif'); $unique_id = date("YmdHis"); $unique_id = $unique_id . rand(0,999999999); if(isset($_POST['upload_file'])){ $file_tmp = $_FILES['pic']['tmp_name']; $file_name = $_FILES['pic']['name']; $file_size = $_FILES['pic']['size']; $file_ext = get_extension($_FILES['pic']['name']); $file_new = $unique_id . "." . $file_ext; $createdby = $_SESSION["ufullname"]; if(in_array(get_extension($file_name),$allowed_ext)){ if($file_size < 2000000) { // Check the width of the uploaded image list($file_width,$file_height) = getimagesize($file_tmp); if($file_width > $max_width) { if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){ connect(); $link = get_option('admin_url'); $sql = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) "; $result = mysql_query($sql); if($result) { if($file_ext == "jpg" || $file_ext == "jpeg" ){ $src = imagecreatefromjpeg($file_tmp); } else if($file_ext == "png"){ $src = imagecreatefrompng($file_tmp); } else if($file_ext == "gif"){ $src = imagecreatefromgif($file_tmp); } $file_height_new = ($height/$width) * $max_width; $tmp = imagecreatetruecolor($max_width,$file_height_new); imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height); imagejpeg($tmp,$upload_dir . $file_new,100); echo "Success! <i>$file_name has been added to your media library!</i>"; imagedestroy($src); imagedestroy($tmp); } else { echo "Error! <i>File couldn't be saved to the database.</i>"; } } else { echo "Error! <i>File couldn't be saved to $upload_dir.</i>"; } } else { if(move_uploaded_file($file_tmp, $upload_dir . $file_new)){ connect(); $link = get_option('admin_url'); $sql = " INSERT into mediaT ( name, url, link, createdon, createdby ) VALUES ( '$file_name', '$upload_dir$file_new', '$link$upload_dir$file_new', NOW(), '$createdby' ) "; $result = mysql_query($sql); if($result) { echo "Success! <i>$file_name has been added to your media library!</i>"; } else { echo "Error! <i>File couldn't be saved to the database.</i>"; } } else { echo "Error! <i>File couldn't be saved to $upload_dir.</i>"; } } } else { echo "Error! <i>Filesize is to large. Please upload a file under 2MB.</i>"; } } else { echo "Error! <i>Only the following file types are allowed ".implode(',',$allowed_ext).".</i>"; } } else { echo "Select a file to upload.<br /> <i>(With a maximum filesize of 2MB)</i>"; } function get_extension($file_name){ $ext = explode('.', $file_name); $ext = array_pop($ext); return strtolower($ext); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362653 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 I have also tried creating a separate directory called thumbs to store the resized images by changing the following: imagejpeg($tmp,$upload_dir . $file_new,100); Which I changed to imagejpeg($tmp,"uploads/thumbs/" . $file_new,100); The resized images are not getting saved into the directory so therefor I'm assuming the problem is to do with the resizing part of the code but I can figure out what. From what I can see I am currently resizing the image using the following lines of code. imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height); I understand that I will need to create if statements based on the extension to recreate either jpgs, gifs and pngs using imagejpeg, imagegif and imagepng. But I have left them out for now until I get the resizing part working. While I am testing this current script i am only using jpgs. Can anyone spot something I ma missing? Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362657 Share on other sites More sharing options...
lordshoa Posted July 19, 2012 Share Posted July 19, 2012 Sorry not what i thought it was Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362665 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 Surely what I'm calling the variable for imagecreatetruecolor should matter as long as i continue to use that variable? Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362667 Share on other sites More sharing options...
lordshoa Posted July 19, 2012 Share Posted July 19, 2012 Yes I was wrong I was not looking at it properly Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362670 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 Did you happen to notice anything else that stands out? It's always good to get some fresh eyes on your code. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362675 Share on other sites More sharing options...
lordshoa Posted July 19, 2012 Share Posted July 19, 2012 I was looking at this at first what your saying is your max width is 570 but lets say the image is 600 x 200 so 600/200 = 3 x 570 = 1710 = $file_height_new Would this be a problem. $file_height_new = ($height/$width) * $max_width; Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362678 Share on other sites More sharing options...
lordshoa Posted July 19, 2012 Share Posted July 19, 2012 Sorry I am thiking the wrong way I am thinking your trying to make thumbs but your making bigger or smaller images Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362681 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 That is a very valid point lordshoa, thank you for pointing that out. What method would you suggest that I use then? I also made sure that this wasn't causing the original issue of the image not being resized by changing the $file_height_new variable to a whole number. I am not trying to make images bigger, I only want to resize the image if it's width is larger than 570px. The reason for this is so that all uploaded images are in keeping with the websites design. Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362690 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 Ok so I have found what the problem was that was causing the image not to resize properly. I had been defining the width and height like this. list($file_width,$file_height) = getimagesize($file_tmp); But in my following lines of code I was using the variables $width and $height instead of $file_width and $file_height. I changed the following three lines of code as follows: $file_height_new = ($height/$width) * $max_width; $tmp = imagecreatetruecolor($max_width,$file_height_new); imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$width,$height); to this $file_height_new = ($file_height/$file_width) * $max_width; $tmp = imagecreatetruecolor($max_width,$file_height_new); imagecopyresampled($tmp,$src,0,0,0,0,$max_width,$file_height_new,$file_width,$file_height); This has now resolved the resizing issue, however I have now ran into the same problem I had with my orginal script. The problem is that the thumbnail image that is being created is completely black. I don't know why but the original image isn't be copied properly. Does anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362707 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 I have also tried using both imagecopyresized and imagecopyresampled. They both create a completely black thumbnail. Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362709 Share on other sites More sharing options...
PFMaBiSmAd Posted July 19, 2012 Share Posted July 19, 2012 Has your code been producing any php error messages? I ask for two reasons, it should have been, over the $height/$width variables that don't exist and your ini_set(?display_errors?,1); code is displaying with ? marks instead of straight single or double quotes and is probably not actually having an effect. Try retyping that ini_set statement using straight quotes and see if you get any php error messages. Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362710 Share on other sites More sharing options...
andrew_biggart Posted July 19, 2012 Author Share Posted July 19, 2012 Firstly PFMaBiSmAd, YOU ARE MY HERO! I can't believe that I hadn't noticed that before now. My error reporting had those weird american quotation marks. Once I had fixed this I was able to figure out that because I had moved the temporary file to the uploads directory I was longer able to use it in the imagecreatefromjpeg part of my script. The error I was getting was a failure to open stream because it was having trouble finding the image because it no longer existed. I changed the following lines of code: if($file_ext == "jpg" || $file_ext == "jpeg" ){ $src = imagecreatefromjpeg($file_tmp); } else if($file_ext == "png"){ $src = imagecreatefrompng($file_tmp); } else if($file_ext == "gif"){ $src = imagecreatefromgif($file_tmp); } to the following. if($file_ext == "jpg" || $file_ext == "jpeg" ){ $src = imagecreatefromjpeg($upload_dir . $file_new); } else if($file_ext == "png"){ $src = imagecreatefrompng($upload_dir . $file_new); } else if($file_ext == "gif"){ $src = imagecreatefromgif($upload_dir . $file_new); } The resized image is now being created properly. Thanks for everyones help. Quote Link to comment https://forums.phpfreaks.com/topic/265907-image-upload-and-resize-issues/#findComment-1362719 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.