简体   繁体   中英

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

Here is my code:

$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
       . 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
       . 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
       . '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);

$im = imagecreatefromstring($data);
if ($im !== false) {
    header('Content-Type: image/png');
    imagepng($im);
    imagedestroy($im);

}
else {
    echo 'An error occurred.';
}

I'd like to save the image generated this way to a directory. How do I do this?

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.

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 !

Q: What if image is JPG or GIF?

A: When you use imagecreatefrom... methods, the image is loaded into memory as the uncompressed bitmap. there is not really a image type at this point. you can save it back out as whatever type you wish using the image... function.
Source: https://stackoverflow.com/a/7176074/3705191

But what if we wanted to Determine the Filetype of Byte String?

Before using imagecreatefromstring( $data_string ) , the actual $data_string you're providing as the argument for that function can be used to determine the image type:

$data = base64_decode($data);
// imagecreatefromstring( $data ); -- DON'T use this just yet

$f = finfo_open();

$mime_type = finfo_buffer($f, $data, FILEINFO_MIME_TYPE);
// $mime_type will hold the MIME type, e.g. image/png

Credit: https://stackoverflow.com/a/6061602/3705191

You'll have to compare the string you get with the MIME type of common image files ( image/jpeg , image/png , image/gif , etc.).

$im = imagecreatefromstirng( $data );

if( $mime_type == "image/png" )
  imagepng( $im, "/path/where/you/want/save/the/png.png" );
if( $mime_type == "image/jpeg" )
  imagejpeg( $im, "/path/where/you/want/save/the/jpg_image.jpg" );
// ...

Check this List of Common MIME Types for reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM