Table of Contents
Creating new images
imagecreate()
imagecreatetruecolor()
Opening existing images
imagecreatefromstring()
Other imagecreatefrom*() functions
Destroying images
imagedestroy()
This chapter teaches you how to create and destroy images [2][http://
pterodactyl.l2p.net/book/php5image/page/bi01.html#php_manual].
Creating new images
You can create a new blank image using either the imagecreate() or the
imagecreatetruecolor() functions.
imagecreate()
The imagecreate() function creates a new palette based image.
resource imagecreate() ( x_size,
y_size);
int x_size;
int y_size;
x_size. An horizontal size of an image.
y_size. An vertical size of an image.
Returned Values. An image identifier representing a blank image of
size x_size by y_size.
Example 12.1. The imagecreate() function
$img = imagecreate(468, 60); 1
1
Create a blank image of 468x60 size.
[Tip] Tip
It is recommended to use rather the imagecreatetruecolor() function
(see the section called "imagecreatetruecolor()").
imagecreatetruecolor()
The imagecreatetruecolor() function creates a new true color image.
resource imagecreatetruecolor() ( x_size,
y_size);
int x_size;
int y_size;
x_size. An horizontal size of an image.
y_size. An vertical size of an image.
Return Values. An image identifier representing a blank image of size
x_size by y_size.
Example 12.2. The imagecreatetruecolor() function
$img = imagecreatetruecolor(468, 60); 1
1
Create a blank image of 468x60 size.
[Note] Note
This function requires GD 2.0.1 or later (2.0.28 or later is
recommended).
This function will not work with GIF file formats.
Opening existing images
imagecreatefromstring()
The imagecreatefromstring() function creates a new image from the
image stream in the string.
resource imagecreatefromstring() ( image);
string image;
image. The image stream in the string.
Return Values. An image identifier representing the image obtained
from the given string. These types will be automatically detected if
your build of PHP supports them: JPEG, PNG, GIF, WBMP, and GD2. An
image resource will be returned on success. false is returned if the
image type is unsupported, the data is not in a recognised format, or
the image is corrupt and cannot be loaded.
Example 12.3. The imagecreatefromstring() function
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$img = imagecreatefromstring($data);
?>
Other imagecreatefrom*() functions
There are a bunch of other imagecreatefrom*() functions listed below.
imagecreatefromgif()
imagecreatefromjpeg()
imagecreatefrompng()
imagecreatefromwbmp()
imagecreatefromxbm()
imagecreatefromxpm()
These functions create a new image from file or URL.
resource imagecreatefrom*() ( filename);
string filename;
filename. The image filename.
[Tip] Tip
You can use a URL as a filename with this function if the fopen
wrappers have been enabled.
Return Values. An image identifier representing the image obtained
from the given filename or an empty string on failure.
Example 12.4. The createimagefromgif() function
$img = createimagefrompng('image.gif');
Example 12.5. The createimagefromjpeg() function
$img = createimagefrompng('image.jpg');
Example 12.6. The createimagefrompng() function
$img = createimagefrompng('image.png');
Destroying images
imagedestroy()
The imagedestroy() function destroys an image.
bool imagedestroy() ( image);
resource image;
image. The image identifier returned by one of the image create
functions (see the section called "Creating new images" and the
section called "Opening existing images").
Return values. This function returns true on success or false
otherwise.
This function frees any memory associated with image image.