I have a database containing movies Name, their description and their cover picture. The cover picture field type is as blob and the problem is that I can't retrieve it from the database. I want to display the movie name along their cover picture beside them... How to do it.. Here is my code..
<?php
include ("php/connection.php");
$ID = $_GET['id'];
$listmovies = "SELECT * FROM movies where Genres LIKE '%$ID%'";
$result = mysql_query($listmovies);
while ( $row = mysql_fetch_assoc($result) ) {
?>
<table border="1" width="100%">
<tr>
<td rowspan="2" width="90" height="120">
<?php
// set the header for the image
header("Content-type: image/jpeg");
echo $row['Image'];
?> </td>
<td width="200" height="10">
<?php
echo $row['Title'];
?></td>
</tr>
<tr>
<td width="200" height="110">More Detail</td>
</tr>
<?php } ?> </table>
I just want to display The Imgaes beside the title of the movie?
Yes it won't display because any output above header would always generate error ... you need to have a different page to output your image or include it has base64 image
Remove
header("Content-type: image/jpeg");
echo $row['Image'];
And add this :
printf("<img src=\"data:image/jpeg;base64,%s\" />",base64_encode($row['Image']));
^--- Note this is only for jpeg images
I suggest something like that:
Make a new php file called for example image.php; That file will replace physical image file.
In that file you put the code from your post plus some logic to get image data from database;
In parent template(php file maybe) you put code for image:
<img src="image.php?id_movie=<?php echo $id_movie; ?>" width ="200" height="200" /> More Detail
In image.php i suggest to be careful at spaces outside php tags (at the beniging and at the end).
Also to image.php you need to give id of the movie to know what image to load from database.
Related
I have a database which contain blob images. I want to display them in a webpage. I'm using the following php code to get the image. But it didn't work properly.
<?php
while($row=mysql_fetch_array($results))
{?>
<tr>
<td><img src="<?php echo $row["image"]?>" height="200" width="250"></td>
</tr>
<?php
}?>
With this code I'm getting a webpage like this.
Where I have to do the correction to my code.
try to convert it to base64:
<img src="data:image/jpeg;base64,<?php echo base64_encode($row['image']); ?>" height="200" width="250">
Note that you should also store the image type in your DB, so you can dynamically change "data:image/jpeg".
I am trying to display image from a blob field of a MySQL table. Looks like I have some sort of error in the following line. As soon as I put "header("Content-type: image/jpeg")" things get messed up and instead of displaying webpage, all source code of the page is displayed.
Please let me know how to correct.
<div class="image" align="left">
<a href="<?php header("Content-type: image/jpeg"); echo $rec['image']; ?>">
<img src="<?php echo $rec['image']; ?>" width="150" border="0"/>
</a>
</div><!-- image -->
You normally don't put the actual image contents in the src= attribute of the image tag. Instead, you point to the URL of an image file.
(There are ways to include the image source directly in the HTML, but it doesn't work consistantly with all browsers, and you still won't have your <a> link working properly.
Instead, the best way to do this is to create a separate PHP file to serve the image.
Your HTML:
<div class="image" align="left">
<img src="myimage.php?key=<?php echo($key) ?>" width="150" border="0"/>
</div><!-- image -->
myimage.php:
<?php
header("Content-type: image/jpeg");
$key = $_GET['key'];
// todo: load image for $key from database
echo $rec['image'];
You're trying to put the image data inline inside the content. The only feasible way to do this is via a Data URI data URI. Something like:
<img src="data:image/jpeg;base64,<?= base64_encode($rec['image']) ?>" width="150" border="0" />
However, what you probably want to do is put it into a separate script. So your HTML would be:
<img src="showimage.php?id=XXX" width="150" border="0" />
And your showimage.php script would be:
<?php
// Get $rec from database based on the $_GET['id']
header('Content-Type: image/jpeg');
echo $rec['image'];
?>
I've done something like that retrieving blob from my database in another way that you may find useful, here is the code example.. see if it suits your needs and if you needed anymore help let me know.
while ($row = mysql_fetch_array($hc_query2)) {
$title = $row['title'];
$text = $row['text'];
$image = $row ['image'];
$output ='<div class="HCInstance"><img src="data:image/jpeg;base64,' . base64_encode($image) . '" alt="High Council" width="100px" height="100px"/>
<div class="HCHeader"><h2>'.$title.'</h2></div><br/><div class="HCDetails"><p>'.$text.'</p></div></div>';
echo $output;
}
The images are stored in my folder and not in database. When i click on one of the images, it will lead me to see more info on the next page. This would means that i can see the image with a bigger size.
How do I do this with less php pages created? Below are snippets of my codes
<table>
<tr>
<th class="timgB"><h4><img src="city/GVcementMixture.jpg" style="vertical-align: text-bottom;" title="Cement Mixture"/> Cement Mixture </a></h4></th>
</tr>
</table>
You could keep it all in one form/page with a big if/else or switch/case statement to evaluate the selected image and show only info pertaining to the selected image such as the full size image or a sub-set of child images. But it would be best presented in jQuery as #Razor said.
<form ...>
<button type=submit name=imgID value=1>
<button type=submit name=imgID value=2>
...
<?php
if (isset($_POST['imgID'])):
// Show info related to selected image
...
you can do it like this. use class="small for make image size small and class="big-img" to big size image. the ?img=img23.jpg in your link decide which image should be shown with big size. or if you use thumbnail of image as small size and want to show a full size only when some one click on thumbnail then update the link in upper div (class="big-img") or leave comment.
//it will work as you want
<body>
<div class = "big-img"> <!-- set class to show big size image-->
<?php if(isset($_GET['img'])): ?>
<img src="city/.<?php echo $_GET['img']; ?>" alt="">
<?php endif; ?>
</div>
<table>
<tr><!-- set class to show small size image-->
<td class = "small"><img src="city/img23.jpg" alt=""></td>
<td class = "small"><img src="city/img24.jpg" alt=""></td>
<td class = "small"><img src="city/img25.jpg" alt=""></td>
</tr>
</table>
</body>
I can't seem to get my image to display properly. Previously, I have used the following code snippet and it worked perfectly.
catalog.php (worked perfectly):
<p class="image">
<a href="synopsis.php?id=<?php echo $row['id']; ?>">
<img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" />
</a>
</p>
synopsis.php (not displaying image at all):
<?php
$id = $_GET['id'];
...?>
<p class="image">
<img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>" width="250" height="400" />
<?php echo $row['synopsis']; ?>
</p>
where getimage.php:
<?php
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb", $link);
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['dvdimage_path']);
?>
Any idea why can't I display this image?
EDIT 1:
So after debugging, I got an error message:
Undefined index: id in C:\xampp\htdocs\synopsis.php on line 106
so i went to add the following code into the php code just before echo $row['id']:
<p>getImage.php?id=<?php error_reporting(0); echo $row['id']; ?></p>
However,
the paragraph i got was just getImage.php?id=.
Then, i went into synopsis.php -> <img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>
and changed that into:
<img border="0" class="floatleft" src="getImage.php?id=2">
Again, same problem happens, where i can't get the specific image out.
I suspect something is wrong with my getimage.php file. However, this getimage.php file has been working fine for other pages when i use the snippet.
My requirements are very simple:
In catalog.php, i populate images and text from dvd database using a while loop. Then, each of these images has got their specific primary ID. when i click the the images, they will go to the link: synopsis.php?id="primaryid" Then, using this "primaryid" i should be able use getimage.php?"primaryid" to generate an image on synopsis.php page.
EDIT 2:
actually, i made a syntax error somewhere. So this line:
<img border="0" class="floatleft" src="getImage.php?id=2">
is working perfectly, this means the fault lies in somewhere that i cant echo 'id' out correctly.
EDIT 3:
I have included the links to the relevant source code:
catalog.php
synopsis.php
getimage.php
sortmenu.css
style.css
database in xml format
Questions to ask yourself:
Is it really required that you use a php script to mimic the image? If not, just use the image path.
Is there any output before the header(); function in the getimage.php file? Even just a space before the
Is the image actually a JPEG?
Are there any errors coming up when you go to getimage.php?id=ID in your browser?
In synopsis.php you are getting the id from the querystring but then trying to use a database value. I cant see all of your code so im posting solutions to cover two scenarios
src="getImage.php?id=<?php echo $id; ?>"
or
src="getImage.php?id=<?php echo $row[$id]; ?>"
i have images in my blob field in database i use blow colde for displaying image but
it does not show any pic , someone could tell me where is my wrong?
this is my model:
public function getListUser() {
$select = $this->select()
->order('lastname DESC');
$adapter = new Zend_Paginator_Adapter_DbTableSelect ($select);
return $adapter;
}
this my controller:
$userModel = new Admin_Model_User();
$adapter = $userModel->getListUser();
$paginator = new Zend_Paginator ($adapter);
$paginator->setItemCountPerPage(1);
$page = $this->_request->getParam('page', 1);
$paginator->setCurrentPageNumber($page);
$this->view->paginator = $paginator;
this my view code:
<td style="width: 20%;"> <?php
echo $this->lastname ?> </td>
<td style="width: 20%;"><?php header("Content-type: image/gif"); print $this->image; ?></td>
This is not how you can display your image.
Your code should look something like this.
<td style="width: 20%;"> <?php
echo $this->lastname ?> </td>
<td style="width: 20%;"><img src="/link-to-php-that-shows-image.php?id=123" /></td>
Now, in the php file for example (link-to-php-that-shows-image.php). You get the image field from the database and display it. Example..
<?php
$image = //get image from database, based on id etc.
header("Content-type: image/gif");
print $image;
?>
You cannot do this line of code.
<td style="width: 20%;">
<?php header("Content-type: image/gif"); print $this->image;?>
</td>
It's because, <td style="width: 20%;"> is html element which gets printed before image is outputed and headers are send to output image, so you get error
Error:: Cannot modify header information - headers already sent by ..
But you don't get this error in zend framework because the error display might have been turned off, so have a look at your application.ini file for setting.
The best way to do this would be to store image in folders and store the filename or filepath in database and echo out something like
<td style="width: 20%;">
<img src="<?php echo $imagefullpath; ?>"/>
</td>
But if you still want to store image in database then, you can use ajax to load image. For further assistance, please comment.
For the beginners there is a very handy rule:
PHP can't do anything beside plain HTML can.
Now answer yourself a question:
would you see anything if you paste image file contents directly into HTML code?
And then another one
How do images being displayed in HTML?
And a final one:
are you sure you want to store images in the blob field in database?