dmonaghan@hotmail.com Posted July 11, 2012 Share Posted July 11, 2012 I cannot display a blob image from MySQL on my live hosted environment, it works on dev. I have a getimage.php file that I pass the id of the image to, I am not writing out any text on the page. Both these methods work perfectly on my development environment. Simple code: Method 1: header("Content-type: image/jpeg"); $data = mysql_result($result, 0,'documentfile'); echo $data; Method 2: header("Content-type: image/jpeg"); $data = mysql_result($result, 0,'documentfile'); $bImage = imagecreatefromstring($data); imagejpeg($bImage); imagedestroy($bImage); I don't get a php error I get the following error: The Image http://www.mywebsite.com/getimage.php?id=15" cannot be displayed because it contains errors` If I hardcode an imagepath on my live environment it works fine: header("Content-type: image/jpeg"); $data = file_get_contents("../images/sunflowers.jpg"); echo $data; If I remove the: header("Content-type: image/jpeg"); The image displays as it would if you right click an image and open in notepad. Also if I move the header to after where i set the image I get a broken image on the page and not the 'error' text above. $data = mysql_result($result, 0,'documentfile'); header("Content-type: image/jpeg"); echo $data; Any help would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/ Share on other sites More sharing options...
requinix Posted July 11, 2012 Share Posted July 11, 2012 Comment out the call to header() and look for error messages. You can also compare the output from the two sites (given the same file). Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1360837 Share on other sites More sharing options...
dmonaghan@hotmail.com Posted July 11, 2012 Author Share Posted July 11, 2012 Hi, As mentioned, if I comment out the header, the image displays as it would if you right click an image and open in notepad. thanks Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1360839 Share on other sites More sharing options...
requinix Posted July 11, 2012 Share Posted July 11, 2012 As mentioned, if I comment out the header, the image displays as it would if you right click an image and open in notepad. Fair enough, missed that. So how do the two outputs compare? If you save the non-working image as a .jpg and view it on your computer does that work? Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1360842 Share on other sites More sharing options...
dmonaghan@hotmail.com Posted July 11, 2012 Author Share Posted July 11, 2012 Hi requinix, The outputs are different between dev and live. it's the same Db so they should be the same. Dev starts with: ????JFIFHH??!1"AQa q?????#$?2BR???rb? Live Starts with: ÿØÿ? JFIFHHÿ? I can save the image on dev and open it fine I cannot however save the image on live: $bImage = imagecreatefromstring($data); imagejpeg($bImage, '../images/fromweb.jpg'); imagedestroy($bImage); thanks Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1360858 Share on other sites More sharing options...
requinix Posted July 12, 2012 Share Posted July 12, 2012 Looks like the image data was UTF-8 encoded. Do you do anything about that when uploading images or inserting to the database? Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1361117 Share on other sites More sharing options...
dmonaghan@hotmail.com Posted July 12, 2012 Author Share Posted July 12, 2012 Hi requinix, I don't do any encoding apart from when uploading the image. $FileType = $_FILES['myimage']['name']; $FileTypePos = strpos($FileType, "."); $FileType = substr($FileType, $FileTypePos + 1); // Temporary file name stored on the server $tmpName = $_FILES['myimage']['tmp_name']; // Read the file $fp = fopen($tmpName, 'r'); $data = fread($fp, filesize($tmpName)); $data = addslashes($data); fclose($fp); Then I add $data via a stored procedure. I appreciate you helping with this. Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1361131 Share on other sites More sharing options...
requinix Posted July 12, 2012 Share Posted July 12, 2012 What data type do you use for the documentfile column? It needs to be a binary type, not a text type. Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1361138 Share on other sites More sharing options...
dmonaghan@hotmail.com Posted July 12, 2012 Author Share Posted July 12, 2012 So I uploaded the images into the DB again and they now appear. Must be something to do with db encoding and the transfer of the db from dev to live. Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1361140 Share on other sites More sharing options...
PFMaBiSmAd Posted July 12, 2012 Share Posted July 12, 2012 If magic_quotes_runtime is on, fread will escape the data. Using addslashes will escape it a second time, leaving it invalid. What does var_dump(get_magic_quotes_runtime()); show on both systems? Also, using GD functions just to save an image you retrieved from a database as a file is A) wasteful, you can just save the binary data to a file and B) modifies the image because the default quality level for imagejpeg is 75%. Quote Link to comment https://forums.phpfreaks.com/topic/265529-cannot-display-image-blob-from-mysql-in-php-on-my-live-environment-works-on-dev/#findComment-1361143 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.