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();
Related
So in PHPmailer you can add a 64base coded image to the mail's body if you use addStringEmbeddedImage and in the data part you base64_decode the image. Ex:
$mail->addStringEmbeddedImage(base64_decode($str), "img_".$i, "img_".$i,"base64",$type);
I get the images from my text editor in base64 form and then replace the image with the respective container
<img src=\"cid:img_".$i."\" ".$height. " " .$width. ">"
And it works fine but for some reason, as soon as I add an image more than one time, it doesn't matter if its the very next image or first and last one, the mail will just save(?) the first image. Is not that it doesn't show it because in the original mail you can see that there's only 1 container specified in it.
--boundary
Content-Type: image/png; name="img_1"
Content-Transfer-Encoding: base64
Content-ID: <img_1>
Content-Disposition: inline; filename="img_1"
As you can see, each image has a different id (which is the order they are in the email) and the only way it can tell it is the same image is by the decoded base64 image so i really dont know why this only happens when its the same image. I could add img1----img2----img1----img3 and it will show img1 ---- img2---- |emptycontainer|---- img3
Edit: So for reference, this is the while I use to extract all the images
//$body of the mail, all the images i want to replace have that alt"" at the start
while(strstr($body, "<img alt")){
$i++;
$start= strpos($body, "<img alt");
$end= strpos($body,">",$start);
$str = substr($body,$start,$end-$start);
$height = "";
$width = "";
if (strstr($str, "height:")){
$ini = strpos($str, "height:")+7;
$fin = strpos($str,";",$ini);
$height = "height= \"".substr($str,$ini,$fin-$ini-2)."\"";
}
if (strstr($str, "width:")){
$ini = strpos($str, "width:")+6;
$fin = strpos($str,"\"",$ini);
$width = "width= \"".substr($str,$ini,$fin-$ini-2)."\"";
}
//here i replace the whole image with a container
$body= substr_replace($body, "<img src=\"cid:img_".$i."\" ".$height. " " .$width. ">", $start,$end-$start+1);
//get the type after data:
$start= strpos($str, "data:");
$end= strpos($str,";",$start);
$type= substr($str, $start+5, $end-$start-5);
//and this is where i get the base64 string
$start= strpos($str, "base64,");
$end= strpos($str,"\"",$start);
$str = substr($str, $start+7, $end-$start-7);
$mail->addStringEmbeddedImage(base64_decode($str), "img_".$i, "img_".$i,"base64",$type);
}
I appreciate any help and thanks for reading
I can see why you would use the same image more than one in a message, but why would you attach the same image more than once to achieve that? Much of the point of cid values is that they allow you to reference the same image from multiple places, so if say you have a logo image that appears in both header and footer, you can attach it once and reference it from both places, reducing message size. I suspect your issue is that you're trying to work around this and failing by attaching the same image data with two cid values, but PHPMailer is noticing the data is the same and not doing the second attachment, leaving your second cid pointing at nothing. You can fix it by using the same cid value for both image tags.
This should have the problem you mention:
$img = base64_decode($str);
$mail->addStringEmbeddedImage($img, "img_1", "img_1", "base64", $type);
$mail->addStringEmbeddedImage($img, "img_2", "img_2", "base64", $type);
<img src="cid:img_1">
<img src="cid:img_2">
This should work:
$img = base64_decode($str);
$mail->addStringEmbeddedImage($img, "img_1", "img_1", "base64", $type);
<img src="cid:img_1">
<img src="cid:img_1">
The code calculates an SHA256 hash of the content and uses that to check whether they are the same.
$bin_string = file_get_contents($_FILES["file"]["name"]); // image reading
$hex_string = base64_encode($bin_string); // encoded image
Saving into database and then retrieving back...
$decoded= base64_decode($message); // decoded image....
In HTML:
<?php header("Content-Length: " . strlen($decoded));
header('Content-Type: image/png'); ?>
<?php echo $decoded ?>
But this is not working.... what else is needed?
Here is the output of base64_encode($bin_string) :
/9j/4AAQSkZJRgABAgEASABIAAD/7Q1oUGhvdG9zaG9wIDMuMAA4QklNA+0KUmVzb2x1dGlvbgAAAAAQAEgAAAABAAEASAAAAAEAAThCSU0EDRhGWCBHbG9iYWwgTGlnaHRpbmcgQW5nbGUAAAAABAAAAHg4QklNBBkSRlggR2xvYmFsIEFsdGl0dWRlAAAAAAQAAAAeOEJJTQPzC1ByaW50IEZsYWdzAAAACQAAAAAAAAAAAQA4QklNBAoOQ29weXJpZ2h0IEZsYWcAAAAAAQAAOEJJTScQFEphcGFuZXNlIFByaW50IEZsYWdzAAAAAAoAAQAAAAAAAAACOEJJTQP1F0NvbG9yIEhhbGZ0b25lIFNldHRpbmdzAAAASAAvZmYAAQBsZmYABgAAAAAAAQAvZmYAAQChmZoABgAAAAAAAQAyAAAAAQBaAAAABgAAAAAAAQA1AAAAAQAtAAAABgAAAAAAAThCSU0D+BdDb2xvciBUcmFuc2ZlciBTZXR0aW5ncwAAAHAAAP////////////////////////////8D6AAAAAD/////////////////////////////A+gAAAAA/////////////////////////////wPoAAAAAP////////////////////////////8D6AAAOEJJTQQIBkd1aWRlcwAAAAAQAAAAAQAAAkAAAAJAAAAAADhCSU0EHg1VUkwgb3ZlcnJpZGVzAAAABAAAAAA4QklNBBoGU2xpY2VzAAAAAHMAAAAGAAAAAAAAAAAAAABIAAAAoAAAAAkAYQBpAHIAdABhAHMAdABpAGMAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAAAAAAKAAAABIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADhCSU0EERFJQ0MgVW50YWdnZWQgRmxhZwAAAAEBADhCSU0EFBdMYXllciBJRCBHZW5lcmF0b3IgQmFzZQAAAAQAAAACOEJJTQQMFU5ldyBXaW5kb3dzIFRodW1ibmFpbAAACcEAAAABAAAAcAAAADIAAAFQAABBoAAACaUAGAAB/9j/4AAQSkZJRgABAgEASABIAAD/7gAOQWRvYmUAZIAAAAAB/9sAhAAMCAgICQgMCQkMEQsKCxEVDwwMDxUYExMVExMYEQwMDAwMDBEMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMAQ0LCw0ODRAODhAUDg4OFBQODg4OFBEMDAwMDBERDAwMDAwMEQwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAAyAHADASIAAhEBAxEB/90ABAAH/8QBPwAAAQUBAQEBAQEAAAAAAAAAAwABAgQFBgcICQoLAQABBQEBAQEBAQAAAAAAAAABAAIDBAUGBwgJCgsQAAEEAQMCBAIFBwYIBQMM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qD1jstOgQD5NI0jZPxsQ5Q/00kUe9QP5TXt2Kax5z16+XvHHcJaof8AYxIMP5sDlCATwtR/kH+WteV3ZXGoP3e7u7iUn/hxhOvy9DXwRwBGw/kr+VcJHDy3aGnQuHf/AIkxys67Mf4ingCb2nkDyPZ0+raBp8ZHQi2iJ+8rlZ1GQ85FPCE0h0fSYKejZW8VOnCJF/UMhxk9VpFKiqKKAB7CmRS3irsVaZEYUZQw9xXG1QVzoWiXQIudPtpweokhjb9YOSE5Dqikhv8A8pvy1v2LXPlyxJPdIhEf+SfHLo6vKOUijhC7y3+WXlDyzqZ1DQbaSxkeNopIEmlaBlYg7xuzLUEbMMGXUzyCpbqIgMqyhk7FX//X9UMqsKMAw8DvirlRFFFAUe22Kt4q7FXYq7FXYq7FXYq7FXYq7FXYq7FXYq7FXYq7FXYq7FX/2Q==
1)
$_FILES["file"]["name"] contains only name of uploaded file. Real temporary path of uploaded file on your server is stored in $_FILES["file"]["tmp_name"].
$bin_string = file_get_contents($_FILES["file"]["tmp_name"]);
2)
There can be some invisible character printed out before or after the image.
Please ensure that <?php is the first thing on the first row of your file and, for simplicity, remove all other <?php and ?>, including the last one. You can add die; on the last row to end the script to ensure that nothing else is added to the end of image.
I got the solution...
simply convert the image using
$bin_string = file_get_contents($_FILES["file"]["tmp_name"]); // image reading
And then for converting string back to image use the following :
" />
So i am trying to create a compass to show wind direction.
Function rotate($angle) {
$original = imagecreatefrompng("img/Arrow.png");
$compass = imagerotate($original, $angle, 0);
return $compass;
}
That will be displayed using some html that i am echoing. The variable angle is being passed from a database. The html on the php script looks like this:
<img src='".rotate($row['wind_dir'])."'/>
The image never displays, and clearly the browser does not know where it is.
When i view the html in my browser, the above line shows as
<img src="Resource id #4"/>
and when i click on it, it navigates to a 404.
What am i doing wrong? Have i forgotten a line in the image rotation function?
EDIT:
Having tried some of the responses below, i get an image, but it only shows as a black box!
EDIT2:
Well after much fiddling, it turns out all that was needed was to the third value of imagerotate() to -1 as follows:
$original = imagecreatefrompng("img/goog.png");
$compass = imagerotate($original, $angle, -1);
imagealphablending($compass, true);
imagesavealpha($compass, true);
header('Content-Type: image/png');
imagepng($compass);
imagedestroy($compass);
I posted a comment about using CSS or JS rotation instead but since then I've had a better idea.
The compass is always going to be Arrow.png in one of 360 positions.
Use a batch process in Photoshop or PHP to create 360 versions. One for each degree. Then you can just call Arrow_120.png for example for 120 degrees. You remove the issue with your existing code of creating images on the fly while avoiding compatibility issues with CSS / JS.
You have to execute the function and send header: try like below, say our php file name is rotate.php :
rotate.php
function rotate($angle) {
$original = imagecreatefrompng("test.png");
$compass = imagerotate($original, $angle, 0);
header('Content-Type: image/png');
imagepng($compass);
imagedestroy($compass);
}
if(isset($_GET['angle'])){
rotate($_GET['angle']);
}
THen in your html you can call the web resource i.e you php file as:
<img src="url_to_rotate.php?angle=90" />
Also remember to sanitize the GET input before executing it.
The displayed image should be a image file. To achieve this you should use imagejpeg();
So basically you should have 2 php files:
1: Creates the image file using your code and imagejpeg();
<?php
$original = imagecreatefrompng("img/Arrow.png");
$compass = imagerotate($original, $_GET['angle'], 0);
header('Content-Type: image/jpeg');
imagejpeg($compass);
?>
2: The php file that displays the image.
<img src='image.php?angle=".$row['wind_dir']."'/>
if you want just one file you could do the following:
<?php
$original = imagecreatefrompng("img/Arrow.png");
$compass = imagerotate($original, $_GET['angle'], 0);
ob_start();
imagepng($compass);
$stringdata = ob_get_contents();
ob_end_clean();
$imageData = base64_encode($stringdata);
$src = 'data: image/png;base64,'.$imageData;
echo '<img src="',$src,'">';
?>
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
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" />