hallucinated Posted September 1, 2008 Share Posted September 1, 2008 here is a little script to resize a POSTed image if(isset($_FILES['userfile'])) { //check file type list($width, $height, $imtype, $attr) = getimagesize($_FILES['userfile']['tmp_name']); if ($imtype == 3) { // cheking image type $ext="png"; } // to use it later in HTTP headers elseif ($imtype == 20) { $ext="jpeg"; } elseif ($imtype == 2) { $ext="jpg"; } elseif ($imtype == 1) { $ext="gif"; } else { $msg = 'Unknown file format. Please select an image file.'; } // This is the temporary file created by PHP $uploadedfile = $_FILES['userfile']['tmp_name']; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // For our purposes, I have resized the image to be // 230 pixels wide, and maintain the original aspect // ratio. This prevents the image from being "stretched" // or "squashed". If you prefer some max width other than // 230, simply change the $newwidth variable $newwidth=230; $newheight=($height/$width)*230; $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // here is where i attempted to assign the $tmp image to a variable, i guess that doesn't work // $image = imagejpeg($tmp); // trying to upload the image to a database if (!isset($msg)) { // If there was no error $data = file_get_contents($tmp); $data = mysql_real_escape_string($tmp); // Preparing data to be used in MySQL query mysql_query("UPDATE profile SET picture = '$data' WHERE userid = '$myid'") or die(mysql_error()); $msg = 'Image updated successfully.'; } } For some reason, file_get_contents($tmp); says that $tmp is non-existent. Any help? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 1, 2008 Share Posted September 1, 2008 $tmp does not hold a file destination, it holds an image resource handle. you're calling $data = file_get_contents() and then overriding $data... so... file_get_contents() is not really being used anyway Quote Link to comment Share on other sites More sharing options...
hallucinated Posted September 1, 2008 Author Share Posted September 1, 2008 So, temp holds the actual image data? How would i get the image into a variable that I could then upload to a MySQL BLOB column? Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 1, 2008 Share Posted September 1, 2008 to get the jpeg image you might want to try <?php ob_start(); imagejpeg($tmp); $image = ob_get_clean(); ?> I've never stored images in a database, so I've never tried this method... but it might work Quote Link to comment Share on other sites More sharing options...
hallucinated Posted September 1, 2008 Author Share Posted September 1, 2008 ob_start(); imagejpeg($tmp); $image = ob_get_clean(); if (!isset($msg)) { // If there was no error //$data = file_get_contents($tmp); //$data = mysql_real_escape_string($tmp); // Preparing data to be used in MySQL query mysql_query("UPDATE profile SET picture = '$image' WHERE userid = '$myid'") or die(mysql_error()); $msg = 'Image updated successfully.'; } } results in: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment Share on other sites More sharing options...
hallucinated Posted September 1, 2008 Author Share Posted September 1, 2008 Found this solution: imagejpeg($tmp, $filename, 100); $instr = fopen("$filename", "rb"); //need to move this to a safe directory $image = addslashes(fread($instr,filesize("$filename"))); if (!isset($msg)) { // If there was no error //$data = file_get_contents($tmp); //$data = mysql_real_escape_string($tmp); // Preparing data to be used in MySQL query mysql_query("UPDATE profile SET picture = '$image' WHERE userid = '$myid'") or die(mysql_error()); $msg = 'Image updated successfully.'; } Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 1, 2008 Share Posted September 1, 2008 that works, but remember with that code you have a file sitting around ($filename) that you might want to consider unlink()ing if you won't be using it. Quote Link to comment Share on other sites More sharing options...
hallucinated Posted September 1, 2008 Author Share Posted September 1, 2008 Well, i suppose that would require me setting a directory with DELETE permissions. If i did not wish to do it the way I mentioned, what would you recommend? (considering the ob_start() method you mentioned earlier did not work). Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 1, 2008 Share Posted September 1, 2008 you could go as is, and assuming you never change $filename, the file will be overridden every run... you'll still have a file sitting in your directory (possibly open to the public), but it's certainly not the end of the world. Also, this code worked for me.. ob_start(); imagejpeg($img); $img = ob_get_contents(); ob_end_clean(); It should work for you after I made a modification. I'm still not quite sure why it did not work in the first place, since ob_get_clean() and the two functions I used here are essentially the same. Quote Link to comment Share on other sites More sharing options...
hallucinated Posted September 2, 2008 Author Share Posted September 2, 2008 No clue why it didn't work for me, that new code didn't work either. And I would and I would want to avoid rewriting the file over and over, especially with multiple people accessing the site at once, the wrong picture my just get inserted into the database. Plus, it appears as if each uploaded retains its filename. Quote Link to comment Share on other sites More sharing options...
genericnumber1 Posted September 2, 2008 Share Posted September 2, 2008 In your original code it would be something like... if(isset($_FILES['userfile'])) { //check file type list($width, $height, $imtype, $attr) = getimagesize($_FILES['userfile']['tmp_name']); if ($imtype == 3) { // cheking image type $ext="png"; } // to use it later in HTTP headers elseif ($imtype == 20) { $ext="jpeg"; } elseif ($imtype == 2) { $ext="jpg"; } elseif ($imtype == 1) { $ext="gif"; } else { $msg = 'Unknown file format. Please select an image file.'; } // This is the temporary file created by PHP $uploadedfile = $_FILES['userfile']['tmp_name']; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // For our purposes, I have resized the image to be // 230 pixels wide, and maintain the original aspect // ratio. This prevents the image from being "stretched" // or "squashed". If you prefer some max width other than // 230, simply change the $newwidth variable $newwidth=230; $newheight=($height/$width)*230; $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); ob_start(); imagejpeg($tmp); $image = ob_get_contents(); ob_end_clean(); // trying to upload the image to a database if (!isset($msg)) { // If there was no error $image = mysql_real_escape_string($image); // Preparing data to be used in MySQL query mysql_query("UPDATE profile SET picture = '$image' WHERE userid = '$myid'") or die(mysql_error()); $msg = 'Image updated successfully.'; } } Quote Link to comment Share on other sites More sharing options...
hallucinated Posted September 2, 2008 Author Share Posted September 2, 2008 Yes, that is what I had. However, I figured out the solution: ob_start(); imagejpeg($tmp); $image = ob_get_contents(); ob_get_clean(); $final = addslashes($image); $final is inserted into the database. Thanks for your help. 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.