Subject: Working with ImageCreate in PHP?
Posted By: jwang Post Date: 2/13/2004 10:14:34 PM
I was downloaded the GD library but I can't config to PHP.ini for Apache run the lib gd.dll. I want to use the ImageCreate function, how can I do?
Thank for your help!
Reply By: richard.york Reply Date: 2/14/2004 12:04:48 AM
Jwang,

First let's verify your installation of gd in PHP.
For Windows, check the following:

1.)  Be sure that you have downloaded and installed the Windows zip package from http://www.php.net/downloads.php which includes many of the extra PHP extensions, including gd, if you used the Windows Installer extra extensions were not included, no worries though, just download the above package and move all the extra stuff in it to your PHP directory.  But if you're running PHP as an Apache module you've likely already downloaded this package... check the following php.ini settings...

2.)  Open php.ini (C:\Windows\php.ini) on a windows system.
Uncomment the line..
extension=php_gd2.dll

Modify the following line to point to the extensions directory.
extension_dir = "C:\PHP\extensions\"

For Linux:
See the gd documentation available at http://www.php.net/gd for configuration options.

Restart Apache and run the following script:
<?php
phpinfo();
?>

If you see the gd heading followed by a list of gd configurations, then the install was successful.

<?php
    header("content-type: image/png");
    $im = @ImageCreate(150, 150) or die("Could not create image");
    $bg_color = ImageColorAllocate($im, 240, 240, 240);
    $text_color = ImageColorAllocate($im, 0, 0, 0);
    ImageRectangle($im, 40, 40, 140, 140, $text_color);
    ImageString($im, 3, 5, 5, "Square", $text_color);
    ImagePng($im);
    ImageDestroy($im);
?>

This code looks ok to me.  What errors or warnings do you see when you run it?  (Ran independently of anything else, e.g. not from within the 'src' attribute of an img tag.)

Also it doesn't make sense to suppress errors in an experimental script, remove the '@' error supression operator from the call to ImageCreate().

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

Reply By: richard.york Reply Date: 2/14/2004 12:14:53 AM
Also Jwang,

I just noticed you posted a test URL... http://www.phucuong.net/testPNG.php...

Run this modified copy...  
Image output doesn't happen until the imagepng function is called, so in order to see errors issued previous to that call move the call to header to happen just before the imagepng function call.  The reason for this is you are changing the content of your PHP script to become an image, so the browser will treat it like one.. which more or less means your errors are lost to oblivion.

Also leave off the error supression operator that I already mentioned and this should show you some errors.  Also be sure that you are reporting errors while you are developing, set this php.ini directive to display all errors..

error_reporting = E_ALL

This is best done in a development environment where you are the only person that will see the errors, whereas on your live server this may pose a security risk and well simply be unprofessional.

<?php
    $im = ImageCreate(150, 150) or die("Could not create image");
    $bg_color = ImageColorAllocate($im, 240, 240, 240);
    $text_color = ImageColorAllocate($im, 0, 0, 0);
    ImageRectangle($im, 40, 40, 140, 140, $text_color);
    ImageString($im, 3, 5, 5, "Square", $text_color);

    header("content-type: image/png");
    ImagePng($im);
    ImageDestroy($im);
?>

hth,
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::

Reply By: jwang Reply Date: 2/24/2004 4:02:07 AM
Give me an example, please and I can test it on your url site?
Thanks a lot!

Reply By: nikolai Reply Date: 2/24/2004 12:50:49 PM
The example is given to you in source code.  You should be able to run it on your own site if you have PHP with GD installed properly.

You can verify that GD is installed by looking for it in a <?php phpinfo(); ?> script.


Take care,

Nik
http://www.bigaction.org/

Go to topic 10173

Return to index page 942
Return to index page 941
Return to index page 940
Return to index page 939
Return to index page 938
Return to index page 937
Return to index page 936
Return to index page 935
Return to index page 934
Return to index page 933