PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 10, 2022

[FIXED] How to save an image using PHP and GD

 October 10, 2022     gd, php     No comments   

Issue

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


Solution

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. E.g.

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



Answered By - danishgoel
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing