Displaying Image uploaded to mysql PHP - need to display string - php

I was trying to figure out how to upload an image into mysql then display it. I got the uploading part set. The problem I am have is displaying it. When I go to display it does one of the following:
Displays no image but only has a little image icon 10x10px image on top left
Displays the whole string of the image and then properly renders the image.
When I remove/comment out the echo $contentIMG statement, nothing displays.
<?php
include 'connections/conn.php';
error_reporting(E_ALL);
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$contentIMG = $row_Recordset1['content'];
echo $row_Recordset1['content'];
//echo $contentIMG;
// set the header for the image
//header("Content-type: image/jpeg");
header("Content-type: image/jpeg\n\n");
echo "where is the image #1<br>";
//echo '<img src="data:image/jpeg;base64,'.$contentIMG.'"/>';
echo "where is the image #2<br>";
echo '<img src="data:image/jpeg;base64,'.base64_encode( $contentIMG ).'"/>';
if (isset($contentIMG)){
//echo '<img src="data:image/jpg;base64,'.base64_encode($contentIMG).'" />';
}
//echo mysql_result($result, 0);
// close the db link
mysql_close($result);
}
else {
echo 'Please use a real id number';
}
echo "break <br>";
$img=base64_encode($row_Recordset1['content']);
?>
I looks like this
enter image description here

I may be wrong but I think that when you set your header Content-type to image/jpeg, you should just return the image data (assuming it is stored as a blob in your database)
<?php
header('Content-type: image/jpeg');
echo $contentIMG;
?>
Your code should look like this:
<?php
include 'connections/conn.php';
error_reporting(E_ALL);
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$contentIMG = $row_Recordset1['content'];
header('Content-type: image/jpeg');
echo $row_Recordset1['content'];
// close the db link
mysql_close($result);
}
else {
echo 'Please use a real id number';
}
?>

Related

Retrieve image long raw datatype stored in oracle database in php

I tried to display image stored in oracle database
I get it as decode data
I tried to this code but not work
first way
$img= studimage::select('studimage')->where('studnum',$id)->first();
header("Content-type: image/jpeg");
echo ($img->studimage) ;
sec-way
echo '<img src="data:image/jpg;base64,'. base64_encode($img->studimage). '" />';
the two ways does not work :(
I'm using this same functionality in one of my project, please check my code below
you can either make a page that will render the image
<img src="image.php?id=123" />
That image.php page would have this:
$sql = "SELECT image FROM images WHERE image_id = " . (int) $_GET['id'];
$stid = oci_parse($conn, $sql);
oci_execute($stid);
$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);
if (!$row) {
header('Status: 404 Not Found');
} else {
$img = $row['IMAGE']->load();
header("Content-type: image/jpeg");
print $img;
}
Or, you could base64 encode it into the src (note, not all browsers handle this well):
<img src="data:image/jpeg;base64,<?php echo base64_encode($img); ?>" />

Image from blob object not displayed in a page

This is my HTML code for displaying image stored as blob object which is stored in a database.
<tr><td>photo</td><td><img src="image.php?id=<?php echo $employee['id']; ?>" /></td></tr>
This is the image.php file:
<?php
include db.php;
$employee_id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
try
{
$sql = "SELECT photo
FROM employee where id = $employee_id limit 1";
$s = $pdo->prepare($sql);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Employee not found' . $e->getMessage();
include 'error.html.php';
exit();
}
$employee = $s->fetch();
$image = $employee['photo'];
header('Content-Type: image/jpeg');
echo $image;
?>
When I look at the HTML code in Firebug, I can see
<img src="image.php?id=9">
But no image is displayed in the table row. Do you know how to find out what is wrong?
When I open the page ...image.php?id=9 in Firefox
It shows a message
The image ...image.php?id=9 cannot be
displayed because it contains errors.
It is strange that every image I have contains an error. I inserted only images with
size smaller than the blob object.
do you get an error?
try to use "exit;" after the echo and remove the closing php tag.
it's optional and you don't really need it (http://php.net/manual/en/language.basic-syntax.instruction-separation.php).
header('Content-Type: image/jpeg');
echo $image;
exit;

PHP: if image exists show image else show different image

i am making a login system with registration and a profile page in php and i am trying to make a profile picture work.
if the user has not uploaded a profile picture yet then make it show a "no profile picture" image if the user has uploaded a profile picture make make it show the image that he has uploaded.
Right now it only show the default picture, noprofile.png.
< img src="uploads/< ? echo "$username" ? >/noprofile.png">
i want it to show icon.png if icon.png has been uploaded and if it hasnt been uploaded make it show, noprofile.png.
Just run it through the logic, using file_exists:
$image="/path/on/local/server/to/image/icon.png";
$http_image="http://whatever.com/url/to/image";
if(file_exists($image))
{
echo "<img src=\"$http_image\"/>\n";
}
else
{
echo "<img src=\"uploads/$username/noprofile.png\"/>\n";
}
Check to see if the file has been uploaded by using file exists. If the file exists, use that url else use the default noprofile.png.
you could make a column in the DB to store a value if it has been uploaded or not.
OR
you could see if the file exists.
<?php
if (file_exists('uploads/' . $username . '/icon.png')) {
echo '<img src="uploads/' . $username . '/icon.png">';
}
else {
echo '<img src="uploads/' . $username . '/noprofile.png">';
}
?>
<?php
$img = file_exists(sprintf('/path/to/uploads/%s/icon.png', $username))
? 'icon.png' : 'noprofile.png';
?>
<img src="uploads/<?php printf('%s/%s', htmlspecialchars($username), $img) ?>">
You could use http://us3.php.net/file_exists to check if the image file is there.
Another alternative is - assuming you keep your user info in a database - have a column with the image name. Since you have to retrieve info from your user table anyway, check to see if that column is NULL or blank. If it is, the user has not uploaded an image yet.
Then, in the page you display the user photo, you might have code something like this:
$userPhoto = ($photoName)? $photoName : 'placeholder';
echo '<img src="uploads/'.$userPhoto.'.png" />
Assuming the filepaths are correct, here's what you do...
<?php $filename = "uploads/".$username;
$imgSrc = file_exists($filename) ? $filename : "uploads/noprofile.png"; ?>
<img src=<?php echo $imgSrc?>
Use onerror attribute in img tag
<img onerror="this.src= 'img/No_image_available.png';" src="<?php echo $row['column_name ']; ?>" />

displaying an image saved as a Blob in a MySql DB

Im creating a blog and the little bit of code below is where the blog geets printed out.
I've got a blob saved in my mysql database and im trying to turn it back into an image.
the imageName, imageType, imageSize, imageContect all receive values when i run my code. The problem is that the imageContent variable displays a load of random characters rather then an image. it seems that the reason for this is the headers but i've no idea what to do. can anyone help me to recode the image. thanks
while($row = mysql_fetch_array($result))
{
echo "name ".$row['imageName'].'<BR>';
echo "type ".$row['imageType'].'<BR>';
echo "size ".$row['imageSize'].'<BR>';
echo '<B>'.$row['blogTitle'].'</B><br />';
echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=edit&blogID='.$row['blogID'].'>Edit</A> ';
echo '<A HREF = http://www.alcaeos.com/blog/displayblogProcess.php?mode=delete&blogID='.$row['blogID'].'>Delete</A><BR />';
echo $row['blog'].'<br />';
header("Content-length:".$row['imageSize']);
header("Content-type:.".$row['imageType']);
header("Content-Disposition: attachment; filename=".$row['imageName']);
echo $row['imageContent'].'---------<br /><br /><br />';
}
Here is a fairly simple example of displaying an image stored as a blob.
<?php
require_once ('./includes/db.inc.php'); // Connect to the db.
//let the browser know its an image
header("Content-type: image/jpeg");
// Make the query.
$query = "SELECT thumbnail FROM items where item_id=" . $_GET['item_id'];
$result = #mysql_query ($query);
if ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo $row['thumbnail'];
}
mysql_close(); // Close the database connection.
?>
Skip the Content-length and Content-disposition, just use Content-type and set it to a valid MIME type.
Assuming they're JPEGs, you'd do this:
header( 'Content-type: image/jpeg' );
Check out this link too

php - Decide whether an image exist and if it does show it

I have a code that shows a different image depending where on the page I am, but some places don't have an image so it displays a "no image" icon. I want to add a condition that checks if there really is an image in the given path and if returns false don't do anything. I have no idea how to do it.
This is the original code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
}
?>
What I need to know is which function do I use to get a true/false of that image path. Thanks
Use file_exists
$image_path = 'Imagenes/grupos/' . substr(get_search_query(), 1) . '.jpg';
if (file_exists($image_path)) {
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
} else {
echo "No image";
}
http://php.net/manual/en/function.file-exists.php
You can use file_exists

Categories