Help with image processing

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
chiyosdad
Forum Newbie
Posts: 4
Joined: Wed Dec 24, 2003 11:46 am

Help with image processing

Post by chiyosdad »

Hi,
I'm trying to write a script that graphs my site traffic data for me. I'm not familiar with imaging with php, so I'm just playing around right now.
First, I ran the phpinfo and verfied that gd is supported:
gd
GD Support enabled
GD Version 1.6.2 or higher
WBMP Support enabled
So I practically copied a sample program:

Code: Select all

<?php

if(!($im = imagecreatetruecolor(500, 250)) echo "error creating im";

$tc = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 1, 5, 5,  "This is not a Hello World", $tc);

header("Content-type: image/jpg");
imageJPEG($im);
imagedestroy($im);
?>
And it didn't work. I tried looking at it from an html page with the img tag as well, and it gave me one of those red crosses. The worst thing is, the red cross isn't 500x250 big. Anyway, any help would be appreciated.
Also, I'm not really sure what that header() line does for me. Shouldn't it also work if I just removed that line?
Thanks.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

there's a syntax error in that script.
if(!($im = imagecreatetruecolor(500, 250)) echo "error creating im";
must be
if(!($im = imagecreatetruecolor(500, 250))) echo "error creating im";

also note:
http://de.php.net/manual/en/function.imagecreatetruecolor.php wrote:imagecreatetruecolor() returns an image identifier representing a black image of size x_size by y_size.
writing black text onto a black-image doesn't make much sense, does it? ;)
chiyosdad
Forum Newbie
Posts: 4
Joined: Wed Dec 24, 2003 11:46 am

Post by chiyosdad »

Ah yeah, that was a typo, but it didn't solve the problem.
I went back and fixed that, and also tried to figure out where the problem was. I used

Code: Select all

<?php
if(!($im1 = imagecreate(500, 300))) echo "error1";
if(($tc = imagecolorallocate($im1, 250, 250, 250))==-1) echo "error2";
if(!imagestring($im1, 1, 5, 5, "echo:!", $tc)) echo "error3";

header("Content-type: image/jpeg");

imagejpeg($im1,'',70);

?>
I didn't get any text, just the red cross, so I'm guessing the problem is with imagejpeg().
volka wrote:
http://de.php.net/manual/en/function.imagecreatetruecolor.php wrote:imagecreatetruecolor() returns an image identifier representing a black image of size x_size by y_size.
writing black text onto a black-image doesn't make much sense, does it? ;)
If that was the problem, I should get a black rectangle. But that's not what I got (the red cross). So I don't know what's going on.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

imagecreate works differently. The first color you allocate is the background-color
try this one

Code: Select all

<?php
/* <-- debug-settings */
ini_set('display_errors', TRUE);
if (!function_exists('imagecreate'))
	die('imagecreate does not exist');
if (!function_exists('imagejpeg'))
	die('imagejpeg does not exist');
/* debug-settings --> */	

$im1 = imagecreate(500, 300) or die('imagecreate failed');
$bgcolor = imagecolorallocate($im1, 0, 0, 0);
$textcolor = imagecolorallocate($im1, 255, 255, 255);
if ($bgcolor == -1 || $textcolor == -1)
	die('imagecolorallocate failed');
imagestring($im1, 1, 5, 5, "echo:!", $textcolor) or die('imagestring failed');
header("Content-type: image/jpeg");
imagejpeg($im1,'',70);
?>
chiyosdad
Forum Newbie
Posts: 4
Joined: Wed Dec 24, 2003 11:46 am

Post by chiyosdad »

Yeah, i got
imagejpeg does not exist

So it is time to hound my system admin. Thanks for the help.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

Also, I'm not really sure what that header() line does for me
You need the header, it tells the web browser that the file is an image file, and not plain text or anything
Post Reply