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

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

Here is my code:

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


        
2条回答
  •  野性不改
    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 !

提交回复
热议问题