-2

I simply have the following code to generate an image from base64encodedString:

<?php
    require_once 'phpTextToImage.php';

    $img = new phpTextToImage;
    $text = $_GET["text"];
    $img->createImage($text);

    header("Content-Type: image/png");
    imagepng($img->image);
    imagedestroy($img->image);
?>

Inside phpTextToImage.php there is a few lines of code:

class phpTextToImage {
    public $image;

    function createImage($text) {

        // "iVBORw0KGgoAAAANSUhEUgAAAFkAAAAcCAYAAAAKnhNwAAAFWElEQVRoge2Zf4hVRRTHPy6PdS01VzaRRUJiyaU2K7NfJkbqH1FiYTbDaqQZSiol6ogFSogVViOkYGm2/fAXzph/qBVpPyApy1ATi6CQEDGJWGpZVl1lWfpjzvXdfd73Y98+XxDvC49z78w9c+Z878yZc+7rp4yiWDjrJwBfy+0obdRvRQ9WZjjrq4GLcjtTG7WjROMqYCkwGrgErE+VYuAKApz1C4ANsaZu4GKF5BLBWV8FrJbbg8Dj2qhWgKr/bFb/P9wMDJXrVyKCoUJyKTEkdn063lEhuXSIc9kd7+gRk5311wBLgCeABlE8CewCrDbqfC4jzvo5wEKgEegEvgFWaqNOJCnICf880AzcJPZ+BjZpo94r1LtccNYPI8TKKYTtfAbYCqwvQPdZ4BlCKOgCTgCbtVFbYs9MBfZkqP7qrIfA2bKq2MPDgaMyodGEF1ANNAGrgMPO+iFkxzqgBRgjukOBqaI3IcGBwcC3wBuiUyO/u4EWZ/32fCTkg7O+XnyaB9TLvEaKP/tz6FU76z8B3gbGEngYDIwHPnTWb4o93gW0AR2xtnZp64SeS7yFsALPApOAAcC1wHzC8m8CXsrh02TgRWCQNqo/cD9wikDcdmd9Tcbzm8WBv4FHgf7AIGC52JshK6kvaAFGEAjQYmMAYec05tBbBzwses0EHq4T/7qBebKC0UZ9qo2qBR6J6d+ljarVRq2EnuFiHIH9pdqor6StE9jorL8HmA08BizOMrGXtVFrohtt1CFnvQYOi6NPAe8AOOubgKgKWqSN2ivX54HXnfWjgDnAXGBjDjKyQmw8JLfLtVFerruBnc76VuDzBL1GwsoHWKyN2inXl4A1zvo7genAImBvpn4SLq9kYb42NmgcR0WOyDHWFdtbG/UDIY5BWK0RmkW2Akn2dklfrvCUD1NFdgLbEvoPZtGbSeClA0g6F/aJHCe5cV4kFiNyANYRtjqk879iipcvCDH+9ljbGJHHtFFdmQraqM+A64uwFccdIo9oo9p7oTdWZAfwmhxgcYwUWUPg6K98A14mzVmfAhYQTtMmSpfe/SFyWKytTuTZEtlIQmSvtzaiuQ0HTJ5nB1IoyULwfmCitLcBvxBOSYAbCGlMMWiLbDnrU0kr9yoh2oW9WcVxnCAcnLnQmqcfSK/kOaQJfgtYFs+JnfXzgE0Uh3qRnTGCWzP6rgYicof3Ui+aW4c2Km8uXQiikPCgyN+1UQsTio6+hI7bRMY/gx4TOUZ2UQ846xuc9aud9Sv6YPekyN7uwOMiG6VY6jOqMuQV8UsMPV3AWNMTdOtJn/IH4l0i64BpCWPNAlYQTvpiERUbNzrrJyf0j8+iF81tKCFtvfIB6wf2ZiIRuYdF3uusnyYDVcvkviNUYZGBbKt6lbN+iWQmUZ66h1AtdRL7zipl9kdyu8FZP0V0aqQAeUH6ig1RAB+T3j3vO+snio2U+LgrSUkbdRyIUoq1zvonoxXtrB/irJ8B/Oisn13oRCLC3iV8OUoBu531F4ELhGS9gZ51ftJbPE0oGtYC55z1F4CfCOlQNzBfG3UqQ2cuIWzUAfvE5jlCKZsCtmij3izUkUxI/J9JSMVGAF/G/NpNeAFHsqjPBQ4RfN0KXHDWnwP+IdQDDcCthc6lSibUDtwHbAH+lL4zhArtFtJbCELOm4lj2qjnSBPXTTh4DgCTtFEfZCpoo9rE5nJCHOwi/VGpWRs1q1AnskEbdYSQL+8gnWqdAl4lnEPfZ9FrBx4gfOw6RHhRKQInXnxaWug8+vXlP74KCkPle3IZUCG5DKiQXAZUSC4DKiSXARWSy4AKyWXAv48vqzlv2H9WAAAAAElFTkSuQmCC";

        // how can I print here image to access it as an image/png?
        $this->image = base64_to_png($text, "image.png");
        return true;
    }

    function base64_to_png($base64_string, $output_file) {
        $ifp = fopen( $output_file, 'wb' ); 
        $data = explode( ',', $base64_string );
        fwrite( $ifp, base64_decode( $data[ 1 ] ) );
        fclose( $ifp ); 
        return $output_file; 
    }
}

I would like to use it the following way:

?text={{base64encoded}}

I have tried the following solution:

https://stackoverflow.com/a/15153931/2725435

but nothing is printed on the browser.

I have used the following source to get base64:

https://codebeautify.org/image-to-base64-converter

with the following image:

enter image description here

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 1
    What have you tried so far, and what went wrong with it? Have you researched how to unencode a base64-encoded string? For example looking at `base64_decode()`? – droopsnoot Apr 20 '23 at 17:05
  • yes, I will add it to the question within a minute. – Bartłomiej Semańczyk Apr 20 '23 at 17:06
  • Code [here](https://stackoverflow.com/a/11511605/892493) will help. `data:image/png;base64,` is not part of the base64 encoding and needs to be parsed to determine the image type and removed if present before decoding. – drew010 Apr 20 '23 at 17:15
  • As a general note, "it didn't work" is rarely a helpful comment; you need to tell us what _did_ happen when you tried that code. – IMSoP Apr 20 '23 at 17:18
  • @IMSoP of course you are right.... thank you;) I should attach some logs, but there are no logs;) Just nothing happened on browser;) – Bartłomiej Semańczyk Apr 20 '23 at 17:19

1 Answers1

1

You've actually written more code than you need, probably because you've put together different examples without understanding them.

Firstly, the code in your base64_to_png method is for saving the image to a file; but you don't want a file, you just want the data that would be inside that file. So instead of the calls to fopen, fwrite, and fclose, you just want to return base64_decode( $data[ 1 ] ) which is the raw data of the image.

Secondly, the function imagepng takes an object (or "resource") that you have created or loaded from file to be manipulated, and saves it out as a PNG. You can't pass it a filename, or a string of data, directly; since you don't want to manipulate the image, you just want to display it as-is, you don't need this function and imagedestroy at all.

If you have the raw data of the image (from the previous point), then all you need to do is echo it, to send it to the browser.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Ok, let me try your solution from 2 and 3. You might be right. I am not good with php, but I need it to the other project. Need some time to check it and try;) Thank you. Will be back soon – Bartłomiej Semańczyk Apr 20 '23 at 17:29
  • First of all thank you for your clarification. Yes, I totallt misunderstood the example;) Now it works and it is really simple;) – Bartłomiej Semańczyk Apr 21 '23 at 11:18