I need to quickly generate an image (base64 encoded JPEG) to simulate an API call.
Of course I can:
public function readImageApiCall($imageId)
{
return function () use ($imageid) {
return ['result' => base64_encode(file_get_contents('myimage.jpg'))];
}
}
But I don't care the actual image content, i just need a valid JPEG image without the need of placing a dummy image in the filesystem.
You can do it with gd PHP extension and imagejpeg (you must capture output buffer)
A sample to how to create an image from scrach
http://www.thesitewizard.com/php/create-image.shtml
a link to PHP manual
http://php.net/manual/en/function.imagejpeg.php
But, why you don't set it in the code?
$result = "/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAZAAA/+4ADkFkb2JlAGTAAAAAAf/bAIQAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQICAgICAgICAgICAwMDAwMDAwMDAwEBAQEBAQECAQECAgIBAgIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMD/8AAEQgAYABgAwERAAIRAQMRAf/EAaIAAAAGAgMBAAAAAAAAAAAAAAcIBgUECQMKAgEACwEAAAYDAQEBAAAAAAAAAAAABgUEAwcCCAEJAAoLEAACAQMEAQMDAgMDAwIGCXUBAgMEEQUSBiEHEyIACDEUQTIjFQlRQhZhJDMXUnGBGGKRJUOhsfAmNHIKGcHRNSfhUzaC8ZKiRFRzRUY3R2MoVVZXGrLC0uLyZIN0k4Rlo7PD0+MpOGbzdSo5OkhJSlhZWmdoaWp2d3h5eoWGh4iJipSVlpeYmZqkpaanqKmqtLW2t7i5usTFxsfIycrU1dbX2Nna5OXm5+jp6vT19vf4+foRAAIBAwIEBAMFBAQEBgYFbQECAxEEIRIFMQYAIhNBUQcyYRRxCEKBI5EVUqFiFjMJsSTB0UNy8BfhgjQlklMYY0TxorImNRlUNkVkJwpzg5NGdMLS4vJVZXVWN4SFo7PD0+PzKRqUpLTE1OT0laW1xdXl9ShHV2Y4doaWprbG1ub2Z3eHl6e3x9fn90hYaHiImKi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mfM/aerPfabpzqlf8Amnfyqsh8v6jD/IL4w7k2F038yNlbQ3dsSTcm6dpUlXsrv3rDeK7aky3VPceTxNGd0Un8Nr9n46t23n6f7uqwFZTsq08sE8ioZbRuk+z38V/BmSNwc+YBrTHrTj5cek91bpdwNbyfAwI/b1riVf8AK/8A5uOay238Xk/ij1FtLJYfF7swGT3Oe69wbm2S82Y7J3HvXF5Shx21uq6nPZDHY6l3JNCYvJFLO4LGWPWdMr/67TKiulmPqxEy1Mg0gtM8owFBIXWcEnPHoOHlYFyRPSMsppp8lQJQelSK9bOv8nj4Gbs+A3xxyfX2/M1HuLe+8t4bj7D3pnIqCPERZrem9M/l9zbnyNFg46zJNg8HDU5aKhx1LLUS1S0dEjzkSOwEQX11NfXst7caTPNIXbTwq2cDoTxRJBEsMddCKAK+g6tu9penOiT/ADH/AJd/xC+ee1W218lOoMNu3JU1HLSbc7Ew09ZtDtjZReOsijn2f2TtubHbrw3hXITj7cVD0cqzypNDJHJIjWidoX8SMkPQ5HHNPP8AL9mOFeqsquKMAR/LjX/D1To//CY34wVuYil3P8hPlH2BtqGqaX+6+9O3twjG1tO+GxG3HpMrJtFtqZqvppdu4CioZYzXok8FOokBYkk7n5l3y5TRNdzstDxc+YVafZRVH2DpOtlaJ8MaD8vRi3+Ek9X4fHb499d/GPq/b/UvWGJo8JtPbdFRY/GY3G0FLisZj6DG0NNjMbjcViqJVpcZi8dj6OOKGFdRAUszO7MxI6kgA+XSrh0OfvXXuve/de697917r3v3Xuve/de697917r3v3Xuve/de697917r3v3Xuve/de697917r3v3Xuve/de697917r3v3Xuve/de697917r3v3Xuve/de697917r3v3Xuve/de697917r3v3Xuve/de6//9k=";
Related
I am using Croppie jQuery plugin which returns the cropped image encoded in base64.
After submitting the form (with the cropped image encoded in base64) - I decode & resize it using the Intervention Image library:
public function decodeResizeAndStore(Request $request)
{
$croppie_code = $request->croppie_code;
// https://stackoverflow.com/a/11511605/4437206
if (preg_match('/^data:image\/(\w+);base64,/', $croppie_code, $type)) {
$encoded_base64_image = substr($croppie_code, strpos($croppie_code, ',') + 1);
$type = strtolower($type[1]);
$decoded_image = base64_decode($encoded_base64_image);
$resized_image = Image::make($decoded_image)->resize(300, 200);
// AND NOW I WANT TO STORE $resized_image using Laravel filesystem BUT...
}
}
Finally, I want to store the resized image using Laravel's filesytem (File Storage) and that's where I'm stuck - when I try this:
Storage::put($path, (string) $resized_image->encode());
... it doesn't work. Actually, it is working something - it looks like there is some memory leak or something, the browser's tab freezes, my RAM & CPU usage go high...
So I just tried:
dd($resized_image->encode());
... and yes, this is where it definitely crashes - when using encode() method.
I am not sure why, maybe this is happening because I'm not working with a standard image upload but with decoded base64?
But, on the other side, Intervention Image can create a new image instance from the base64 as well as from the decoded base64: http://image.intervention.io/api/make
... and, in my case, this works OK:
$resized_image = Image::make($decoded_image)->resize(300, 200);
I could then use the save() method and everything would work OK. But I need to use Laravel's File Storage.
Do you know how I can handle this?
Assuming you're using latest version of Laravel (5.7):
you can use stream method like so:
// use jpg format and quality of 100
$resized_image = Image::make($decoded_image)->resize(300, 200)->stream('jpg', 100);
// then use Illuminate\Support\Facades\Storage
Storage::disk('your_disk')->put('path/to/image.jpg', $resized_image); // check return for success and failure
I know we can use imagecreatefrompng and imagejpeg functions to convert PNG to JPEG. But it creates a new file.
I just need file_get_contents() to store the file into database. That means I need JPEG file contents from my PNG file
eg:-
imagejpeg(imagecreatefrompng($_REQUEST['sig_data']), "test.jpeg");
$appSig = addslashes(file_get_contents("test.jpeg"));
Above example I need to create a 'test.jpeg' file. If I didn't pass that as an argument to imagejpeg function, it print the file in screen. And I can't do object buffering also. imagejpg function just return the status.
Admittedly somewhat hacky, but you can capture the direct output of imagejpeg:
ob_start();
imagejpeg($resource); // no file name
$imageData = ob_get_clean();
I create an image with an external function.
The function that returns the raw data of the image:
function create_image()
{
...
ImageJPEG($myimg,NULL,85);
$imgdata = ob_get_contents();
ob_end_clean();
return $imgdata;
}
My script which should show what the image looks like:
$rawdata = create_image();
<img src="data:image/jpeg;base64,".base64_encode($rawdata)."" />
Now the image is not complete in the <img> tag. If I make the quality 50 (with ImageJPEG($myimg,NULL,50);) the image will be displayed completely. If I catch the rawdata and write it to the disk, the image will be complete in every quality.
$rawdata = create_image();
$im = imagecreatefromstring($rawdata);
ImageJPEG($im,"./test.jpg",90);
Only in the <img> tag it doesn't work.
Does anybody have an idea why it doesn't work?
I personally have never used this technique, I usually use a simple image tag with my image.jpg.php (for example) as an image. For this I put a header("Content-type: image/jpeg") in the image file and simply echo the data.
I really never have tried it with raw data, but I believe this would be preferrable, especially for maintenance (and compatibility).
I need the a function like imagecreatefrompng(), but I want to pass in the image directly (as opposed to the filename). Is this possible?
Note: I need this because the image is coming from a blob in a database.
imagecreatefromstring()
It should detect it is a png from the image header and return an image resource.
Yes, you can use imagecreatefromstring()
ttp://us2.php.net/manual/en/function.imagecreatefromstring.php
I have an upload Api that as a response object delivers (along with other stuff inside a Json object) a base64 encoded jpeg image.
I create the encoded image as so:
$im; // gd image resource
ob_start();
imagejpeg($im);
$data = base64_encode(ob_get_clean());
The data then is put inside a form field using javascript and submitted.
How can I create a GD resource from that again so that I actually can save that image as a file?
Everything in PHP.
You can use imagecreatefromstring() function:
$data = base64_decode($_POST['image_as_base64']);
$formImage = imagecreatefromstring($data);
"String" does not mean a "real" string. In that case it means bytes/blob data.
How can I create a GD resource from that again so that I actually can save that image as a file?
Are you sure you need to do that extra step? How about:
file_put_contents('MyFile.jpg', base64_decode($_POST['MyFormField']));
I did by this way:
// input is in format: data:image/png;base64,...
$str="data:image/png;base64,";
$data=str_replace($str,"",$_POST['image']);
$data = base64_decode($data);
file_put_contents('tmp/'.mktime().'.png', $data);