I am saving image as binary to database. I want to display this image in MPDF. Normal image display is working properly but binary image can not displaying The code I have used is
<td><img style="vertical-align: top" src="'.$studphoto.'" width="80" /></td>
The code I have used while inserting to database is
$photo_get = file_get_contents($_FILES["photo"]["tmp_name"]);
list($width, $height, $image_type) = getimagesize($_FILES["photo"]["tmp_name"]); /* Here gets image type */
$mime_photo = image_type_to_mime_type($image_type); /* This function returns the mime type of the file when the image type got from getimagesize() is passed */
$photo_en = base64_encode($photo_get);
$photo = "data:" . $mime_photo . ";base64," . $photo_en;
The data getting from DB is data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAGUASgMBIgACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAGAQIEBQcDAAj/xAA6EAACAQMDAQUGAwUJAQAAAAABAgMABBEFEiExBhNBUWEHFCJxgZEjMlIVQlOSoSRDVGKCwdHh8Rb/xAAaAQACAwEBAAAAAAAAAAAAAAAEBQECAwAG/8QAJREAAgICAQQCAwEBAAAAAAAAAAECAwQREiExQWEFE0JRcbEi/9oADAMBAAIRAxEAPwCl0m51GzhuovdBL3wwpP7tcoLDVW02a0Fkz9624tg5Fb4lnbJ+WCMf6RXQRovRQPpUbO0Yeuh9pLq4spYtMb+zJtX8Pg/Oh7tQLrSJZ7LVXSOedhI8arkr5fKiX2k+1S5muv2b2XuZrZYHZJ51UAuRx8J8utZVcTT3dxJcXcrzTOdzO5LEnzzVji/0HV7bSNRivGVp9hB2qMVoVx2r0vWrj3s3EcLy8d2zdDWNYJPPTypcgcD58VDWzKytTWjbwAQGUgg9CKXFZ12P7Ufs8+537M1u5+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4OxzyPIkdflTNO1GCG7aCF0lfORhuceWDUue3naNRafhoeSuwED5ZqJdBbTdKxBl6kkAc/Kq7TWgh6OHaFhcTNCRuRiAw8wfCtCtTFpXZ1dwGWXKrjGSegoD7OW7axrW2KRCYB3jkjIyegozngitm726lN3cr+SMdB9KLoahH2L8qfKXFHexmeKWDfkMy5cCrr3yP1/lNVelq6gz3KYlk6L+kVZb18v6UVVqMdMW3Wbl0M8UV0Va8o5r00iQQtLKQFUZOashIk5NRXdlfr8aPZYkx+YYB8aEZLK6t5t9nIVI5C54pdd1O5vNVs0X8jSAqPJR1owSyjlghZcZHn1H1oG+1J78HsMPBlRV9c318+tgqNd1oYh2kMPJDzXU22p6lhr5+7j67R1NGsVkAucKSeOafPEiptxuOKFd68IMVfXWzPdX1e87L26SaUywtKe7c7c+tX/ss7WX9/f8AdauqOk5xFJswcih7tXbJrN4lrESI4XzIw6D0rtDGbd4RbfhiHGwr4YopWJQX7NKfj5XuUn21pf03qSMMAV6g/el2L+k0BaL26EcKwaqrblx+Kg6j1q/HazRiAffl5or7YdxLdgX1S4uDAe716zt8iNu+fyXp96oNR1e4veJGCRZztHT61DeMluAcelQLq0nvJFjJMcPiM8tQztc+jfQfY3w1GH/1CPKXsn2KC61m0lR0MAym4c9f/BWjWcC7QFYECsz0uGSzEYjxhByp/rRVo2sd5cvbv8A2Z8sUPZDmung1yYSi1J/l/oWbo4wSxUADqTiqDV9XjCtFbsHY8ZXwqj1W7e+nIZm7lThUz19arXs9s4kikdXxggHg/Oso1ryEVfH61Kb36JO0Dp1NOAwPWmgsv5lB9VrnL3kilVG0HqSea0Ga6LSEU78t4N0+VLsroFwOOgr2a4nihi8rzTHGHX516vVBD7HSRARnxpkYIm7xTglAPua9XqlNorZFSS3+yVtCcDw4zSUleqSyE8aXGTSV6oLC03Apa9UM4//Z
Please somebody help me to solve this :-(
Try like
<img src="<?php echo $studphoto;?>" />
And try to add the header with
header('Content-Type: image/jpeg;');
Related
I am trying to access image which is saved as blob, and the type of column is image which is saved using c# in mssql
While retrieving image is showing correctly using c# code
While accessing using php the image is broken
code which i have tried
$image = imagecreatefromstring($stationDetail->cstatpic3);
ob_start();
imagejpeg($image);
$data = ob_get_contents();
ob_end_clean();
echo '<img src="data:image/jpeg;base64,' . base64_encode($data) . '" />';
exit;
also from this link
PHP display image BLOB from MySQL
I have a database which includes a table of jpeg photo data. This data has been sent to the database from an iPad app and can be displayed in a webpage using the following:
$photo_query = "SELECT photoID, photoData FROM tblPhotos;";
$resultPhotos = mysqli_query($connect, $photo_query);
while($rowPhotos = mysqli_fetch_array($resultPhotos)) { ?>
<div id="photo"> <?php
echo '<img src="data:image/jpeg;base64,' . base64_encode($row['photoData']) .'"/>';
</div>
}
This works fine and the image displays correctly.
I am now looking to add a simple tool to rotate this image. Below the image is a simple and when this is clicked the javascript function updatePhoto is called with the photoID:
<div onclick="updatePhoto('<?php echo $photoID; ?>')">Rotate</div>
JAVASCRIPT
function updatePhoto(photoID) {
$.post("photoChange.php", {
photoToChange: photoID
},
function(data, status){
document.getElementById('photo').innerHTML = "<img src='data:image/jpeg;base64, base64_encode(" + data + ")'/>";
});
}
PHOTOCHANGE.PHP
$photoID = $_POST['photoToChange');
$select_photo_query = "Select photoData From tblPhotos where photoID= '" . $photoID ."';";
$resultPhotos = mysqli_query($connect, $select_photo_query);
while($rowPhotos = mysqli_fetch_array($resultPhotos)) {
$source = $rowPhotos['photoData'];
}
$degrees=90;
$image = imagecreatefromstring ($source);
$rotate = imagerotate($image, $degrees, 0);
$finalImage = imagejpeg($rotate);
//step to convert jpeg back to binary needed?
echo $finalImage;
This all works in that the photoID is sent to photoChange.php, the source is retrieved from the database etc and data is sent back and placed into the page. But it does not display an image, just lots of data. I know I have a coding issue here but I am not sure exactly what.
I have tried removing all the rotation detail and simply echoing the $source unchanged and this does not replace the image with itself but with lines of data instead. So I wonder if there is an issue when posting data and echoing data back whether I need to stipulate a coding method being used?
Any assistance much appreciated.
Go to: http://php.net/imagejpeg
It returns a boolean value and directly dumps the image to the output.
$finalImage = imagejpeg($rotate);
//step to convert jpeg back to binary needed?
echo $finalImage;
Instead of this, add the content type and let the image render straight (you don't need echo, echo is implicit on imagejpeg()):
header('Content-Type: image/jpeg');
imagejpeg($rotate);
More problems:
You have <div id="photo"> in a while loop, which makes your ID not unique, it can't work when you have more than 1 picture
Another problem:
You interpolate PHP in Javascript a way it won't work ever. Replace the line by this with fixed concatenation:
.innerHTML = "<img src='data:image/jpeg;base64, " + data + "'/>";
Put the base64 encode in the image creation step, we make use of output buffer there:
header('Content-Type: text/plain');
ob_start();
imagejpeg($rotate);
$binary = ob_get_contents();
echo base64_encode($binary);
ob_end();
I have images stored in mssql database . I want to retrieve the data via php and create image file . I have tried numerous possibilities but unable to find any correct solution
Image i have in following format stored in database
0xFFD8FFE000104A46494600010200000100010000FFDB00840001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010
I have tried
$row = mssql_fetch_assoc($result);
$db_img = $row['imgdata'];
$db_img = pack("H" . strlen($db_img), $db_img);
header("Content-Type: image/jpeg");
echo $db_img;
displays black image
Also i have tried
file_put_contents( 'test_image.jpg', $binary_data);
Not Working ....
Also tried ...
imagecreatefromstring("0xFFD8FFE000104A46494600010200000100010000FFDB00840001010101010101010101010101010101010101010101010101010101010101010101010101.........");
I try to generate php code in order to preview an image from a pdf file. Here is my code. There is an error but i cannot understand where it is. I would appreciate any help. Thank you.
<?php
//Get Menu Bar
include('navigate.php');
?>
<table cellspacing="4" width=100%><tr><?php if(!$vol){ ?><td bgcolor="eeeeee" width="50%" valign="top">
<h3 style=" text-shadow:#003">Latest News:</h3>
<?php
$latest=mysql_query("select * from articles ORDER BY id DESC LIMIT 1", $link);
while ($article = mysql_fetch_array($latest)) {
$thevolume= $article['volume'];
$title= $article['title'];
$author= $article['author_main'];
$abstract= $article['abstract'];
$pdf= $article['pdf'];
$im = new imagick('pdf');
$im->setImageFormat('jpg');
?>
<a href="<?php echo $pdf; ?> "PDF: ?php echo "$title - $author"; ?>
<img src="<?php echo $im; ?> " WIDTH="98%" border="1" title="<?php echo "$title - $author"; ?> " caption="Click here to open PDF" />
</a>
You must to pass as parameter a path to PDF file, not only the string 'pdf': http://php.net/imagick.construct
If $article['pdf'] contains it you must to use:
$im = new imagick(realpath($pdf) . '[0]');
$im still being a resource that contains a object instance of imagick, and not the image or pdf document itself, so you must to do this to inline image in HTML (if you want to use JPEG, I would use PNG):
<img src="data:image/jpg;base64,<?php
echo base64_encode($im->getImageBlob());
?> " WIDTH="98%" border="1" title="<?php
echo htmlspecialchars("$title - $author");
?> " caption="Click here to open PDF" />
Note the usage of htmlspecialchars ( http://php.net/htmlspecialchars ) to output strings to navigator and getImageBlob ( http://php.net/imagick.getimageblob ) to get image data (if available) and finally I will encode it in base64 to inline it in HTML document.
If you want to link to a cached version of image you must to save it in a temporary file or create another PHP script to generate the image from PDF file.
Best regards,
Edit 1: How to create a JPEG thumbnail, from PHP manual:
You should read this: http://php.net/imagick.setimageformat#89210
This example will help you:
// convert to JPEG
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(60);
$im->setImageFormat('jpeg');
Probably you will wish to reduce image size:
$im->resizeImage(290, 375, imagick::FILTER_LANCZOS, 1);
And remember to import the page number (beginning with 0, not 1) adding '[0]' to file name as edited.
Good luck!
I tried to link stored image(blob data) inside MySQL DB to PHP but only thing I achieved was whole page of characters. This is what I did:
$query = "SELECT image FROM uploads WHERE id = {$id}";
$image_array = mysql_query($query, $connection);
$row = mysql_fetch_array($image_array);
$image = $row['image'];
echo $image;
// echo base64_decode($content);
This displays raw data how it is stored. I would like to link it into HTML tag or atleast display it on page, where I display a lot another stuff to so
header('Content-type: image/png');
is not solution for me.
Any advice?
To show the image correctly you just need to put one header with Content-type: image/png before echo
header("Content-type: image/png");
echo (base64_decode($row['image']));
If you want to place instead in an image tag you just need to use this code
echo '<img src="data:image/png;base64,' . $row['image'] . '" />';
You need to give proper headers, otherwise the page wont know what you're sending it.
// define results into variables
$name=mysql_result($result,0,"file_name");
$size=mysql_result($result,0,"file_size");
$type=mysql_result($result,0,"file_type");
$content=mysql_result($result,0,"file_stream");
// give our picture the proper headers...otherwise our page will be confused
header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;
You should use something that is called 'Inline image'. Use this code to display the image on a page, asssuming the $image variable contains base64 encoded data:
<img src="data:image/png;base64,<?php echo $image; ?>" alt="Larry" />
Source: https://en.wikipedia.org/wiki/Data_URI_scheme