Skip to main content
Using PHP GD Library to Generate an Image

Using PHP GD Library to Generate an Image

hackernoon.comhackernoon.com2 weeks ago in#Dev Love55

PHP GD is a library that performs fantastic tasks that helps you to do several things with your image. As a PHP Developer, There are times when you have to survey some advanced functionality of your stack. And one is the GD library, which is not easily done by most developers. and it helps you do anything with images. If you’re ready, let’s proceed to this PHP amazing library that helps you generate, and draw an image.What is GD Library GD is an acronym that stands for “GRAPHIC DRAW”. The GD library in PHP enables you to create image data, and it’s the most used image processing tool in PHP. GD was originally developed by Thomas Boutell. It is a powerful image-processing library for PHP (it is also available for other languages like Perl).GD library allows you to dynamically create and edit images, charts thumbnails, etc. The following file type that can be created or edited using the GD library, are GIF, JPEG, and PNG.How Does GD Library work You should understand that the GD library has a pattern in which it works. Now if you don’t follow the process of the GD library you won’t see anything on your interface. Below are the steps for implementing the GD library in your project: Step 1You must check if the GD library is enabled in your system’s local server (XAMMP). If it’s disabled you have to enable it, once it’s enabled you are good to go. If yours is enabled you will see a display similar to the image below. To see if the GD library is enabled in your local server, go to your PHP index file and echo **phpinfo();** then you’ll see the GD support in your interface.little example, from (me) Rabbi Emmykolic Step 2: You will go to your **index.php** and create an image tag. And in the src of the image tag, you’ll write the address of the PHP file where you want to write the GD codes. So the image tag will look like this. An Example From (me) Rabbi Emmykolic Now, this is where you’ll write the PHP code. little example, from (me) Rabbi Emmykolic After this step, you’ll start writing some functions of the GD library. In the PHP file your GD will begin to work automatically. Using GD Library in your projects Example1: Using GD Library to create a Color-box with a JPEG extension, when saved. Step1: Create an image with the specified dimensions $image = imageCreate(300,200); Step2: Create a color (this first call to imageColorAllocate also automatically sets the image background color) $colorRed = imageColorAllocate($image, 255,0,0); Step3: Create another color $colorYellow = imageColorAllocate($image, 255,255,0); Step4: Draw a rectangle imageFilledRectangle($image, 50, 50, 250, 150, $colorYellow); Step5: Set type of image and send the output header(“Content-type: image/jpeg”); imageJpeg($image); Step6: Release memory imageDestroy($image); By running the above code, you will get the output seen in the image below: little example, from (me) Rabbi Emmykolic Example 2: How to use GD library to create and rotate a png image. Run the code below to see how to rotate an image.  » Read More

Like to keep reading?

This article first appeared on hackernoon.com. If you'd like to continue this story, follow the white rabbit.

View Full Article
Laravel Vs Symfony: Answering All The Questions To Make a Better Choice

Laravel Vs Symfony: Answering All The Questions To Make a Better Choice

#Dev
How to Channel a Daily Vision into a 20-Year Photography Career

How to Channel a Daily Vision into a 20-Year Photography Career

#Silicon Valley
WordPress Punts Locally Hosted Fonts for Legacy Default Themes to 6.2 Release

WordPress Punts Locally Hosted Fonts for Legacy Default Themes to 6.2 Release

#Web Design
Fresh For Designers

Is the Dynamic Island plain stupid or the next revolutionary UX pattern?

#All

Let's talk about Dev