Creating png images

Hi, ive been attempting to delve deeper into the world of php, more directly creating dynamic images using php. So went for a bit of a look around, and found some really useful stuff on php.net (real good site that one).
Anyway yeah, was a bit confused about all these extra things they were talking about here. Do i need the gD thingy or the libPNG?? I have Apache 2.0.54 running on my windows xp, with php 5.1.2. Just installed it all pretty much standard i think.

Anyway yeah, so i found this script on that page on php.net and well, it dont work:

<?php

header("Content-type: image/png");
$string = $_GET['text'];
$im    = imagecreatefrompng("images/button1.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px    = (imagesx($im) - 7.5 * strlen($string)) / 2;
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);

?> 

It comes up with this when i try it out:

<br />
<b>Fatal error</b>:  Call to undefined function imagecreatefrompng() in <b>E:\SERVER\Apache2\htdocs\php\pic\string.php</b> on line <b>5</b><br />

I am passing a variable to text, and have a png in the correct place (well at least i think it is), but yeah the error says imagecreatefrompng() is an undefined function. Whats going on. Is that part of the gD thingy that i need to install??

Thanks in advance, this is php stuff is all a bit intense.

-Aussie Devil

You’ll be needing GD installed. No idea what libPNG is, but it won’t be needed for the script you provided.

I personally have never installed a library, so can’t help you there. Most of my hosts, as well my local apache server has GD preinstalled (xampp package).

PHP comes bundled with a gd library, but you should check to see if it’s configured.

Toss <?php phpinfo(); ?> into a file and run it on your server.
At the top you should see “Configure Command” and to the right of that some info. Check and see if that info has something like: “–with-gd=shared” (the =shared may not be there).

Or you can always try something simple like:

<?php
header("Content-type: image/png");
$im = imagecreate(100, 100);
$black = imagecolorallocate($im, 0, 0, 0);
imagepng($im);
imagedestroy($im);
?>

If --with-gd is not there or the php code above doesn’t work, let us know. Otherwise, keep on reading…

You may want to grab the GD2 library and set that up instead. It’ll give you more support and more things to play with. I believe you can get it from: http://www.boutell.com/gd/ – otherwise I can always email you the php_gd2.dll file.

Once you have that put that into your php directory (same location as your php.exe file is) and then go and open your php.ini file.

Search for “extension=” in the php file for a list of extensions (probably around line 556 or so). Then you’ll put the following line in by the other extensions:

extension=php_gd2.dll

(Note if the gd2 library you download is called something different then name it whatever it’s called – it should be php_gd2.dll more than likely)

Or if that’s already there, simply uncomment the line (remove the ; before “extension”). Make sure to restart the server.

You should be set then.

Thanks for your very comprehensive post Ankou. I went through and tried all your thing, here are the results:

  1. In the phpinfo() “–with-gd=shared” was there.

  2. Tried you simple code and still came up with the similar error as before, not recognising imagecreate() as a function.

  3. Went to that site u sent me, had had a look around, but couldnt find where to download the dll from, there was one link bout halfway down that said this “Windows DLL .zip (NOT CURRENTLY FOR PHP)”, tried downloading that, but i was a zip file and had a bunch of exes and txt files in there, no dll’s soo, abandonended that.

  4. Then had a look in the php folder, and found “php_gd2.dll” in the ext folder?? whats goin on there, then i had a look in the ini file (there were two btw: “php.ini-dist” and “php.ini-recommended”. I tried adding “extension=php_gd2.dll” to the recommended one, but it didnt seem to work. Might have a bit more of a fiddle with it after i finish posting here.

Thanks once again.

-Aussie Devil

Yeah I found this morning that php_gd2.dll comes with the PHP bundle, I forgot to come and post that. Sorry!

Okay you should have a file called php.ini (just that, no -dist or -recommended after it). It should be found in your apache folder. apache/apache2/ or apache group/apache2/ – if it’s not there then do a search for php.ini on your computer and you should find it.

When you do find it and open it search for extension_dir and see what that value is. This is where you’ll need to put the php_gd2.dll file that you have.

For example my extension_dir line in the php.ini file looks like so:
extension_dir = “./”

That’s in relation to where your php.exe file is. If that line reads:
extension_dir = “./ext”

Then you would need to have the php_gd2.dll file in the ext directory of where your php.exe file is found.

Example…
My php.exe file is found at: *F:\php*
If my extension_dir line read: extension_dir = "./ext"
I would put php_gd2.dll in: *F:\php\ext*

Once you have that set, then make sure that you uncomment the extension=php_gd2.dll line in your php.ini file.

Restart the server and give that basic script I posted before a try.

The only other thing I can think to check would be to make sure that your Path variable is set for PHP and PHP\ext.

  1. Open System (Start > Control Panel > System OR Hold windows key and press pause/break key)

  2. Click on the “Advanced” tab

  3. Click on “Environment Variables” button

  4. Under “System variables” find “Path” and and double click that line

  5. You should be able to see the “Variable Value” – look for your path to PHP
    (Note: it may be easier to copy the variable value and paste it into a text editor)

What you’re looking for is to make sure that your path to php.exe is in the value. Remember above when I said my php.exe file was found in F:\php? I want to make sure that that’s in my path variable value. I also want to make sure that F:\php\ext is there as well.

If it’s not there you need to add it. Just make sure it’s the path to where your php.exe file sits!

Also don’t add the trailing slash to the directory either. So do NOT put *F:\php* or F:\php\ext

After you have those there (if they weren’t there). Save the variable (click ok). Then restart the computer, fire up your server and give making a PHP image another try.

BTW – if that doesn’t work, I’ll keep hunting around for other possible solutions.

Im thinking my apache and php installation are messed up. I couldnt find the php.ini file anywhere, quite strange, i guess ill reinstall them tomorrow, i originally used the tutorial here. To install it, any other tips or advice u can give me when reinstalling??

Also i just had another query, how can i move the actually web folder somewhere other than Apache2\htdocs folder? cause it gets a bit annoying having to navigate through 4 subfolders to get to the actual site.

Thanks once again for all your terrific help.

-Aussie Devil

EDIT: I was just looking through that tutorial about installing apache and php, and i realised that i had placed php.ini into my windows folder, like the tutorial said to do. Then went and opened that up and found the gd2 extension line, uncommented it. and now it works!!! :hugegrin:

Thank you! Thank you! Thank you! :love:

-Aussie Devil

laugh I finished reading the first paragraph in your previous post and thought, “I wonder if aussie devil skipped putting the php.ini file where it needs to be?”. Then I saw your edit!!

Glad to hear it’s all working. Enjoy playing with the gd/gd2 libraries!