how to create a base64encoded string from image resource - php

I have sent a base64 encoded string via AJAX to PHP and created an image resource with imagecreatefromstring - all is fine.
Now I want to get the base64 encoded string after resizing te image, but i CANT find a function to get the base64encoded string.

Taken from http://www.php.net/manual/en/book.image.php#93393
$image = imagecreatefromstring($file);
// start buffering
ob_start();
imagepng($image);
$contents = ob_get_clean();
echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";
imagedestroy($image);

Related

An image created by PHP has no extension and is not treated as an image

I am loading an image from a remote server (google for example), but PHP never treats it as an image. This is my code:
$photo = imagecreatefromstring(file_get_contents("https://some.com/image.jpg"));
$filename = $photo["name"];
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); //returns ""
$filepath = $photo["tmp_name"];
is_array(getimagesize($filepath); //returns false
What am I doing wrong?
imagecreatefromstring returns an image not array.
$photo = imagecreatefromstring(file_get_contents("https://some.com/image.jpg"));
ob_end_clean();
header('Content-type: image/jpeg');
imagejpeg($photo, null, 80);
If this code doesn't produce an image then start debugging from finding out the return value of file_get_contents().
Also - after creating an image (using any of imagecreatefrom... functions) you are dealing with image resource, not file system or any other object. Image resource does not have information you are trying to extract (name, pathinfo etc.)

Imagecreatefromwebp(): WebP decode: fail to decode input data

I am trying to convert a webp file to JPEG using imagecreatefromwebp() but unfortunately, it throws me a warning: Warning: imagecreatefromwebp(): WebP decode: fail to decode input data.
Here's my code
$filename = dirname(__FILE__)."\\".$keyword."1.webp"; // $keyword = 'xyz';
$im = imagecreatefromwebp($filename);
// Convert it to a jpeg file with 100% quality
imagejpeg($im, './example.jpeg', 100);
imagedestroy($im);
Please help.
i am using this code, it works fine for me. Here $data contains the base64encoded data
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
The imagecreatefromwebp() function accepts either a valid file or URL. You can also pass the your binary data in that function. You can check the function definition and example here http://php.net/manual/en/function.imagecreatefromwebp.php

Laravel 4 - unserialize(): Error at offset while save base64 image

I'm trying to upload base64 image to server.
I've already try many answers, but i always get the same errors.
Links that i tried
How to save a PNG image server-side, from a base64 data string
https://www.reddit.com/r/laravel/comments/39wclp/how_to_convert_an_base64_image_string_to_an_image/
Convert Base64 string to an image file?
This code below should work.
$image = base64_decode(Input::get('image_cropped'));
$image_name= 'file_name.png';
$path = public_path() . "/images/" . $image_name;
file_put_contents($path, $image);
If you inspect the form sent from request the image encoded starts with data:image/png;base64, so try with:
$img = explode(',', Input::get('image_cropped'));
$img_name = 'file_name.png';
file_put_contents(public_path().'/images/'.$img_name, base64_decode($img[1]));
This worked for me.

PHP View Picture From Base64 String

I am connecting to Active-Directory and getting the thumbnailPhoto attribute successfully.
I have stored the file in the DB using Base64 encoding which makes the result look like:
/9j/4AAQSkZJRgABAQEAYABgAAD/4RHoRXhpZgAATU0AKgAAAAgABQEyAAIAAAAUAA ...
(Full Base64 encoded string: http://pastebin.com/zn2wDEmd)
Using a simple Base64 Decoder and decoding the string into a binary file and rename that to jpeg and open with an image viewer (here: Irfan View) I get the correct picture - see yourself:
How do I achieve this through PHP - I have tried using:
<?php
$data = '/9j/4A...'; //The entire base64 string - gives an error in dreamweaver
$data = base64_decode($data);
$fileTmp = imagecreatefromstring($data);
$newImage = imagecreatefromjpeg($fileTmp);
if (!$newImage) {
echo("<img src=".$newImage."/>");
}
?>
I'm just getting a blank page!
Your problem is that imagecreatefromstring() doesn't return a file, but rather an image in memory that should be output with the correct headers.
$data = base64_decode($data);
// Create image resource from your data string
$imgdata = imagecreatefromstring($data);
if ($imgdata) {
// Send JPEG headers
header("Content-type: image/jpeg");
// Output the image data
imagejpeg($imgdata);
// Clean up the resource
imagedestroy($imgdata);
exit();
}

Displaying an image created with imagecreatefromstring

Let's say I have the code that looks something like:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
echo "<img src=" . /* what goes here? */ . "alt=\"the image\" />";
//
// more stuff here
//
?>
What do I replace /* what goes here? */ with so my image data will display?
Thank you.
What do I replace /* what goes here? */ with so my image data will display?
The location you highlighted is the so called src attribute of the img HTML-tagDocs. The value is a so called URIDocs.
In your case you want that URI to point to the image data in question. You have not specified which type the image should be output as, so I will assume it's a PNG image in the following example.
You now need to convert your image data into an URI. The most straight forward URI to create from the image data is a so called data: URIWikipedia:
<?PHP
//
//... stuff here
//
$im = imagecreatefromstring( $imageData );
ob_start();
imagepng($img);
$png = ob_get_clean();
$uri = "data:image/png;base64," . base64_encode($png);
echo "<img src=" . $uri /* URI goes here */ . " alt=\"the image\" />";
//
// more stuff here
//
?>
Even this is the most straight forward way, it is not always recommended to do so because the image data will be returned with the HTML to the browser. If the image is large, this is commonly considered an overhead.
Instead of using the data: URI you can place any other URI in there as well, for example a HTTP URI that is pointing to a PHP script on your server that is returning the image. Such a script can be very simple:
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/png');
imagepng($img);
This is comparable to what Marc B suggested, see his answer as well.
<?php
$img = imagecreatefromstring($string);
header('Content-type: image/jpeg');
imagejpeg($img);
should be all you need. Doing it with the image tag as you are, you'd need to output the image to a temporary file and point the image tag at that (which incurs a second HTTP request), or use a data url.
I think you can do something like this...
$src = "data:image/gif;base64," . $imageData ;
echo "<img src=\"$src\" alt=\"the image\" />";
You have to save the resource to a file first or output it using something like imagepng() in a separate request.
See imagecreatefromstring() documentation for more information.
If you want to use a Data URI scheme, you can try this instead:
<?php
// If your image is binary data. use `base64_encode($imageData)`.
$imageData = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
echo '<img src="data:image/png;base64,'. $imageData .'" />';

Categories