Hudzilla.org - the homepage of Paul Hudson
Contents > Multimedia > Images Wish List | Report Bug | About Me ]

11.2.1     Creating new images: imagecreate(), imagedestroy(), imagecolorallocate()

This is NOT the latest copy of this book; click here for the latest version.

resource imagecreate ( int x_size, int y_size)

int imagedestroy ( resource image)

int imagecolorallocate ( resource image, int red, int green, int blue)

It's time to go from theory to practice and get stuck in with image creation! Here is what we've got so far:

<?php
    $image
= imagecreate(400,300);
    
// do stuff to the image
    
imagejpeg($image, '', 75);
    
imagedestroy($image);
?>

Go ahead and save that in your public HTML directory as picture1.php. As most of your pictures will probably be referenced from a web page, we will also make a companion web page. Save this as phppicture.html:

<HTML>
<TITLE>PHP Art</TITLE>
<BODY>
PHP woz 'ere:
<IMG SRC="picture1.php" />
</BODY>
</HTML>

That is all you need to get started - open up your web browser and load in phppicture.html - you should see a large black box for the image.



Author's Note: Be sure not to have anything outside the PHP code block, not even an empty line. Everything outside the PHP block is sent to the browser as part of the picture, and even having a single space character at the end of the file will cause problems.

The next step is to add a little colour in place of the "do stuff to the image" comment, and for this task we will enlist the help of imagecolorallocate(). This new function takes four parameters - the image resource you are choosing a colour for, then three integers between 0 and 255, one each for the red value, then green value, and the blue value of the colour. You can also specify these colours in hexadecimal format (e.g. 0xff) rather than decimal.

The first colour you allocate is automatically used as the background colour for your image, so this next piece of code is a minor modification of the last script to include colour information...

<?php
    $image
= imagecreate(400,300);
    
$gold = imagecolorallocate($image, 255, 240, 00);
    
imagepng($image);
    
imagedestroy($image);
?>

Save that over picture1.php, and refresh phppicture.html - you should see the black square turn replaced by a yellow square. Not a big step forward, but a step forward nonetheless!

Author's Note: Don't worry about deallocating colours, as they are just numbers and not resources, meaning they don't use up any special memory. If you really want to deallocate a colour (perhaps if you're working with a paletted image), use the imagecolordeallocate() function.





<< 11.2 Images   11.2.2 Choosing a format >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
pascy@gmx.at - 20 Aug 2008

Hi,

I have a different question!
Taking your example above:
If the user would like to save the image with mouse right-click "Save image as" the name will be picture1.php and not imagename.png as it would be nice from a user perspective...

Can you tell a solution for that problem ?
Or what & where i should look for :-(

thanks
Paul

A PHP User - 20 Aug 2008

I also cannot call PHP_GD2.DLL with Wamp. I've double checked and restarted everything.

A PHP User/nat.rodri@gmail.com - 20 Aug 2008

hello I'm using PHP 4.4.4 in Macosx and if i use apache apear fatal error imagecreate() in ....

how can I fixed, I think that the problem is from apache but i dont know???

john - 20 Aug 2008

Like Philippe, I get "Fatal error: Call to undefined function: imagegpng() in D:\mystuff\count.php on line 61."

My php_gd2.dll is loading correctly. When I look at a phpinfo script I see:
GD Support: enabled
GD Version: buldled (2.0.23 compatible)
FreeType Support: enabled
FreeType Linkage: with freetype
GIF Read Support: enabled
JPG Support: enabled
PNG Support: enabled
WBMP Support: enabled
XBM Support: enabled

The previous functions in the script work: imagecreate, imagestring, header("Content-type: image/png"). But imagepng($im) fails.

Any hints?

Thanks,
John

john - 20 Aug 2008

Like Philippe, I get "Fatal error: Call to undefined function: imagegpng() in D:\mystuff\count.php on line 61."

My php_gd2.dll is loading correctly. When I look at a phpinfo script I see:
GD Support: enabled
GD Version: buldled (2.0.23 compatible)
FreeType Support: enabled
FreeType Linkage: with freetype
GIF Read Support: enabled
JPG Support: enabled
PNG Support: enabled
WBMP Support: enabled
XBM Support: enabled

The previous functions in the script work: imagecreate, imagestring, header("Content-type: image/png"). But imagepng($im) fails.

Any hints?

Thanks,
John

A PHP User - 20 Aug 2008

Using apache 2 php 5 installed on windows xp home edition, I have the same problems. I am certain that the extension_dir and php_gd2.dll parts of php.ini are all settled.
what can I do next?

A PHP User - 20 Aug 2008

Using apache 2 php 5 installed on windows xp home edition, I have the same problems. I am certain that the extension_dir and php_gd2.dll parts of php.ini are all settled.
what can I do next?

A PHP User - 20 Aug 2008

Using apache 2 php 5 installed on windows xp home edition, I have the same problems. I am certain that the extension_dir and php_gd2.dll parts of php.ini are all settled.
what can I do next?

A PHP User - 20 Aug 2008

make sure that extension_dir = "C:\php\ext" is directed where it should go.

Sam - 20 Aug 2008

I'm still having issue after removing the comment in the php_gd2.dll line in the php.ini file. Now the error message I get is "Unable to load dynamic library './php_gd2.dll' - The specified module could not be found.

I'm using WinXP and IIS.

Sam - 20 Aug 2008

I'm still having issue after removing the comment in the php_gd2.dll line in the php.ini file. Now the error message I get is "Unable to load dynamic library './php_gd2.dll' - The specified module could not be found.

I'm using WinXP and IIS.

A PHP User - 20 Aug 2008

I am getting the same error as philippe. Undefined function and a red cross where the picture should be. Using apache 2 php 5 and mysql server 5

Philippe - 20 Aug 2008

And that this line extension_dir = "c:\php\ext" points to the correct directory ( in windows )

Philippe - 20 Aug 2008

Make sure the extension=php_gd2.dll line in your php.ini is not commented out !!

Philippe - 20 Aug 2008

When I try this code I'm nog getting any images, just a red cross.

When I try to execute the php code I get the following message : "Fatal error: Call to undefined function imagecreate() in ..."



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus ten?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow