Jump to content

Cannot display Image blob from MySQL in PHP on my live environment, works on dev


Recommended Posts

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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%.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.