/*this code is working(echoing image) on localhost but not displaying on live site */
PLEASE HELP,,,,
<?php echo "<img src=geti/tipb_geti.php?id=$lastid width=200px height=180px >";
/*this below php block is tipb_geti.php file*/
<?php
include("connect.php");
$id=#addslashes($_REQUEST['id']);
$image_query=#mysql_query("select image from tipuranibasti where id=$id");
$image_row=#mysql_fetch_assoc($image_query);
$image=$image_row['image'];
header("content-type: image/jpeg");
echo $image;
?>
What's the memory limit on your live server? You're making TWO copies of the image data here:
$image=$image_row['image'];
which is utterly pointless. You could simply have
echo $image_row['image'];
instead and save yourself the extra wasteful/pointless copy operation.
Check if your characterencodigs. The livedb might have got UTF8 ancoding or the server has, but you not, if not, what is the output? Comment out header("content-type: image/jpeg"); and check if you get the same things live and local.
oh and i would prefer:
<?php print'<img src="geti/tipb_geti.php?id=' . $lastid . '" width="200px" height="180px" />';
makes your html/xhtml a bit more well formatted and print'test'; is slightly faster than print"test"; since "$var" will output the content of $var and '$var' wont, this is a little less overheat. But don't think you gain hyperspeed from that :P You won'T notice the bit more performance, I only think it provides a better code.
Related
what I need to do in my website is to show a list of jpg images stored in a Mysql database using Blob type (I don't want to use any other way). but instead of the image I got strings like this
����$ExifMM*bj(1r2��i��SI'SI'Adobe Photoshop CS6 (Windows)2014:07:31 20:02:56�����&(."�HH����XICC_PROFILEHLinomntrRGB XYZ � 1acspMSFTIEC sRGB���-HP cprtP3desc�lwtpt�bkptrXYZgXYZ,bXYZ#dmndTpdmdd��vuedL�view�$lumi�meas$tech0rTRC<�gTRC<�bTRC<�textCopyright (c) 1998 Hewlett-Packard CompanydescsRGB IEC61966-2.1sRGB IEC61966-2.1XYZ �Q�XYZ XYZ o�8��XYZ b����XYZ $����descIEC http://www.iec.chIEC http://www.iec.chdesc.IEC 61966-2.1 Default RGB colour space - sRGB.IEC 61966-2.1 Default RGB colour space - sRGBdesc,Reference Viewing Condition in IEC61966-2.1,Reference Viewing Condition in IEC61966-2.1view��_.���\�XYZ L.....
This is the code...
<?php
// Create connection
$con=mysqli_connect("erossenses#localhost","erossenses","-----","my_erossenses");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Racconti");
echo "<br>";
echo "<font style=\"font-family:Verdana; font-size:15px; color:maroon\"><b><u>Titolo</u></b></font>";
echo "<br>";
echo "<br>";
header('Content-type: image/jpeg');
while($row = mysqli_fetch_array($result)) {
echo $row['Immagine'];
echo "<br>";
}
mysqli_close($con);
?>
Where am I wrong?
You can't generate multiple images with php at once. you can only do them one at a time, so you'd have to do it via ajax. this isn't advisable at all anyway. you're best to learn about ajax and file uploads and how to secure them than wasting any more time on this. Storing images in mysql is suicide, especially for a beginner. You are on the wrong path and learning dangerous habits already.
to advance your skills, read the manual http://php.net/ or better yet, get a quality php book.
Try the below code
header("Content-length: $fileSize"); header("Content-type: $fileType"); header("Content-disposition: download; filename=$fileName"); echo $fileData;
Hope file size,name, type, and data stored in table.
The problem is you're echoing a lot of stuff that's not an image. If you set header('Content-type: image/jpeg'); the browser will expect to get only the image data (for just one image).
If you really must use images from the database, a solution would be to create an additional file, let's say image.php?id=1 which gets the id of the image and displays only that specific image. You can use this file in the src of an img tag.
I have uploaded an Image in my database. The datatype is BLOB. Now I want to view this image in my browser. I have written the following code but its not generating the image. Kindly check it.
Thanks
<?php
include 'connect.php';
//$id= $_GET['product_id'];
$query_images = "SELECT image FROM product_images WHERE product_id=121";
if (!$query_images_result = mysql_query($query_images))
{
echo mysql_error();
}
else
{
$fetch_images = mysql_fetch_array($query_images_result);
$print_images = $fetch_images['image'];
header('Content-type:image/jpeg');
echo $print_images;
}
?>
File 2
<body>
<img src='single_product_image_show.php' alt="image" />
</body>
maybe something like this? You might wanna use base64 encoding to build in the image
FYI: NOT TESTED.
$sql = "SELECT `image` FROM `product_images` WHERE `product_id`=121 LIMIT 1";
$query_images_result = mysql_query($sql);
if (!$query_images_result)
{
echo mysql_error();
}
else
{
list($print_images) = mysql_fetch_array($query_images_result);
$base64source = "data:image/jpeg;base64," . base64_encode($print_images);
echo '<img src="'.$base64source.'" alt="" />';
}
There are multiple possible reasons why this could happen. First try your script directly without "header('Content-type:image/jpeg');" so you can see what it actually returns. Like http://[youhostname]/single_product_image_show.php I assume you will see some errors.
Some best practices to serve images from php:
If you can remove the end php tag (?>). You don't actually need it and you can avoid issues when you have whitespace character after the close tag which will be written to the response and so results as a corrupt image.
You should specify the Content-Length header as well. It's not mandatory but the browser
will know how big the file it has to download.
Specify some cache control headers (https://en.wikipedia.org/wiki/List_of_HTTP_header_fields) so browsers can cache your image so it won't result a full page load and mysql query every time the image appears on the site.
You can store ETag and/or Last-modified in the database which you can retrieve and put in the headers.
I'm having kind of controller http://samplesite.com/application/getimage written in php. It is a simple script that responds to GET with some parameters. According to these parameters I find an image in database (image is stored there as a BLOB) and I return it as follows:
header("Content-type: image/jpeg");
echo $image;
$image is a BLOB of image obtained from database.
When I open this URL http://samplesite.com/application/getimage/?id=200 my web browser displays an image.
Now I want to display this image inside a table in another php script. I want to perform it like
echo "<img src="http://mysite.com/application/getimage/?id=200" ...some params here... />";
but
this approach does not work. It displays no image or makes other problems.
What approach should I choose to make this work? I'm new to php normally work with Java EE. Thanks very much.
EDIT:
Of course there should be inside the echo \" and not only ". But this approach that I wrote here works fine. Problem was in syntax, I had one extra ).
Passing a PHP file AS an image (not recommended)
Your issue is that you haven't escaped speech marks, replace:
echo "<img src="http://mysite.com/application/getimage/?id=200" ...some params here... />";
With:
echo "<img src='http://mysite.com/application/getimage/?id=200' ...some params here... />";
or:
echo "<img src=\"http://mysite.com/application/getimage/?id=200\" ...some params here... />";
However, there is a far better way to do this:
Using image BLOBs properly
You're on the right track -- you can use an image BLOB instead of a file to put an image inline, but there's a little more work to it. First, you need to base64_encode the blob before you pass it. You should omit the header as you're passing text, not an image:
echo base64_encode($image);
Next, you need to set up the image tag like this, declaring that you are using data, the mime type and finally including the output of your getimage script as declared by RFC2557:
<img src="data:image/jpeg;base64,<?php include('http://mysite.com/application/getimage/?id=200'); ?>"/>
That should fix the issue.
I have a .php file that's supposed to load an image for display in an img tag(i.e., <img src="the_file.php?which=0"/>). It looks like this:
<?php
ob_clean();
header("Content-type: image/png");
include_once("util.php");
//Do a simple calculation to get $name from (int)$_GET["which"];
$im = imagecreatefrompng("protected_directory/".$name.".png");
imagepng($im,NULL,0,NULL);
imagedestroy($im);
ob_end_flush();
?>
It works correctly, but the image loads substantially slower than just loading it directly(i.e. <img src="protected_directory/the_name.png"/>, where "the_name" was calculated the same way as in the PHP file, but I can't just do this because the protected_directory isn't world readable).
My question is, why is this suddenly so much slower? It's not a large image, but nor is it terribly small.
If you're just displaying an existing file, use readfile() to output it to the browser. There's no need to go through all the overhead of creating an editable GD object for this.
imagepng is known to be slow, if you need to output images with a PHP script, use code like this:
$filename = md5(time() . mk_rand());
imagepng($im, $filename);
echo file_get_contents($filename);
As another answer, I figured out that you can use the third parameter to compress the image (PNG uses zlib). Setting it to 9 works about as well as the other solutions.
I've create a table where I've saved images through "BLOB". I need to show those images along with other items. But I don't know how to show those images together in the same page.
Here's my php code that displays other things in form of a table. Similarily, I wanted to display images accordingly. Any help?
<?php
$display_query = mysql_query("SELECT * FROM eportal");
echo "<table id='pageTable'><thead><tr><th>Item code</th><th>Description</th><th>Cost</th></tr></thead>";
echo "<tbody>";
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td></tr>";
}
echo "</tbody>";
echo "</table>";
mysql_close($connection);
?>
Saving images to the DB is not a good idea but if You think You need to it this way, then You can retrieve the data from DB table, encode it to base64 (http://php.net/base64_encode) and then in HTML print it in this way:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
Using PHP You would write:
echo '<img src="data:'.$image_mime_type.';base64,'.base64_encode($image_data_from_db).'" alt="My image alt" />';
As other people mentioned, storing the images in the database is usually a bad idea.
Images are not transmitted in the same HTTP response with another page data.
To show images from the database, you would need to implement a script which, given the item id, would read the field and send the image's binary data; and provide the path to the script in your form's <img src="">:
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td><td>";
print "<img src=\"image.php?id=".$row['id']."\"></td></tr>";
}
image.php is another script which outputs the value of image_blob given the eportal.id. You would also need to provide correct size and mime type in the headers.
You better just store the images in a file (accessible by the web server) and store the path fo the file in the database.
Read the Blob data and write it into the file with header type image.. and try to print it, It should display the image file.
And yes saving image or any file in DB is really a bad habit as you are increasing DB size and it slowdown the performance also.. I suggest you to just try to convert you Blob into Image but don't apply in your work. Just save the image at desired location and keep its location path into DB to fetch and save next time.
The debate of storing blobs versus storing a path to the image file on disk has been debated over and over again. Microsoft provides a research paper comparing the pros and cons of each here. With that said, to display a blob as an image you need to make a call to a separate page and output header information that tells the browser what type of image is stored.
For example:
connectToDatabase();
$sql = "SELECT image_blob FROM eportal;";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['image_blob'];
$db->close();
In case you still want to save your images in database, you will need second script which will get those images from database and pass them to browser with correct headers.
header("Content-type: image/jpeg");
#
# Replace this with database read:
# readfile('myimage.jpg');
Also, you will need to store what kind of image u use. There will be different header for JPEG, GIF or PNG file.