Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a base64encoded string from image resource

Tags:

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.

like image 589
netzaffin Avatar asked Dec 14 '11 10:12

netzaffin


People also ask

How to convert an image to Base64 string in Java?

Convert Image File to Base64 Stringbyte[] fileContent = FileUtils. readFileToByteArray(new File(filePath)); String encodedString = Base64. getEncoder(). encodeToString(fileContent);

How to decode image Base64 in Java?

In Java to convert a Base64 String to file, firstly we need to decode the string to byte[] array and then write all bytes to the file system. String base64String = "<Base64 String>"; byte[] decodedBytes = Base64. getDecoder(). decode(base64String); Files.

How to convert text file to Base64 in Java?

In Java to convert a file to Base64 String object firstly we need to read all bytes of the file and then use the Base64. getEncoder(). encodeToString() method to encode it to Base64 String. byte[] byteData = Files.


1 Answers

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); 
like image 112
Michael Robinson Avatar answered Oct 16 '22 10:10

Michael Robinson