Updated December 8, 2023
What is imagecreate in PHP?
In PHP, the imagecreate function is used to create a new image resource, which can be manipulated and modified using various image functions provided by the GD (Graphics Draw) library. This function initializes a blank image canvas with a specified width and height.
Table of Contents
Syntax
$image = imagecreate($width, $height)
Parameters
It accepts two parameters, i.e., width and height, as seen in the above code. As the name suggests, width and height are the image’s dimensions.
- $width: The width of the image.
- $height: The height of the image.
The function returns an image resource identifier, which is then used in other GD library functions to perform operations on the image, such as drawing lines, adding text, or manipulating pixels.
Working of imagecreate() in PHP
Before directly looking at the primary function, let us check our prerequisites.
If you use the Windows operating system and PHP, you should also install the XAMPP control panel and start Apache.
A very common error is “Fatal error “users get while using imagecreate(). Imagecreate is undefined. You must make a few changes to one of your core Xampp files to solve this. After installing XAMPP, go inside the folder and look for the “php” folder. Inside the php folder, look for the “php.ini” file. It is a configuration file. Open this file in any editor and search for the “gd.”
As you can see, “extension = gd” is commented using “;” Uncomment it by removing “;” Once you uncomment, it should look like the code below.
You need to make one more change in the same file. Search for “gd” again, and you will find this.
Uncomment the last line, “gd.jpeg_ignore_warning = 1,” and save the file. Restart the Apache from the Xampp control panel to reflect the changes.
Now, you can start using the imagecreate function; it won’t give you any fatal errors.
The example below will create an image with some dimension and a simple background color.
Code:
<?php
// 1. Giving dimensions to the image
$image = imagecreate(500, 300);
// 2. Giving background color to the image
$bckgrnd_color = imagecolorallocate($image, 213, 40, 217);
// 3. Applying the background color to the image
imagefill($image, 0, 0, $bckgrnd_color);
header("Content-Type: image/png");
imagepng($image);
image destroy($image);
?>
In step 1, we use the imagecreate function and define its width = 500 and height = 300. After that, we will depict a background color. This color is in RGB format. And in step 3, allot that color to the image.
Output:
Examples of Imagecreate() Function Using PHP
Below are the different examples of using the imagecreate() function in PHP:
Example #1
Creating a simple image with some text
Code:
<?php
// 1. Giving dimensions to the image
$image = imagecreate(500, 300);
// 2. Giving background color to the image
$bckgrnd_color = imagecolorallocate($image, 185, 102, 81);
// 3. Giving text color
$txt_color = imagecolorallocate($image, 255, 0, 0);
// 4. Using a function to execute an image containing text.
image string($image, 5, 180, 100, "Hi, testing imagecreate()", $txt_color);
header("Content-Type: image/png");
imagepng($image);
image destroy($image);
?>
Output:
In the above code, we use the imagecreate function, define the width and height, and give the background color. In step 3, we define a color for our text; in step 4, we write and provide the text color.
Example #2
Modifying the existing image and giving new text, new text color, and new background color.
Code:
<?php
// import the image you want to modify with the proper extension.
$existingImage = imagecreatefrompng('your_path/your_image.png');
// Set your new modifications
$newText = "New color and text";
$newTextColor = imagecolorallocate($existingImage, 255, 0, 0);
$newBackgroundColor = imagecolorallocate($existingImage, 15, 74, 6);
// Execute the modifications of text
image string($existingImage, 5, 180, 100, $newText, $newTextColor);
// Execute the modifications of the background color
imagefill($existingImage, 0, 0, $newBackgroundColor);
header("Content-Type: image/png");
imagepng($existingImage);
imagedestroy($existingImage);
?>
Output:
Start by importing the image you want to edit. Use the imagecreatefrompng() function. Later, define your modification (new text, new text color, and new background color). Using the imagestring() function gives the new text and text color, and using the imagefill() function gives the new background color.
Example #3
Drawing a shape and giving color to the shape and color to the background
Code:
<?php
// 1. Giving dimensions to the image
$image = imagecreate(500, 300);
// 2. Set the vertices of the polygon
$values = array(
50, 50,
50, 250,
250, 50,
250, 250
);
// 2. Giving background color to the image
$background_color = imagecolorallocate($image, 213,40,217);
imagefill($image, 0, 0, $background_color);
// Giving color to the polygon
$image_color = imagecolorallocate($image, 255, 255, 255);
// Execute polygon
imagepolygon($image, $values, 4, $image_color);
header('Content-type: image/png');
imagepng($image);
?>
Output:
Do the basic steps of creating an image with dimensions and giving a background color. For creating a polygon shape, 1st set the vertices of the polygon as shown in the step and then provide the color to the lines of the polygon. After doing this, use the imagepolygon function. It accepts four parameters, i.e., background image, values of a polygon, total number of items in the array, and color of the polygon lines.
Example #4
Drawing a Hexagon
Code:
<?php
// Create a blank image
$width = 500;
$height = 500;
$image = imagecreatetruecolor($width, $height);
// Set background color
$backgroundColor = imagecolorallocate($image, 90, 128, 216);
imagefill($image, 0, 0, $backgroundColor);
// Define hexagon points
$centerX = $width / 2;
$centerY = $height / 2;
$size = 100; // Adjust the size of the hexagon
$points = array();
for ($i = 0; $i < 6; $i++) {
$angle = deg2rad(60 * $i); // 60 degrees for each corner
$x = $centerX + $size * cos($angle);
$y = $centerY + $size * sin($angle);
$points[] = $x;
$points[] = $y;
}
// Set hexagon color
$hexagonColor = imagecolorallocate($image, 0, 0, 0);
// Draw the hexagon
imagepolygon($image, $points, count($points) / 2, $hexagonColor);
// Output the image
header('Content-Type: image/png');
imagepng($image);
// Free up memory
imagedestroy($image);
?>
Output:
Common Use Cases
The imagecreate function is generally used while dealing with palette images. The palette images are those images that use a restricted (limited) set of colors. Some of the common use cases are
- Generating icons and favicons: These images are small and can be created with colors.
- Generating pixel art: Similar to icons, pixel art is small and repeats color sets.
- Creating Thumbnails: Thumbnails are creative and colorful headings. These can be easily executed using Thumbnail.
- Creating Heatmaps: Heatmaps visually represent data using colors. Standard colors, like red, yellow, blue, etc., are usually used in heatmaps. Hence, imagecreate is capable of creating heatmaps.
- Image Altering: The imagecreate function allows users to import images and make changes according to their needs.
Best Practices and Tips
- AEnsureApache is started, and changes are made in the php.ini file to avoid fatal errors.
- Make a habit of freeing up space using the “imagedestroy($your_image).”
- Try to define each component differently. This will keep your code well organized and make it reusable.
$newText = "New color and text";
$newTextColor = imagecolorallocate($existingImage, 255, 0, 0);
$newBackgroundColor = imagecolorallocate($existingImage, 15, 74, 6);
- If the code is too long and confusing, use comments so that you will understand what the steps are performing every time you go through the code.
- When importing images, give the path and extension properly.
- Be Upgraded with the libraries like “gd.”
- Make sure you know the use of RGB color format. It is extensively used while defining colors and creating images.
Conclusion
The imagecreate() is a useful function to generate and manipulate images by incorporating lines, shapes, colors, and text into images, which are useful in graphic design and development. The imagecreate function creates charts, graphs, thumbnails, image watermarks, and user avatars. It has diverse benefits, from defining basic width and height parameters to generating custom images.
Frequently Asked Questions (FAQ’s)
Q1. Can I create different types of images using the ‘imagecreate()’ function?
Answers: Yes, different types of images can be created by adding a third parameter for specifying image type to the ‘imagecreate()’ function
Example: imagecreate(100, 50, IMG_PNG)
It will create a PNG image.
Q2. What are the issues faced while using imagecreate()?
Answers: GD library not installed appropriately, less memory limit, and invalid input writing.
Q3. What is the imagecreatetruecolors() function?
Answers: By using the imagecreatetruecolors() function, we get access to many color options. It is also capable of handling images with color gradients.
Q4. What is the imagefilledpolygon() function?
Answers: Imagefilledpolygon is similar to imagepolygon, but the difference is that imagepolygon creates an outline of the polygon while imagefilled polygon creates a filled polygon.
Q5. Is it possible to resize an image in PHP?
Answers: Using functions like the imagecopyresampled() function makes it possible to resize the image in PHP, and it also maintains the clarity of the picture.
Recommended Articles
We hope that this EDUCBA information on “PHP Imagecreate” benefited you. You can view EDUCBA’s recommended articles for more information,