简体   繁体   中英

How to save an image using PHP and GD

I am created dynamic image using php and gd library as follows,

imagecopy ($iOut,$imgBuf[0],$left,$top,0,0,imagesx($imgBuf[0]),imagesy($imgBuf[0]));
imagedestroy ($imgBuf[0]);


ob_start();
    imagepng($iOut);
    printf('<img src="data:image/png;base64,%s"/>', base64_encode(ob_get_clean()));

How can I save this image to my local directory for further use. Any help please

Thanks

Look at the imagepng documentation at http://php.net/manual/en/function.imagepng.php

You can pass in a second parameter to the function as

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

So you can do

imagepng($iOut, $filename_to_save_to);

then you can simply display the image in the browser as

echo '<img src="' . $public_visible_path_to_saved_file . '"/>';

For this to work, i would choose the $filename_to_save_to as a file in a subdirectory of your web root. Eg

if your web root is /var/www i would choose /var/www/uploaded_images/filename.png then you can simply display it by specifying the $public_visible_path_to_saved_file as uploaded_images/filename.png

This will work if you want to save the files to your server:

ob_start();
imagepng($iOut);
$data = ob_get_clean();
file_put_contents('/my/file.png',$data);

function render image with php GD

   function render_image_page() {

    header ('Content-type: image/png');

    $im = @imagecreatetruecolor(120, 20) or die('Cannot Initialize new GD image stream');
    $text_color = imagecolorallocate($im, 233, 14, 91);
    imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
    imagepng($im);
    imagedestroy($im);exit;

}

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