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.
Convert Image File to Base64 Stringbyte[] fileContent = FileUtils. readFileToByteArray(new File(filePath)); String encodedString = Base64. getEncoder(). encodeToString(fileContent);
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With