image creation?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
asi0917
Forum Commoner
Posts: 41
Joined: Thu Nov 25, 2004 10:37 am
Location: Shoreline, Washington
Contact:

image creation?

Post by asi0917 »

Code: Select all

<?php
header ("Content-type: image/png");
$im = @imagecreatetruecolor(50, 100)
     or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im,"pic.png");
imagedestroy($im);
?>
<img src="pic.png" border=0>
is it my imagination that this code should output and image that is 50x100 with the text "a simple text string" in it?
it doesn't seem to work and its copied directly form the php.net manual except for the imagepng($im,"pic.png"); bit but ya, that shouldn't make it not work...
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Re: image creation?

Post by andre_c »

assuming that

Code: Select all

<?php
header ("Content-type: image/png");
$im = @imagecreatetruecolor(50, 100)
     or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im,"pic.png");
imagedestroy($im);
?>
and

Code: Select all

&lt;img src=&quote;pic.png&quote; border=0&gt;
are on different files (which they should be), are you getting any errors?

actually if you name you php file image.php, the image tag should say

Code: Select all

&lt;img src=&quote;image.php&quote; /&gt;
User avatar
asi0917
Forum Commoner
Posts: 41
Joined: Thu Nov 25, 2004 10:37 am
Location: Shoreline, Washington
Contact:

next

Post by asi0917 »

now im using this code:

Code: Select all

<html>
<body>
<?php
header ("Content-type: image/png");
$im = @imagecreatetruecolor(50, 100)
     or die("Cannot Initialize new GD image stream");
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im,"image.php");
imagedestroy($im);
?>
<img src="image.php" />
</body>
</html>
with this error:
Error wrote: Warning: Cannot modify header information - headers already sent by (output started at /home/freehost/t35.com/b/l/blkdragon96/image.php:3) in /home/freehost/t35.com/b/l/blkdragon96/image.php on line 4
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

That problem is because you've sent text already to the browser, before you call header(). However, you shouldn't need to call header() anyway. That function is used to send HTTP headers to the browser - it isn't necessary when creating an image.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

like i said, it needs to be 2 files, one with the code, one with the markup

treat the code as if it IS the image
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Looking at it closer, there are other problems. Your code makes sense, but the logic is flawed. The code you showed tries to make a PNG image and save it in image.php. That shouldn't be how you do it.

There's really 2 ways to do what you want. You can have the image creation script run once and store it's output in a graphic, then reference that graphic. Or you can do like ~andre_c said and make the image creation script masquerade as an image file. Either way, there's work to be done.
Last edited by pickle on Tue Jun 07, 2005 4:27 pm, edited 1 time in total.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

i guess i assumed he/she was doing the latter.

pickle is right
Post Reply