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

Php gd image to base64

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:

    Php gd image to base64

    Program:

    Output:

    iVBORw0KGgoAAAANSUhEUgAAApsAAAC4CAYAAACsNSfVAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJ
    cEhZcwAADsQAAA7EAZUrDhsAAAAZdEVYdFNvZnR3YXJdhfdsglgklAEFkb2JlIEltYWdlUmVhZHlxyWqwrwtwefd
    ...
    TeUsalQKBQKhUKhsBvK2FQoFAqFQqFQ2A1lbCoUCoVCoVAo7IYyNhUKhUKhUCgUdkMZmwqFQKBQKO0H0fxpZ1bfc

    Reference:

    • http://php.net/manual/en/function.base64-encode.php
    • http://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.

    ctrl + c
    imagecreatetruecolor

    creates true color GD image object with specified width & height

    imagefilledellipse

    creates ellipse with specified coordinates, radius and color

    ob_start()

    start output buffering

    imagePng($im)

    output $im image in PNG format

    ob_get_clean()

    return contents of output buffer (and clean it after that)

    $base_64

    will contain base64-encoded image


    How to convert image to base64 in PHP?

    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.

    How do you base64 encode in PHP?

    PHP | 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.

    How do I encode an image in base64?

    How to convert image to Base64 online.
    Choose the source of image from the “Datatype” field..
    Paste the URL or select an image from your computer..
    If necessary, select the desired output format..
    Press the “Encode image to Base64” button..
    Download or copy the result from the “Base64” field..

    How decode base64 in PHP?

    The base64_decode() is an inbuilt function in PHP which is used to Decodes data which is encoded in MIME base64. Parameters: This function accepts two parameter as mentioned above and described below: $data: It is mandatory parameter which contains the encoded string. $strict: It is an optional parameter.