Blob to image in browser - php

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

Related

jpeg rotation using imagerotate

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();

PHP Image from URL

I have the following url which returns an image in the body, How would I assign that image to a variable so that I could save it or use it within my code?
https://media.licdn.com/dms/image/C4E03AQGOM3p0fHNkwQ/profile-displayphoto-shrink_100_100/0?e=1556755200&v=beta&t=qeQFKYXpev2ZW3hmP1ODDPd3DYPWvl-GaUnPSZG-aQA
The following code returns noting:
<?php
$url = "https://media.licdn.com/dms/image/C4E03AQGOM3p0fHNkwQ/profile-displayphoto-shrink_100_100/0?e=1556755200&v=beta&t=qeQFKYXpev2ZW3hmP1ODDPd3DYPWvl-GaUnPSZG-aQA";
$image = file_get_contents($url);
echo $image;
?>
If you simply want to view an image from a URL, just put that url in the src attribute of an img element like this :
echo '<img src="' . $url . '"/>';
otherwise, if you have to view it from the fetched data of that url, then your question have been answered here:
php: recreate and display an image from binary data (specifically #Krab's answer)

Creating Image file from image stored in mssql as type Image

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.........");

How to display Binary image in mpdf

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+1S5muv2b2X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m7xTglAPua9XqlNorZFSS3+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;');

PHP: Image retrieval from MySQL Blob directly into <img> tag

I've stored my Images into (Medium) BLOB fields and want to retrieve them embedded within my PHP-generated web pages.
When I test retrieving the stored images using
header('Content-type: ' . $image['mime_type']);
echo $image['file_data'];
everything looks just fine.
However, I have not yet found a way to retrieve the image(s) cleanly into the middle of my documents. For example, using
$image = $row['file_data'];
echo '<img src="data:image/jpeg;base64,'.$image['file_data'].'" alt="photo"><br>';
...or...
$im = imageCreateFromString($image);
I just wind up with a bunch of hexadecimal garbage on screen.
I intitially stored the Images using:
ob_start();
imagejpeg($resizedImage, null, 100);
$content = ob_get_contents();
ob_end_clean();
$sql = sprintf(
"insert into images (filename, mime_type, file_size, file_data, event_id)
values ('%s', '%s', %d, '%s',%d)",
mysql_real_escape_string($fileName),
mysql_real_escape_string($mimeType),
$imageSize,
mysql_real_escape_string($content),
$eventID
);
$result = $cn->query($sql);
Does anyone PLEASE have a working code snippet to successfully display the stored .jpg mid-file in the PHP output?
echo '<img src="data:image/jpeg;base64,'.base64_encode($image['file_data']).'" alt="photo"><br>';
However, remember that old IE versions do not support this kind of inline images! Besides that, the browser cannot cache such an image except together with its containing HTML page.
You should create some sort of "image server". You're already close to that.
For example, create something like image.php that will get a image name and will generate it on the fly.
So, for example, say you want to get somePic.jpg image. You can get it through:
image.php?name=somePic.jpg
<?php
header('Content-type: ' . $image['mime_type']);
echo $image['file_data'];
?>
Your tag:
<img src='image.php?name=somePic.jpg' />
Or more general:
echo "<img src='image.php?name={$image['filename']}' />"
Why not just call your test page image.php, then have it called from the browser on the rendered page:
<img src="image.php?imageid=123" alt="photo" />

Categories