How to Create an Image in PHP

Introduction

Making a website with PHP is very easy and involves doing many of things, such as creating an image. So this article describes how to create a simple image using PHP. Use the procedure described here to create an image using PHP.

Step 1

Check if "extension=php_gd2.dll" in the "php.ini" file is enabled. To check it use the following procedure.
 

  • Click on the php.ini file

php-ini-file.jpg 

  • If it is checked then uncheck it "extension=php_gd2.dll", as in the following image:

php-ini-uncheck.jpg
Step 2

After unchecking it, restart the Wamp server.

Step 3

You can check whether the service is enabled using the following procedure.


1-Open "NotePad" and enter the following code into Notepad:

<?php
phpinfo();
?>

2-Save it with name of "phpinfo.php".

3-Run this file on localhost, it will display output that looks like the following, which proves that your GD Support has been enabled.

gd.jpg

So the GD service has been enabled. Now you are ready to write the image creating code using PHP.

Save the following code with the name "image.php" in your www root.

<?php

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

    $myImage = imagecreate(200, 100);

    $mycolor = imagecolorallocate($myImage, 100,0,400);

    $myBlack = imagecolorallocate($myImage, 1, 1, 1);

    imageline($myImage, 15, 100, 120, 60, $myBlack);

    imagepng($myImage);

    imagedestroy($myImage);

?>
Run it, it will give output like:

create-image-in-php.jpg
 

Then create another PHP code file with the name "call.php" with the following code:

<html>

  <head>

    <title>Call PHP File</title>

  </head>

  <body>

    <img src="image.php" alt="" />

  </body>

</html>

Output

call-image-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all