Php gd image to base64

Well, my question is very simple, i just wanna convert the output of imagepng/imagejpg to base64, how i can do this ? the correctly way is with capturing output buffer ? thanks.

asked Dec 18, 2011 at 12:40

imagejpeg/imagepng doesn't return any data, they write the image data directly to the output stream [or to a file].

If you'd like to capture this data encoded as base64 the easiest method is to use PHPs Output Control Functions, and then use base64_encode on the $image_data.

ob_start []; 

  imagejpeg [$img];
  $image_data = ob_get_contents []; 

ob_end_clean []; 

$image_data_base64 = base64_encode [$image_data];

answered Dec 18, 2011 at 12:44

0

The most common use case for base64 encoded images is HTML output. I'd like to offer a more complete solution for this case. I have also added the ability to switch output image formats.

// Example
$gdImg = imagecreate[ 100, 100 ];
imagecolorallocate[ $gdImg, 0, 0, 0 ];
echo gdImgToHTML[ $gdImg ];
imagedestroy[ $gdImg ];

// Create an HTML Img Tag with Base64 Image Data
function gdImgToHTML[ $gdImg, $format='jpg' ] {

    // Validate Format
    if[ in_array[ $format, array[ 'jpg', 'jpeg', 'png', 'gif' ] ] ] {

        ob_start[];

        if[ $format == 'jpg' || $format == 'jpeg' ] {

            imagejpeg[ $gdImg ];

        } elseif[ $format == 'png' ] {

            imagepng[ $gdImg ];

        } elseif[ $format == 'gif' ] {

            imagegif[ $gdImg ];
        }

        $data = ob_get_contents[];
        ob_end_clean[];

        // Check for gd errors / buffer errors
        if[ !empty[ $data ] ] {

            $data = base64_encode[ $data ];

            // Check for base64 errors
            if [ $data !== false ] {

                // Success
                return "

LachmanskiLachmanski

1013 silver badges5 bronze badges

2

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The base64_encode[] function is an inbuilt function in PHP which is used to convert any data to base64 encoding. In order to convert an image into base64 encoding firstly need to get the contents of file. This can be done with the help of file_get_contents[] function of PHP. Then pass this raw data to base64_encode[] function to encode.

    Required Function:

    • base64_encode[] Function The base64_encode[] function is an inbuilt function in PHP which is used to Encodes data with MIME base64. MIME [Multipurpose Internet Mail Extensions] base64 is used to encode the string in base64. The base64_encoded data takes 33% more space then original data.
    • file_get_contents[] Function The file_get_contents[] function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file.

    Input Image:

    Program:

    Output:

    iVBORw0KGgoAAAANSUhEUgAAApsAAAC4CAYAAACsNSfVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJ
    cEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJdhfdsglgklAEFkb2JlIEltYWdlUmVhZHlxyWqwrwtwefd
    ...
    TeUsalQKBQKhUKhsBvK2FQoFAqFQqFQ2A1lbCoUCoVCoVAo7IYyNhUKhUKhUCgUdkMZmwqFQKBQKO0H0fxpZ1bfc

    Reference:

    • //php.net/manual/en/function.base64-encode.php
    • //php.net/manual/en/function.file-get-contents.php

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

    Chủ Đề