Scotty2024 Posted September 5, 2009 Share Posted September 5, 2009 I'm trying to save modified images into a MySQL database. I am able to save images directly into the database (second PHP code block below) but I can't after I modify them (first PHP code block below). By modify I mean resize them after they have been uploaded. After uploading the file I check the size and modify if needed. Here is an example for jpg files if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $size = getimagesize($tmpName); if($size[0] > $size[1]) //width > height { //modify as needed, x & y are some hardcoded value $h = x; $w = y; } else if($size[0] < $size[1]) { //modify as needed, x & y are some hardcoded value $h = x; $w = y; } else if($size[0] == $size[1]) //width == height { //modify as needed, x & y are some hardcoded value $h = x; $w = y; } //I'm not sure about the above code, but the following code below is all a guess. $src = imagecreatefromjpeg($tmpName); $dst = imagecreatetruecolor ($w_tn, $h_tn); //$w_tn & $h_tn calculations are not shown here imagecopyresized($dst, $src, 0, 0, 0, 0, $w_tn, $h_tn, $w,$h); imageinterlace($dst, 1); $out_file= rand() . '.jpg'; imagejpeg ($dst, $out_file); $query = "INSERT INTO pictures (`fileName`, `fileType`, `fileSize`, `imageData`) VALUES ('$out_file //right?', 'image/jpeg', '//I don't know', '$dst //right?')"; //I'm lost on this query mysql_query($query) or die(mysql_error()); } I'm lost as to what to pass in my query for imageData. By the way, imageData is type mediumblob. I am able to save a retrieve images from my database if I don't modify them. I did it like this <?PHP if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } require("connect.php"); $query = "INSERT INTO pictures (`fileName`, `fileType`, `fileSize`, `imageData`) VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); mysql_close(); echo "<br>File $fileName uploaded<br>"; } ?> How can I merge these two pieces of code together? Thanks! Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/ Share on other sites More sharing options...
SilveR316 Posted September 5, 2009 Share Posted September 5, 2009 Where you have this: imagejpeg ($dst, $out_file); Wrap that in output buffers, and capture the contents of them. Then stick the contents into the database. Make sure you remove the 2nd parameter ($out_file) from imagejpeg() otherwise itll just save it to disk instead of outputting it. If you do output it, then read the contents of the $out_file file the same way you did it in your 2nd piece of code and save that. Functions: ob_start(), ob_get_clean(); Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912902 Share on other sites More sharing options...
Scotty2024 Posted September 5, 2009 Author Share Posted September 5, 2009 Hello SilveR316! Thanks for the reply! I'm not familiar with output buffers. Could you give me an example, or perhaps point me to a relevant tutorial? Thank you! Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912905 Share on other sites More sharing options...
SilveR316 Posted September 5, 2009 Share Posted September 5, 2009 http://ca.php.net/manual/en/function.ob-get-clean.php Take a look at Example #1. The imagejpeg() function and related functions (imagepng(), imagegif()) will output directly to the browser (just like a regular 'echo') if you leave off the 2nd parameter. Since they are essentially just echoing to the browser, you can use output buffering to capture the output in to a variable. // resize the image here ob_start(); imagejpeg($dst); $image = ob_get_clean(); // save $image to the database Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912908 Share on other sites More sharing options...
Scotty2024 Posted September 5, 2009 Author Share Posted September 5, 2009 I gave that a try but I am getting the following error and I'm not sure why 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 Here is the updated code if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $size = getimagesize($tmpName); if($size[0] > $size[1]) //width > height { //modify as needed, x & y are some hardcoded value $h = x; $w = y; } else if($size[0] < $size[1]) { //modify as needed, x & y are some hardcoded value $h = x; $w = y; } else if($size[0] == $size[1]) //width == height { //modify as needed, x & y are some hardcoded value $h = x; $w = y; } //I'm not sure about the above code, but the following code below is all a guess. $src = imagecreatefromjpeg($tmpName); $dst = imagecreatetruecolor ($w_tn, $h_tn); //$w_tn & $h_tn calculations are not shown here imagecopyresized($dst, $src, 0, 0, 0, 0, $w_tn, $h_tn, $w,$h); imageinterlace($dst, 1); $out_file= rand() . '.jpg'; ob_start(); imagejpeg ($dst); $image = ob_get_clean(); require("connect.php"); $query = "INSERT INTO pictures (`fileName`, `fileType`, `fileSize`, `imageData`) VALUES ('fileName', 'image/jpeg', '', '$image')"; mysql_query($query) or die(mysql_error()); mysql_close(); } Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912916 Share on other sites More sharing options...
SilveR316 Posted September 5, 2009 Share Posted September 5, 2009 You probably need to escape the $image string. http://ca.php.net/manual/en/function.mysql-real-escape-string.php Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912919 Share on other sites More sharing options...
Scotty2024 Posted September 5, 2009 Author Share Posted September 5, 2009 Cool that seems to have fixed it! Thanks! I do have another question about outputting the stored images. I am able to do it as follows: <?php if(isset($_GET['fileName'])) { require("connect.php"); $id = $_GET['fileName']; $query = "SELECT * FROM upload WHERE fileName = '$id'"; $result = mysql_query($query) or die(mysql_error()); list($fileName, $fileType, $fileSize, $content) = mysql_fetch_array($result); header("Content-Type: image/jpeg"); echo $content; //picture does print successfully! mysql_close(); } ?> <html> <head> <title>Download File From MySQL Test</title> </head> <body> <?php require("connect.php"); $query = "SELECT fileName FROM upload"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) == 0) { echo "Database is empty <br>"; } else { while(list($fileName) = mysql_fetch_array($result)) { ?> <a href="download.php?id=<?php echo urlencode($fileName);?>"><?php echo urlencode($fileName);?></a><br> <?php } } mysql_close(); ?> </body> </html> This does not allow me to format my output. I would like to display the image within my website somewhere rather than just outputting it. I've tried to customize the output but it doesn't seem to work. Is it possible to customize it a little more beyond just outputting it as is it doing now? I would also like to output more than one image. Thanks! Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912922 Share on other sites More sharing options...
SilveR316 Posted September 5, 2009 Share Posted September 5, 2009 The thing about storing images in a database, is that you need to write a script that will show the image. This means outputting the appropriate headers as well. Take the first part of your code, and stick it in to a php file. Then you can request the image using that php file. Like this: <img src="image.php?id=45" /> Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912926 Share on other sites More sharing options...
Scotty2024 Posted September 5, 2009 Author Share Posted September 5, 2009 Is there a more secure way to pass the id rather than through the URL? I would like to make it so that not just any picture could be retrieved but only "requested" images could. Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-912929 Share on other sites More sharing options...
Scotty2024 Posted September 5, 2009 Author Share Posted September 5, 2009 Any ideas how to pass the id more securely? The only other way I know how to pass variables is with a form. I'm not sure how that could be applied in this situation. Thanks. Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-913203 Share on other sites More sharing options...
Scotty2024 Posted September 5, 2009 Author Share Posted September 5, 2009 Never mind, I'm just going to pass the id number with sessions. I think that will work. Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-913342 Share on other sites More sharing options...
Scotty2024 Posted September 6, 2009 Author Share Posted September 6, 2009 I am finding this to not be very reliable and extremely odd where it fails. For example, if I am trying to get two or more images from my database, it is very rare for both images to be displayed. For example if I want to get picture A and picture B I would do something similar to the following: $_SESSION['picID'] = 'A'; $_SESSION['picAlbum'] = 'test'; echo "<img src='image.php' />"; $_SESSION['picID'] = 'B'; $_SESSION['picAlbum'] = 'test'; echo "<img src='image.php' />"; 90% of the time picture A would display and picture B would not. However, if I refresh the page enough picture B would display eventually and picture A would not. In Firefox nothing is displayed when the picture fails but in IE (v8) there is a red X where the image should be. What is very odd is that sometimes when it fails, I can try again by passing the picID in the url instead of setting it in the session variable and it works. Like this: $_SESSION['picID'] = 'A'; $album = 'test'; echo "<img src='image.php?e=$album' />"; $_SESSION['picID'] = 'B'; $album = 'test'; echo "<img src='image.php?e=$album' />"; I don't get why it is so touching. I don't understand why sometimes I can set a session variable with the ID and other times I have to pass it in the url. Also how come only the first image displays 90% of the time and both images have never display together? Here is my retrieval code (image.php): session_start(); require("connect.php"); $id = $_SESSION['picID']; $album = $_SESSION['picAlbum']; if($album == '') { //this is for that weird case when it fails if I set the session variable and have //to pass the id in the url. I have no clue why I have to do this sometimes. $album = htmlentities($_GET['e']); } if($id != '' && $album != '') { $query = "SELECT * FROM pictures WHERE album='$album' and id='$id'"; $result = mysql_query($query) or die(mysql_error()); list($ablum, $id, $name, $type, $imageData, $altText) = mysql_fetch_array($result); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo $imageData; } //I thought unsetting the session variables might have helped. I don't think it did. unset($_SESSION['picID']); unset($_SESSION['picAlbum']); mysql_close(); Do you see anything wrong? Thanks! Link to comment https://forums.phpfreaks.com/topic/173183-saving-modified-images-in-database/#findComment-913699 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.