Last updated on June 9th, 2022 at 10:27 am

We can create image dynamically using PHP, Just Copy And Paste the Code below.
Any format of this type can be created [.JPG,.GIF,.PNG]. Make sure that you have GD library installed before doing this.

We are going to create an image with size 400×200 and color red. More details can be found in this PHP documentation. We are drawing string to the image using imagestring()

According to PHP.Net they recommend the use of imagecreatetruecolor() instead of imagecreate() so that image processing occurs on the highest quality image possible. If you want to output a palette image, then imagetruecolortopalette() should be called immediately before saving the image with imagepng() or imagegif(). Make sure you have sufficient write access in the location where you save the script because it creates a file with the name image.jpg. You can name your image anything

<?php
create_image();
echo "View Image Created <a href='image.jpg'>Image</a>";
function create_image(){
$im = @imagecreate(400, 200) or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 255, 0, 0); // red
$textcolor = imagecolorallocate($im, 0, 0, 255);
imagestring($im, 10, 0, 0, 'Hello from mistonline.in', $textcolor);
imagepng($im,"image.jpg");
imagedestroy($im);
}
?>

Demo

4 thoughts on “How to generate Image Using PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *