starter's problem with gd

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
caedo
Forum Newbie
Posts: 5
Joined: Tue Jul 22, 2008 12:48 pm

starter's problem with gd

Post by caedo »

A few days ago, I start reading about GD, now I'm trying to do small tutorial to see how the lib works. This is the code I have, is a copy of the starte's tutorial in phpgd.com:

<?php
$image = imagecreate(200,200);
var_dump($image);
$black = imagecolorallocate($image, 0, 0, 0);
$white = imagecolorallocate($image,255,255,255);
imagefilledrectangle($image,0,0,200,200,$white);
imagerectangle($image,0,0,199,199,$black);
?>

Can you see an error that I cant? What I'm doing wrong?

thanks in advance

Caedo.

pd:

btw, this is the info of GD showed by phpinfo():

GD Support enabled
GD Version bundled (2.0.34 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.1.9
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XPM Support enabled
XBM Support enabled
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: starter's problem with gd

Post by onion2k »

Problem 1: You're using var_dump() to output the image information - outputting anything other than headers or image data will break the image. So comment that line out.

Problem 2: Your rectangles are the wrong sizes. The white one, which is from 0,0 to 200,200, is 201 pixels square. That's bigger than the image. It needs to be from 0,0 to 199,199. The black one inside it needs to be from 1,1 to 198,198.

Problem 3: You're not sending the content type header or any image data. You need something like:

Code: Select all

header("Content-type: image/jpeg");
imagejpeg($image);
... that goes at the end of the script.

I really should fix that tutorial some day. :)
caedo
Forum Newbie
Posts: 5
Joined: Tue Jul 22, 2008 12:48 pm

Re: starter's problem with gd

Post by caedo »

thanks for the reply, I change the code to this:

Code: Select all

 
<?php 
    $image = imagecreate(199,199);
    $black = imagecolorallocate($image,0,0,0);
    $white = imagecolorallocate($image,255,255,255);
    $red = imagecolorallocate($image,255,0,0);
    imagefill($image,0,0,$white);
    imagerectangle($image,1,1,198,198,$black);
    header("Content-type: image/jpeg");
    imagejpeg($image);
 
?>
 
Now, it outputs the following:

Code: Select all

 
Warning: Cannot modify header information - headers already sent by (output started at /home//simple.php:2) in /home/simple.php on line 9
ÿØÿà?JFIF??????ÿþ?>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ÿÛ?C?    $.' ",#(7),01444'9=82<.342ÿÛ?C  2!!22222222222222222222222222222222222222222222222222ÿÀ??Ç?Ç"?ÿÄ??????????? ÿÄ?µ???}?!1AQa"q2‘¡#B±ÁRÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ???????? ÿÄ?µ??w?!1AQaq"2B‘¡±Á #3RðbrÑ
 
 
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: starter's problem with gd

Post by onion2k »

You have something being outputted on line 2 of the script that isn't image data. I suspect it's a carriage return or something. Your script can't have anything outside of the PHP tags.
caedo
Forum Newbie
Posts: 5
Joined: Tue Jul 22, 2008 12:48 pm

Re: starter's problem with gd

Post by caedo »

you're right, I start coding on line 2, instead of line 1 , so there was an empty line at the beginning of the script, it's working now.

thanks a lot.

Caedo
Post Reply