Jump to content

Recommended Posts

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. 

Link to comment
https://forums.phpfreaks.com/topic/211557-assistance-with-displaying-blob-image/
Share on other sites

Thank you both for your help.  samshel, you are correct - it doesn't make sure to use base64_decode if it wasn't inserted that way.  I don't actually see any reason to encode the images at all.

 

So I changed the code on my getimage.php using both of your suggestions, but I still don't get the image.  Any other thoughts on what I've been doing wrong?  I got this code from different tutorials, but none of the tutorials have explained in enough detail for me to really "get" this, so I'm still kind of muddling through this.

 

here is the new code I tried on 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]); 
$data=$row[0];
         
$im = imagecreatefromstring($row[0]); 
imagejpeg($im);

header('Content-Type: image/jpeg'); // 'image/jpeg' for JPEG images
echo $data;

?>

Your getimage.php code doesn't contain any code to make the connection to the database server or select a database. If you were testing this on a system with error_reporting set to E_ALL and display_errors set to ON and the header() statement was temporarily commented out, you would be seeing the php detected errors.

 

There's also no point in using the following two lines of GD code because you are not manipulating the image using GD functions (actually you are because the default image quality is 75% so using imagejpeg is reducing your image quality) -

$im = imagecreatefromstring($row[0]);

imagejpeg($im);

 

Also, imagejped() in the above code OUTPUTS the image data. So, remove those two lines of code.

 

At a minimum, your code should -

 

1) Make a connection to the database server and select your database.

2) Get and validate/escape the $_GET['id'] value.

3) Query the database to get the image data and the image type.

4) Retrieve the data from the result set.

5) Form and output the content-type header using the image type gotten from the database.

6) Output the image data.

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.