How to save an image created from imagecreatefromstring() function?

后端 未结 2 755
清歌不尽
清歌不尽 2021-02-18 23:11

Here is my code:

$data = \'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl\'
       . \'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr\'
       . \'EX4IJT         


        
相关标签:
2条回答
  • 2021-02-18 23:25

    This is the correct syntax for imagepng:

    imagepng($im, "/path/where/you/want/save/the/png.png");
    

    According to the PHP manual:

    bool imagepng ( resource $image [, string $filename [, int $quality [, int $filters ]]] )

    filename - The path to save the file to.

    If not set or NULL, the raw image stream will be outputted directly.

    0 讨论(0)
  • 2021-02-18 23:31

    Below code will be helpful:

    $data = 'code in bytes'; // replace with an image string in bytes
    $data = base64_decode($data); // decode an image
    $im = imagecreatefromstring($data); // php function to create image from string
    // condition check if valid conversion
    if ($im !== false) 
    {
        // saves an image to specific location
        $resp = imagepng($im, $_SERVER['DOCUMENT_ROOT'].'folder_location/'.date('ymdhis').'.png');
        // frees image from memory
        imagedestroy($im);
    }
    else 
    {
        // show if any error in bytes data for image
        echo 'An error occurred.'; 
    }
    

    Please suggest if some other better way of doing !

    0 讨论(0)
提交回复
热议问题