PHP - Get Width & Height From Mysql Stored Blob Image
Okay, so here is the deal.
Have a table which stores image as blob files. Now i want to read the image width and height directly from the blob field. Is this possible and if yes, how? Things i tried so far; list($size[0],$size[1],$type, $attr) = getimagesize('image.php?i=26ddd45b02859e836d13d4b9fde34281'); print_r($size); $img = 'image.php?i=26ddd45b02859e836d13d4b9fde34281'; echo imagesy($img); image.php grabs the image from DB and show's it with header("Content-type: image/jpg"); It works for just showing the images with the <img> tag. Any ideas of help would be great! Similar TutorialsHi! I am building a little PHP/MySQL application where pictures are uploaded and stored in MySQL in a longblob. These are special circumstances and storing images in the database in an absolute must. Uploading is working fine. The upload script inserts the following into the field `image_data`: base64_encode(file_get_contents($_FILES['image']['tmp_name'])) The problem is displaying the images. I cannot for the life of me make it work. I've tried the code on multiple systems with various versions of PHP and MySQL. I have two files: one called view.php and one called show.php. # view.php # It gets $info['id'] from a query getting the ID of the recent-most image uploaded. echo '<img src="show.php?id='.$info['id'].'" alt="" />'; # show.php # $id is determined by $_GET['id'] and passes through security checks I've omitted here. # There is zero (not even a whitespace) output before the header()s are sent. $query = "SELECT `image_data`,`image_mime`,`image_size` FROM `upload`.`files` WHERE `id` = '$id';"; $sql = mysql_query($query) or die(mysql_error()); $image = mysql_fetch_assoc($sql); header('Content-Type: ' . $image['image_mime']); header('Content-Length: ' . $image['image_size']); echo base64_decode($image['image_data']); The problem is that no image is displayed either in view.php or when I call show.php directly with a valid ID. I have verified that $image['image_mime'] and $image['image_size'] contain the right data. However, if I download show.php and change extension to for example .jpg, the image is there. So the image is stored correctly in the database and $image['image_data'] is outputting the right data. I even compared checksums for the image before and after and they're identical, so I would conclude that the error is in the outputting of the image - but I can't figure out what. Error_report is set to E_ALL but there's nothing useful coming out. Any ideas? Code: [Select] $thephoto = $row['thePHOTO']; in mysql thephoto is BLOB file type and stores an image i want to display the image as an avatar, in a while loop. Code: [Select] while($row = mysql_fetch_assoc($query4)) { $id = $row['ID']; $thename = $row['theNAME']; $thephoto = $row['thePHOTO']; } how do i make blob readable and then display as an image? Hi All, This is my first post on here. I'd really appreciate any help with this, it's driving me mad. I'm trying to create a form to save a image into a mySQL database. I have a website hosted by UK2.net which has a mysql db with a table called gallery(name (varchar 30), size (int), type varchar(30), thePic(mediumBlob)). I have a form with this: Code: [Select] <td><p><label>Pic: </label></td><td><input name="userfile" type="file" id="userfile" /></td></p> Which when submitted actions addImage.php. The code for this looks like this: Code: [Select] <?php $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); } $con = mysql_connect("localhost", "userName", "password") or die(mysql_error()); mysql_select_db("dbName", $con) or die(mysql_error()); $query = "INSERT INTO gallery (name, size, type, thePic) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; if (!mysql_query($query,$con)) { die('Error: ' . mysql_error()); } echo "<br>File $fileName uploaded<br>"; ?> I get the following error message: Quote Warning: fopen() [function.fopen]: Filename cannot be empty in /home/hiddenje/public_html/addImage.php on line 13 Does anyone know what this is about? I've been on out friend google and a lot of people seem to be pointing to permissions but I can't seem to apply it to my scenario and just can't get it to work. Im a developer by trade, but this is my first step into the...interesting world of PHP and mySQL. Again, I'd appreciate any help with this. I'd love someone to talk me through exactly what I'm missing or doing wrong. Thanks in advance. Hi guys, I have a problem. I need to create a page that has a web form to upload an image to a mySQL database and place it in a blob field. Then I need to be able to query the database later to display the image on the site. I've looked around but I just haven't found any examples that can help me. Does anyone know of any good example or can anyone please give me an example? I have nothing so far, just the html web form and database. this is my 3 files which is i am using to show data of customer but i am not able to see them their logo image please see them & help me guys i dont know whats wrong in this. these files i uploaded please find attachment to see them. thnx in advanced [attachment deleted by admin] i am uploading image to s3 server and by php i am making a copy of that image on server , if i upload an image of 2.3 mb than the width of image is not coming but if i upload less size image like 26kb than it is showing the width of image so it is able to create the copy . here is my code of php : $s3 = new S3(awsAccessKey, awsSecretKey); $thumbId = uniqid(); $thumbId .= ".jpg"; $img = ''; if($imgType == "image/jpeg"){ $img = imagecreatefromjpeg($sourceUrl); }else if($imgType == "image/png"){ $img = imagecreatefrompng($sourceUrl); }else if($imgType == "image/gif"){ $img = imagecreatefromgif($sourceUrl); }else{ $img = imagejpeg($sourceUrl); } echo $width = imagesx( $img ); echo $height = imagesy( $img ); please tell me what is the problem with size of image.. regards rahul Hi Fellas, having a bit of a brain freeze here, i am trying to set a max width or height for a uploaded image, but my brain has frozen, maybe its the cold weather lol list($width,$height)=getimagesize($uploadedfile); $newwidth=250; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); basicly i want to find the biggest size of an image, beit width or height then set that size to 250... Any pointers here? Hey guys! I have the following php code that grabs variables (and the browsed image) from Flash. //FLASH VARIABLES $Name = $_POST['Name']; $itemNumber = $_POST['itemNumber']; $filename = $_FILES['Filedata']['name']; $filetmpname = $_FILES['Filedata']['tmp_name']; $fileType = $_FILES["Filedata"]["type"]; $fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000); list($filename, $extension) = explode('.', basename($_FILES['Filedata']['name'])); $filename = $Name; $target = $filename . $itemNumber . "." . $extension; // Place file on server, into the images folder move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$target); This works perfect, but what I want to change is the width and height of the uploaded image. Any ideas/suggestions on how this could be done? Thanks in advance!! Cheers! I have folder with 100's images, how to find Height and width as Excel sheet? Hey guys I have a small issue, i have an upload that resizes the image into thumbnail by max width and ratios the width based on that. Here is my code
What I am wanting to do is instead upload the image with a max height and ratio the width proportionally. What variables do I have to reverse? Hi guys, This is my first time to insert PDF into MySQL BLOB. Below is my form that i used Code: [Select] <?php <form enctype="multipart/form-data" name="frmUploadFile" action="ulf-exec.php" method="post"> <select name="title" id="title"> <option>xxx</option> <option>yyy</option> <option>zzz</option> </select> </label> <input name="des" type="text" class="dropdownlists1" id="des"></td> <input name="fileUpload" type="file" class="dropdownlists1" id="fileUpload" size="20" border=""></td> <input type="submit" name="button" id="button" value="Submit"> </form> ?> I have prepared my database based on the required but decided to test with echo just to confirm there's no issue with the code The action="ulf-exec.php" : Code: [Select] <?php function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $title = clean($_POST['title']); $des = clean($_POST['des']); $fileUpload = $_POST['fileUpload']; if(empty($des) || $fileUpload == "none") die("You must enter both a description and file"); $fileHandle = fopen($fileUpload, "r"); $fileContent = fread($fileHandle, $fileUpload_size); $fileContent = addslashes($fileContent); $date = date('d').'-'.date('m').'-'.date('y'); $time = date('h').':'.date('i').':'.date('s'); echo "<h1>File Uploaded</h1>"; echo "The details of the uploaded file are shown below:<br><br>"; echo "<b>File name:</b> $fileUpload_name <br>"; echo "<b>File type:</b> $fileUpload_type <br>"; echo "<b>File size:</b> $fileUpload_size <br>"; echo "<b>Uploaded to:</b> $fileUpload <br><br>"; echo "<a href='uploadfile.php'>Add Another File</a>"; ?> This is the error: Code: [Select] Warning: fopen() [function.fopen]: Filename cannot be empty in /ulf-exec.php on line 30 Warning: fread(): supplied argument is not a valid stream resource in ulf-exec.php on line 31 Hello, I am storing files that my users upload to the website as a blob in mysql database. Here is the code that does uploading: $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 = mysql_real_escape_string($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = mysql_query("INSERT INTO documents (idapplicant, fileType, fileSize, fileData, fileName) VALUES ('$idapplicant','$fileType','$fileSize', '$content','$fileName')",$db); Here is the code for download: echo "File found:".@mysql_result($result, 0, "fileID"); //always correct $data = @mysql_result($result, 0, "fileData"); $name = @mysql_result($result, 0, "fileName"); $size = @mysql_result($result, 0, "fileSize"); $type = @mysql_result($result, 0, "fileType"); header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo $data; I can upload/download files no problem. However, I noticed problem with GIF files. All GIF files are just dark screen after downloading. All other files (PDF, JPEG) looked just fine. I compared files (before upload and after download) in HEX editor and found that the first byte on all files is set to A0 (it is added in front of all the data) and the last byte is deleted! I have tried: $content = addslashes($content); instead of: $content = mysql_real_escape_string($content); with the same results I also tried removing this line of code and in that case no file would be uploaded at all! Any idea? Thank you I have created a image using imagepng() and then right-click'd and saved it. I then added it to the database by using phpMyAdmin to upload it directly into a field called 'avatar' which is a BLOB. The code the view the image back is the following: header("Content-type: image/png"); echo mysql_result(mysql_query("SELECT `avatar` FROM `rscd_players` WHERE `username` = 'Kryptix'"), 0); Now when I try and add the imagepng() directly to the database it doesn't work. I've tried multiple things. Here's the code: ob_start(); imagepng($char_image); $pngimagedata = ob_get_contents(); ob_end_clean(); mysql_query("UPDATE `rscd_players` SET `avatar` = " . $pngimagedata . " WHERE `username` = 'Kryptix'"); Here's the full code: http://pastebin.com/jqsKc9Qd This is the query that phpMyAdmin produces: http://pastebin.com/nuypNuwJ This is the query that the above code produces: http://pastebin.com/NPWmi5eb phpMyAdmin is 2.2kB, my code is 4.4kB Any idea? I've been trying all night... Hi everyone, i am just trying to learn php for a bit of fun really and started making a sort of 'facebook' website. I am having trouble however trying to display different users images, for example when trying to find a correct 'friend' only the image of the last result is being shown for all people with the same name... here is my code below, if anyone can help me out that would be great file 1 $count=1; while ($numids>=$count){ echo "<form method=\"post\" action=\"friendadded.php\">"; $frienduserid=$_SESSION["passedid[$ii]"]; $friendfirstname=$_SESSION["passedfirstname[$ff]"]; $friendlastname=$_SESSION["passedlastname[$ll]"]; $_SESSION['friendsuserpicid'] = $frienduserid; echo "<table width=\"700\" height=\"50\" border=\"1\" align=\"center\">"; echo "<tr>"; echo "<th></th>"; echo "<th>First Name</th>"; echo "<th>Last Name</th>"; echo "</tr>"; echo "<tr>"; echo "<td><center>"; echo "<img border=\'0\' src=\"frienduserpic.php\" width=\"80\" height=\"80\" align=\"middle\"/>"; echo "</center></td>"; echo "<td><center>"; echo $friendfirstname; echo "</center></td>"; echo "<td><center>"; echo $friendlastname; echo "</center></td>"; echo "</tr>"; echo "</table>"; echo "<center><input type=\"submit\" value=\"Add this friend\" name=\"Add Friend\"></center><br/>"; $ii=$ii+1; $ff=$ff+1; $ll=$ll+1; $count=$count+1; echo "</form>"; } file 2 session_start(); $passeduserid=$_SESSION['friendsuserpicid']; $timespost=$_SESSION['postednum']; $host= $username= $password= $db_name= $tbl_name= mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $query = mysql_query("SELECT * FROM $tbl_name WHERE picid='".$passeduserid."'"); $row = mysql_fetch_array($query); $content = $row['image']; header("Content-type: image/jpeg"); echo $content; Thanks in advance it should display the image but nothing appears. what have i done wrong? Code: [Select] echo "<img src=\"photo.php?id=$studentid\" alt='photo' />"; photo.php Code: [Select] <?php $id= $_GET['id']; $getphoto= mysql_query("SELECT photo FROM Student WHERE SID=$id LIMIT 1"); $row5 = mysql_fetch_assoc($getphoto); $photogot = $row5['PHOTO']; header("Content-type: image/gif"); print $photogot; ?> Hi everyone, I've read lots of tutorials on this, but something is not clicking in my brain with it. I think my coding is close, but, as of right now, all I get is a red x for the image when I try to display the image. Let me share my code as a starting point - I would truly appreciate any helpful comments or blatant errors that are pointed out or shared with me. Here is the upload image form and code (so they clicked the item name and then go into this): if($_REQUEST['modifyfeatured']) { $id=$_REQUEST['modid']; $sqlid="SELECT * FROM product WHERE id='$id'"; $resultid=mysql_query($sqlid, $dbh); $idrow = mysql_fetch_array($resultid); echo "<p>"; echo "Fill out the form below to add a short text line (i.e. 20% Off!) and/or an image (like the new note).<br>"; echo "You are modifying the following item: <p><b>"; echo $idrow['product_id']; echo " "; echo $idrow['title']; echo "</b><p>"; ?> <table border="0" cellpadding="2" cellspacing="0"> <tr> <td> <form method="post" enctype="multipart/form-data"> <input type="hidden" name="id" value="<? echo $id; ?>"> Short Text Message: </td><td> <input type="Text" name="message"></td></tr> <tr><td>Small Image:</td><td> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"></td></tr> <tr><td> </td><td> <input name="upload" type="submit" class="box" id="upload" value="Submit"> </td></tr></table> </form> <? } //then when the click the upload link, here is the code for that: 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']; $message=$_REQUEST['message']; $id=$_REQUEST['id']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include 'library/config.php'; include 'library/opendb.php'; //$query = "INSERT INTO featured_prods (name, size, type, image, message ) ". //"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$message')"; $query="UPDATE featured_prods SET name='$fileName', size='$fileSize', type='$fileType', image='$content', message='$message' WHERE id='$id'"; //$sqldone="UPDATE product SET product_id='$product_id', title='$title', description='$description', regular_price='$regular_price', sale_price='$sale_price', stat='$stat', weight='$weight', close_out='$close', additional='$additional', additionalpix='$additionalpix_name' WHERE id='$id'"; //$resultdone=mysql_query($sqldone, $dbh); mysql_query($query) or die('Error, query failed'); include 'library/closedb.php'; echo "<br>File $fileName uploaded<br>"; } //end if upload is hit Okay, now here is the code trying to display the image: <? $featuredquery="SELECT * FROM featured_prods"; $featuredresult=mysql_query($featuredquery, $dbh); while($featuredrow=mysql_fetch_array($featuredresult)){ $id=$featuredrow['id']; $prodquery="SELECT * FROM product WHERE id='$id'"; $prodresult=mysql_query($prodquery, $dbh); $prodrow=mysql_fetch_array($prodresult); echo $prodrow['title']; echo "<br>"; echo $featuredrow['message']; echo "<br>"; ?> <img src="getimage.php?id=<?echo $id;?>" alt="cover" /> <? } And here is the code for getimage.php: <? $id=$_GET["id"]; $result = mysql_query("select image from featured_prods where id='$id'"); $row = mysql_fetch_row($result); $data = base64_decode($row[0]); $im = imagecreatefromstring($row[0]); imagejpeg($im); header('Content-type: ' . image/jpeg); // 'image/jpeg' for JPEG images echo $data; ?> Again, any help would be appreciated. I'm a real rookie here, and I appreciate everyone's time and effort to assist me very much. Hi, I have managed to get the code working to store a .jpg file in the database under the longblob type. Now all i have left to do is to retrieve that image and display it. So far i have this: list.php Code: [Select] while($r = mysql_fetch_array($sql)) { //for each record ... echo " <img src= getoutside.php?id='".$r[apartmentId]. " '> "; getoutside.php Code: [Select] <?php header("Content-type: image/jpg"); // act as a jpg file to browser $nId = $_GET['id']; include 'dbase.php'; //connect to database $sqlo = "SELECT outside FROM apartment WHERE apartmentId = $nId"; $oResult = mysql_query($sqlo); $oRow = mysql_fetch_array($oResult); $sJpg = $oRow["outside"]; echo $sJpg; ?> The result from this is a box with a red cross in it. Can anyone find a problem with this code please? Thanks I using.. Code: [Select] echo '<img src="data:image/jpg/png/jpeg;base64,' . base64_encode( $row['image'] ) . '" height="150" />'; This is showing up images great in firefox, safari and chrome, but in internet explorer it shows a nice red cross, and I assume it is because of the encoding? Does anyone know how to get working in internet explorer as well? Pretty urgent job! Much appreciated for your help! I have a base64 encoded image -> http://pastebin.com/698ES5t0
Im trying to export this as a png file.
$picture = {data on paste bin}; $picture = base64_decode($picture); file_put_contents('/home/picture.png', $Picture);Now this creates a file picture.png and i can open it in Preview/Gimp etc. However, if i upload the file to my website, the image does not render in chrome/firefox, however it does render in Safarai. It seems that there is no height/width/depth data. |