How to save a base64 decoded image in the filesystem using php? - php

I am getting a base64 encoded JPEG string via a POST request to my web service.
I want to decode it and save it in the filesystem.
How can I achieve this using PHP 5.3.
I am able to successfully decode the data using the base64_decode function.
How can I save this decoded string as a JPEG image in the server?
Thanks in advance.

If you are sure the image will always be jpg then you can simply use: file_put_contents();
<?php
$decoded=base64_decode($encodedString);
file_put_contents('newImage.JPG',$decoded);
//leave it to you to randomize the filename.
?>

Replacing the blank spaces with + signs is required if the data is derived from canvas.toDataURL() function.
$encodedString = str_replace(' ','+',$encodedString);
See this question
It helped a lot in my case.

Related

Convert FPDF output to base64 string

I am working with the PHP API where I need to create the PDF file using the FPDF and then I wanted to save it to the database by converting it to the base64 string. When I have tried the default $pdf->Output() method then I got the PDF file in my postman response.
Then I have tried to use this line of code to encode the pdf to base64 format. It worked but I am getting the broken pdf when I decode the base64 string.
$pdfFile = $pdf->Output("","S");
$base64String = chunk_split(base64_encode($pdfFile));
Am i doing anything wrong with this? Any small help would be appriciated.

PHP base64 encoded image not displaying correctly

I'm trying to send an image (any size) over to an API I'm creating, using base64 encoding. The encoded string hits my API as a parameter in the URL. The API built with PHP.
Once the request hits my API, I want to store the image onto my server, and save the file name in the database. This is working, however I'm getting some odd outputs with the actual image.
For reference, I also followed http://blog.justin.kelly.org.au/simple-base64-encodedecode-url-safe-functions/ but have the same results.
The image I am trying to store on my server:
$encode = base64_encode(file_get_contents($image));
echo $encode;
For testing, the ouput of this is the string I'm using to pass to my API.
In the API:
$image = base64_decode($_POST['image']));
$image_name = md5($image) . ".jpg";
file_put_contents(/public/image/ . $image_name, $image);
This works, my image is put onto the server in the correct directory with a random name which is saved to the database.
However, when navigating to the image directly, the image is warped:
If you have a sharp eye, the top part of the image is actually correct before it starts to fail, which makes me start to think whether the string is not getting encoded correctly to be sent as a parameter?
Any clues would be lovely, cheers.
EDIT: Changed it to POST, removed urlencode/decode & removed strtr.
Thanks to the comments:
base64_encode(file_get_contents($image_path));
No urlencode or strtr needed. Use POST request, and alter the post_max_size on the web server to allow for bigger images to be passed through.
$image = base64_decode($this->getParameters('avatar'));
$image_name = md5($image) . ".jpg";
Maybe I'm looking at it wrong, but first you encode it base64 -> urlencode and then you decode it base64 -> urldecode again, shouldn't the latter be in reverse order?

Coverting Hex to Image in PHP?

I am developing mobile app which talks with server via PHP Webservice. This is my first time using PHP. I managed to upload data in to database. Now i need to send an image to store it in ftp server. For that i converted image->hex and sent from my app.
Server Side
I got the hex code but not sure how to convert it in to an image and store in in ftp server. I am really struggling here. I googled it but couldn't find exact one.
Any help is much appreciated.
Convert the HEX string to binary:
$binary = pack("H*", $hex);
pack("H*", ...) is equivalent to hex2bin, which is available since PHP 5.4.
Write it to disk:
file_put_contents("file.png", $binary);
Suppose you have received a hex string in a page where you want to convert this hex to real image. Please check this code snippet will help you or not.
<?php
$hexpic=".......................
.....................";
# convert the hex string to binary
$data = pack("H" . strlen($hexpic), $hexpic);
#write the binary string into an image file
file_put_contents("sample.png", $data);
?>

base64 encode an image without saving

Can I base64 encode an image that I created on the fly, without first saving it to disk? As far as I know, base64_encode() only accepts strings, and I couldn't find a way to retrive image source object as string without first saving it, and load it with file_get_contents()
GD doesn't provide a method to return an output image as text, but you can fake it with the output buffering functions:
ob_start();
imagejpeg($handle); // no second parameter, will do output instead of writing to file
$img = ob_get_clean();
echo base64_encode($img);

convert image to byte array

i want convert image into byte array in php.actually i am accessing web service in dot net.where i want to pass image as byte array.i tried this code
$data1 = file_get_contents("upload/1311677409gen1.jpg");
$byteArr1 = str_split($data1);
foreach ($byteArr1 as $key=>$val)
{
$byteArr1[$key] = ord($val);
}
and send this array name to web service.but i got error parameter is not valid.i googled it.but dont get proper solution.i need it urgent.help.
Thanks in advance.
Without seeing the .net webservice, you probably have two options:
Stream the image
base64 encode the image before transfer, provided the webservice can decode it.

Categories