I'm using this code to get the Exif orientation of a JPEG image from its base64 encoded data.
$exif = exif_read_data('data://image/jpeg;base64,' . $base64EncodedImageData);
$orientation = $exif['Orientation'];
However this code is slow especially when dealing with HD images. Is there any way to get the orientation faster?
It seems to be much faster when passing a actual file path instead of the base-64 encoded string, but the image is not on the disk and the base-64 encoded string is all I have.
The fact you're loading in the entire blob is why it is taking so long, so you really just need to load in the part of the image that contains the exif data.
When you give exif_read_data a file path instead of a blob, it only reads the necessary information by default. If that is simply not an option, then you may need an ugly workaround / hack.
For example, you could specify a hard-coded value:
$image = base64_encode(file_get_contents('test.jpg'));
echo strlen($image) . '<br />'; // 81684
// only read the first 30000 characters
$exif = exif_read_data('data://image/jpeg;base64,' . substr($image, 0, 30000));
echo '<pre>';
var_dump($exif);
echo '</pre>';
Or you could increment it dynamically until the appropriate size is found:
$image = base64_encode(file_get_contents('test.jpg'));
$read = '8192';
// this will produce errors, so i am suppressing them
while (!$exif = #exif_read_data('data://image/jpeg;base64,' . substr($image, 0, $read))) {
$read += $read;
}
echo '<pre>';
var_dump($exif);
echo '</pre>';
There may be a better solution but I don't know what it is. Hope that helps.
Related
I'm using below code
Here $data is a byte array.
$decoded_data = base64_decode($data);
$im = imagecreatefromstring($decoded_data);
if ($im !== false) {
header('Content-Type: image/png');
$image_nam = "png_".time().".png";
imagepng($im, $image_nam);
imagedestroy($im);
}
But I don't get the exact output
try this simple code for save image into directory
file_put_contents('/myfolder/imageName.jpg', base64_decode($img));
you can also try this
$base_to_php = explode(',', $base64Img);
$data = base64_decode($base_to_php[1]);
$filepath = "/path/imageName.png"; // or image.jpg
file_put_contents($filepath,$data);
I would suggest you to refer link -
This could be possible an issue with metadata present in base64 string.
As per source the problem could be due to data:image/png;base64, is included in binary string.
The metadata present in the binary string results in invalid image data when the decoded back. Can you try it by removing metadata in the function before decoding the string.
You may use function and write output to a new file.
$position_s= strpos($base64_string , "data:image/png;base64,");
substr($base64_string, 0, $position_s) . substr($base64_string, $position_s+ strlen("data:image/png;base64,");
Note: If you are preferring explode function, it would be best to set limits:
explode(',',$base64_string,2)
I want download an image from AWS S3 and process it with php. I am using "imagecreatefromjpeg" and "getimagesize" to process my image but it seem that
Storage::disk('s3')->get(imageUrlonS3);
retrieve the image in binary and is giving me errors. This is my code:
function createSlices($imagePath) {
//create transform driver object
$im = imagecreatefromjpeg($imagePath);
$sizeArray = getimagesize($imagePath);
//Set the Image dimensions
$imageWidth = $sizeArray[0];
$imageHeight = $sizeArray[1];
//See how many zoom levels are required for the width and height
$widthLog = ceil(log($imageWidth/256,2));
$heightLog = ceil(log($imageHeight/256,2));
//more code here to slice the image
.
.
.
.
}
// ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg
$content = Storage::disk('s3')->get(imageUrlonS3);
createSlices($content);
What am I missing here ?
Thanks
I think you are right in your question what the problem is - the get method returns the source of the image of itself, not the location of the image. When you pass that to createSlices, you're passing the binary data, not its file path. Inside of createSlices you call imagecreatefromjpeg, which expects a file path, not the image itself.
If this indeed the case, you should be able to use createimagefromstring instead of createimagefromjpeg and getimagesizefromstring instead of getimagesize. The functions createimagefromstring and getimagesizefromstring each expects the binary string of the image, which I believe is what you have.
Here's the relevant documentation:
createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php
getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php
Resulting code might look something like this:
function createSlices($imageData) {
$im = imagecreatefromstring($imageData);
$sizeArray = getimagesizefromstring($imageData);
//Everything else can probably be the same
.
.
.
.
}
$contents = Storage::disk('s3')->get($imageUrlOnS3);
createSlices($contents);
Please note I haven't tested this, but I believe from what I can see in your question and what I read in the documentation that this might just do it.
$data = b"""
\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x18\x00h\x03\x00\x00\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00d\x00\x00\x00d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00X;ÿ%\x1Cï\t$÷\x07\x11ÿ$\x16ÿ\x11\x1Eü\x13\x19ôçó÷ÈÈø\x07\fû#\x1Fÿ\e\x15ÿ\x0E\x17û,$ú\x13\x17èAHÿODÿ%\x1Dÿ\t\x11ÿ\x18\x0Eú)\x04ô\x1C\n
ñ-\x1AóïÝÿµ±ý\f\tï\x1F\x07ï \x07ñ\x19\x11ø%\tÿ(\x1AÿTOÿFLó!\x1Cô21ý•›þ©÷—–þ““÷üôÿêåÿŽÿ£‘ü¦žõƒ“ý'\x1Dó\e\x1DóISôELõ\e\x0FñO?þìüÿüÿ÷ñüÿêüÿúþÿûþÿóúÿÿúÿøÿõÒíÿ,)ô\e\e÷ORú;Mø\x13\fýN4ÿðõÿÿûýÿüÿÿÿéÿþ÷üÿïÿÿîÿþÿþþþÛãÿ*\eÿ$\x15ÿWLü0Aü\r\rùL;üòùÿÿýþÏ°™’f\x1F•`.a+•g1ØÌ°ÿÿùÞáÿ$!ò\x15\x16ôACÿxÿYRùuëõü÷ÿþÿÎ¥„~6\x00C\x01#\x03‹3\x04àϨÿÿøëåübkëXañ}ÿÿÿòø÷ÿÿûÿÿÿéöïþͤ‹‹?\x00˜S\x03”M\x039\x11ÍÓ°øöõÿþûøùÿÿþýÿÿò‹}ÿLYûmoýøôÿÿþüǬl~A\x00‡D\x00E\x00z7\x06æØ£öÿéìîÿhe÷Saúˆ~ÿBFø\x00\fèD>÷ýüÿÿÿøÔ¨›”[\x1Ee*£c-•Z3ëÍÂÿÿôëãÿ,\x12ø\x06\fóFOöIIÿ\x12\x0FøM9þöþóÿÿôÿöÿÿþôÿøÿÿøÿÿüùý÷ÿûÿùìêÿ:\x14ÿ\x1F\x16ÿKRý?Gþ\x18\x1CñJBúîúÿöÿòúÿþÿÿþÿúýÿÿñýÿþñúýøÿùçóÿ7,ç\x1C ñCMþJAÿ&\x17ô6(ü”ŽÿŸ™ÿŸý üþûöçåñŸÿ˜•øŸ˜ÿŒÿ,&é&\x1AðYNÿQIÿ+\x1Cù\x16\x14ÿ\v\x11ø\t\tï\x06\fù\x0E\x0EöÑ÷ü©Ãÿ\x02\x00ô\x08\rò\f\x06í\n
\x0Fô\x18\x18ÿ&\x15ùXOÿ;Aþ"\x1Eé\x1C\x1Eø\x19 ó'\x1Aò\e\x13ô3#ýææÿµ°ÿ \x12ì+\x1Eö5\eÿ\x19\eõ\e ÿ$\x1DðFLÿýüÿÿþúøøÿøÿþÿþýîþÿíõÿÿÿóÿÿùêýÿúÿÿÿõÿöþ÷øÿþÿÿøûùÿ\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
"""
The above data is image binary data.
I am converting the above binary data into actual image. I have written the code as follows:
Note: I am using image intervention package - Laravel and I have written the code for displaying favicons of banks(Financial Institutions). This api is provided by Yodlee Interactive.
$img = Image::make($data)->save('images/favicons/icon1.jpg');
return $img->response();
OR
$img = Image::make($data)->save('images/favicons/icon1.ico');
return $img->response();
When I am executing the code, I am getting following error,
NotReadableException in Decoder.php line 91:
Unable to init from given binary data.
The response you receive is not byteArray, it is string so you probably have to first convert into byte array and then into image.
This should give you some pointers to start.
<?php
//Here write your code to get $byte_array
$data = base64_decode($byte_array);
$im = imagecreatefromstring($data);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
?>
Or you can probably refer to these links.
Converting a byte array into an image using PHP and HTML
http://www.zoubi.me/blog/php-get-image-byte-array
Please use the following sample code for getting the captcha image.
$bytes = array(Paste byte-array from Yodlee-API response here Ex: 0,2,3,4,1,1,1);
$byteArray = implode(array_map("chr", $bytes));
$imgData =base64_encode($byteArray);
$img = "<img src= 'data:image/jpeg;base64,$imgData' />";
print($img);
Hope this helps.
Regards,
Krithik N
public function addNewCategory($category_title, $strImage) {
// get the image from the base64 string.
$strImage = str_replace('data:image/png;base64,', '', $strImage);
$strImage = str_replace(' ', '+', $strImage);
$strImage = base64_decode($strImage);
// set the path name of where the image is to be stored.
$path = dirname(__FILE__).'/category_images/'.$category_title.".png";
// save the image in the path.
file_put_contents($path, $image);
// insert category and the image path into the MySQL database.
$result = mysqli_query($this->db->connect(), "INSERT INTO category(category_title, path, created_at) VALUES ('$category_title', '$path', NOW())");
if ($result) {
return mysqli_fetch_array($result);
} else {
return false;
}
}
After running this code I found out that the PNG file with the category title name is stored in the category_images directory. But it turns out that the size of the PNG file is zero, so I believe that there is a flaw in converting the base64 string into image.
Your tips for the correction will be very much appreciated.
Edited
The Base64 string here is encoded in Android with the following method.
public String getStringFromImage(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageBytes = stream.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
and here is the encoded string. I got this value using Log.i() method.
iVBORw0KGgoAAAANSUhEUgAAAlgAAAJYCAIAAAAxBA+LAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4nOzd2XNcV5Im+M/dz11iwb5xkyiltlSq1VVZOVVdM9M2bdaP/TJ/7zyMWc/YzFj3dM1YV1eVVVZWKZVaKXEnloi49xx374cbACmJFEUQoCjAf0aDASAQcQMg44uzuB/Cx/8eIYQQwmXFP/cFhBBCCD+nCMIQQgiXWgRhCCGESy2CMIQQwqUWQRhCCOFSiyAMIYRwqUUQhhBCuNQiCEMIIVxqEYQhhBAutQjCEEIIl1oEYQghhEstgjCEEMKlFkEYQgjhUosgDCGEcKlFEIYQQrjUIghDCCFcahGEIYQQLrUIwhBCCJdaBGEIIYRLLYIwhBDCpRZBGEII4VKLIAwhhHCpRRCGEEK41NLPfQGvnPtTP5NYnvrl/IMvH/Tk37m1k3c4XluEEMIvyeULwhN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0HmAggoRoQhhHCWzr98wofmmQ5mTNdw5Sbv3uymm8h5WV2wrJcH4OTF+qMqJaoIYHJfnYxvXr/+xpVrb75xtVlu9YSrWV+OixTE3a3AXd2dlouI4CQAbFhIXJ6/6wZvjysThmXCwSkeVo9Cx6NJdvhQyU+wVKk7tCzDF8IMZk6OAkuOZpTWp7vXtrd3V9b+eTT5h6+/LH2RrhNOGeRZZwJIM77xPiZruU749k9YHBoniOBZncFDCCGc1vkHoRAzGQwpYW19vHetX90uWZLXUBjUoYAB5jCH1bW79+NmfOPG9XfeuHl
According to the encoded string, the data:image/png;base64, is not included in front of the string. I think now this is the reason the PNG file is not properly made. What can be done here to fix this issue?
That encoded data is the contents of the image file, and data:image/png;base64, is only used if you're cramming the image data into HTML.
Putting the data into your HTML source bloats the source, slows down your initial page load, robs the browser of the ability to download large resources like images in parallel, and makes the image un-cacheable between pages. Don't do it unless you literally have no other option.
To convert the image back to a file simply:
file_put_contents('foo.png', base64_decode($str));
Or make a PHP script pretend it's an image:
header('Content-Type: image/png');
echo base64_decode($str);
Or to do the thing I just told you not to do because seriously never do this there are so many ways to not do this, I'll be super disappointed with you if this is what you take away from this answer:
printf('<img src="data:image/png;base64,%s" />', $str);
In my cakephp program, I am converting a base64 encoded image to an image and then storing it in a folder.
How can I validate this image, before conversion?
The string is like data:image/png;base64,iVBORw0KG......
(The image can be of any extension not only png.)
This answer might be useful for processing the image.
This answer might be useful for validating the image.
Code:
$data = 'data:image/png;base64,iVBORw0KG.....';
list($type, $data) = explode(';base64,', $data, 2);
$data = str_replace(' ', '+', $data);
$data = base64_decode($data);
if (imagecreatefromstring($data) == false) { echo "invalid_image"; die(); }
file_put_contents('/storage/folder/image.png', $data);
You might also want to restrict file types.
You must keep in mind that you can't simply trust any of the data sent from the client (like 'data:image/png'), so you must rely on other means (some php functions).