bravo14 Posted April 17, 2012 Share Posted April 17, 2012 Hi I am trying to resize images as they are uploaded. The problem I have is that the image is uploading and adding to the database, but is not resizing. The code I am using is below <?php include("config.inc.php"); include('../includes/connect_inc.php'); // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); // Fetch the photo array sent by add-photos.php $photos_uploaded = $_FILES['photo_filename']; // Fetch the photo caption array $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); //resize photos to maximum height or width //define parameters - maximum height & width for new images $image_max_width=599; $image_max_height=599; $max_dimension=800; // Grab the width and height of the image. list($width,$height) = getimagesize($_FILES['photo_filename']); // If the max width input is greater than max height we base the new image off of that, otherwise we // use the max height input. // We get the other dimension by multiplying the quotient of the new width or height divided by // the old width or height. if($image_max_width > $image_max_height){ if($image_max_width > $max_dimension){ $newwidth = $max_dimension; } else { $newwidth = $image_max_width; } $newheight = ($newwidth / $width) * $height; } else { if($image_max_height > $max_dimension){ $newheight = $max_dimension; } else { $newheight = $image_max_height; } $newwidth = ($newheight / $height) * $width; } // Create temporary image file. $tmp = imagecreatetruecolor($newwidth,$newheight); // Copy the image to one with the new width and height. imagecopyresampled($tmp,$image,0,0,0,0,$newwidth,$newheight,$width,$height); //end of resizing // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $thumbnail_width = 100; $thumbnail_height = (int)(100 * $size[1] / $size[0]); } else { $thumbnail_width = (int)(100 * $size[0] / $size[1]); $thumbnail_height = 100; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $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."/".$filename ); if($source_handle) { // Let's create an 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."/tb_".$filename ); ImageDestroy($destination_handle ); // $result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />"; } } $counter++; } header('Location: add-photos.php'); // Print Result echo <<<__HTML_END <html> <head> <title>Photos uploaded</title> </head> <body> $result_final </body> </html> __HTML_END; ?> Any help would be appreciated. Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/ Share on other sites More sharing options...
creata.physics Posted April 17, 2012 Share Posted April 17, 2012 What errors do you get? If you don't get any, can you add: error_reporting( E_ALL ); to the top of the page and give us any errors that occur? Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338113 Share on other sites More sharing options...
bravo14 Posted April 17, 2012 Author Share Posted April 17, 2012 Hi I have added the error_reporting( E_ALL ); And I am still not getting any errors displayed, it is as if the code is being ignored Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338123 Share on other sites More sharing options...
Psycho Posted April 17, 2012 Share Posted April 17, 2012 You really need to re-think your logic. You are adding the image to the database before you even save it to the server. In fact. You do an insert and then follow it up with an update on that record of information you could have already had when you did the insert. Plus, you are doing things that have no purpose $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); Why are you using addslashes() on the variable $new_id? It is an integer returned from the database. In fact, why are you using addslashes() as all?! You should be using mysql_real_escape_string(). The more I read of that code the more I see wrong. while( $counter <= count($photos_uploaded) ) { You have an array. Why are you using a while() loop with an artificial counter instead of a foreach() loop/ list($width,$height) = getimagesize($_FILES['photo_filename']); Why are you referencing $_FILES['photo_filename'] for getimagesize() since that value is an array? You are in a loop list($width,$height) = getimagesize($_FILES['photo_filename']); Why are you referencing the $_FILES array? You shoudl be referencing the current image in $photos_uploaded $function_to_read = "ImageCreateFrom".$function_suffix; $function_to_write = "Image".$function_suffix; // Read the source file $source_handle = $function_to_read ( $images_dir."/".$filename ); You cannot create a variable that has the "name" of a function and then use that variable as a function. You create a STRING, so what thePHP parser sees is something like $source_handle = "imagecreatefromgif" ( $images_dir."/".$filename ); Complete nonsense. I'm sorry if you feel my response is abrasive, but you need to be more aware of what you are doing and understand why you are doing it. Do, one thing at a time and test it. Then do the next thing. Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338152 Share on other sites More sharing options...
MMDE Posted April 17, 2012 Share Posted April 17, 2012 Quote Hi I have added the error_reporting( E_ALL ); And I am still not getting any errors displayed, it is as if the code is being ignored Proper debugging is in place. If there are no syntax errors but it still doesn't do what you want, then there's got to be something wrong with the logic in the script. Usually logical errors in a script is easiest found by printing values that has an impact on your script's result. With other words, try to echo values that is being used before if and while. For example this line: if($image_max_width > $image_max_height){ Replace it with this: echo 'Line: ' . __LINE__ . '$image_max_width = ' . $image_max_width . '<br/>'; echo 'Line: ' . __LINE__ . '$image_max_height = ' . $image_max_height . '<br/>'; if($image_max_width > $image_max_height){ Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338154 Share on other sites More sharing options...
bravo14 Posted April 18, 2012 Author Share Posted April 18, 2012 Psycho: I appreciate your feedback, if I am honest the page is a mashup of two tutorials, and I have tried to put it together as one. I have attached the two original pages, the upload.php allows multiple uploads, and add the images to the database, and the index.php allows for one image upload but also resizes them. MMDE: I will add the echos and see what I get come out. 18101_.phpFetching info... 18102_.phpFetching info... Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338336 Share on other sites More sharing options...
bravo14 Posted April 18, 2012 Author Share Posted April 18, 2012 I now have a series of errors showing, but I have a feeling that they are relate to the first one, the first error is Warning: getimagesize(60.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\haldon\admin\upload.php on line 57 The entire page code is <?php error_reporting( E_ALL ); include("config.inc.php"); include('../includes/connect_inc.php'); // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); // Fetch the photo array sent by add-photos.php $photos_uploaded = $_FILES['photo_filename']; // Fetch the photo caption array $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); //resize photos to maximum height or width //define parameters - maximum height & width for new images $image_max_width=599; $image_max_height=599; $max_dimension=800; // Grab the width and height of the image. list($width,$height) = getimagesize($filename); // If the max width input is greater than max height we base the new image off of that, otherwise we // use the max height input. // We get the other dimension by multiplying the quotient of the new width or height divided by // the old width or height. echo 'Line: ' . __LINE__ . '$image_max_width = ' . $image_max_width . '<br/>'; echo 'Line: ' . __LINE__ . '$image_max_height = ' . $image_max_height . '<br/>'; if($image_max_width > $image_max_height){ if($image_max_width > $max_dimension){ $newwidth = $max_dimension; } else { $newwidth = $image_max_width; } $newheight = ($newwidth / $width) * $height; } else { if($image_max_height > $max_dimension){ $newheight = $max_dimension; } else { $newheight = $image_max_height; } $newwidth = ($newheight / $height) * $width; } // Create temporary image file. $tmp = imagecreatetruecolor($newwidth,$newheight); // Copy the image to one with the new width and height. imagecopyresampled($tmp,$filename,0,0,0,0,$newwidth,$newheight,$width,$height); //end of resizing // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $thumbnail_width = 100; $thumbnail_height = (int)(100 * $size[1] / $size[0]); } else { $thumbnail_width = (int)(100 * $size[0] / $size[1]); $thumbnail_height = 100; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $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."/".$filename ); if($source_handle) { // Let's create an 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."/tb_".$filename ); ImageDestroy($destination_handle ); // $result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />"; } } $counter++; } //header('Location: add-photos.php'); // Print Result echo <<<__HTML_END <html> <head> <title>Photos uploaded</title> </head> <body> $result_final </body> </html> __HTML_END; ?> Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338341 Share on other sites More sharing options...
bravo14 Posted April 18, 2012 Author Share Posted April 18, 2012 I am making progress :-) I just have one error now and it is Warning: imagecopyresampled() expects parameter 2 to be resource, string given in C:\wamp\www\haldon\admin\upload.php on line 91 The offending line now says: imagecopyresampled($tmp,$filename,0,0,0,0,$newwidth,$newheight,$width,$height); Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338361 Share on other sites More sharing options...
batwimp Posted April 18, 2012 Share Posted April 18, 2012 You're doing exactly what it says you're doing: passing in a string instead of a resource. You should probably do something like: $theimage = imagecreatefromjpeg($filename); imagecopyresampled($tmp,$theimage,0,0,0,0,$newwidth,$newheight,$width,$height); Not tested. Link to comment https://forums.phpfreaks.com/topic/261105-images-not-resizing-on-upload/#findComment-1338436 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.