imagecreate(); [solved]

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
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

imagecreate(); [solved]

Post by anthony88guy »

I’m really excited about making images on the fly. From a couple tutorials here is the code I came up with:

Code: Select all

<?php
header("Content-type: image/png");

$image['height'] = 50;
$image['width'] = 200;
$image['bgcolor'] = imagecolorallocate($img, 0, 0, 0);
$image['textcolor'] = imagecolorallocate($img, 204, 204, 204);
$image['textsize'] = 12;
$image['text'] = 'testing';

$img = imagecreate($image['height'], $image['width']);
imagefill($img, 0, 0, $image['bgcolor']);
imagestring($img, $image['textsize'], 5, 5, $image['text'], $image['textcolor']);

imagepng($img);
?>
When I run the script I receive the following error:
The image “URL” cannot be displayed, because it contains errors.
I know I haven’t specified a font, I am wondering if that could be the culprit?

Thanks.

[edit]
Viewing the page in IE, I receive the following error:
<br />
<b>Warning</b>: imagecolorallocate(): supplied argument is not a valid Image resource in <b>/home/theelite/public_html/members/includes/image.php</b> on line <b>6</b><br />
<br />
<b>Warning</b>: imagecolorallocate(): supplied argument is not a valid Image resource in <b>/home/theelite/public_html/members/includes/image.php</b> on line <b>7</b><br />
‰PNG


IHDR2È
Last edited by anthony88guy on Wed Mar 15, 2006 5:18 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

ensure that you do not have any character data being output besides the script's image.
anthony88guy
Forum Contributor
Posts: 246
Joined: Thu Jan 20, 2005 8:22 pm

Post by anthony88guy »

feyd wrote:ensure that you do not have any character data being output besides the script's image.
Nothing besides the image is being outputted.
SKDevelopment
Forum Newbie
Posts: 13
Joined: Thu Jan 26, 2006 10:42 am

Post by SKDevelopment »

imagecolorallocate() should use the valid resource $img. Try this:

Code: Select all

<?php 
header("Content-type: image/png"); 

$image['height'] = 50; 
$image['width'] = 200; 
$img = imagecreate($image['height'], $image['width']); 

$image['bgcolor'] = imagecolorallocate($img, 0, 0, 0); 
$image['textcolor'] = imagecolorallocate($img, 204, 204, 204); 
$image['textsize'] = 12; 
$image['text'] = 'testing'; 


imagefill($img, 0, 0, $image['bgcolor']); 
imagestring($img, $image['textsize'], 5, 5, $image['text'], $image['textcolor']); 

imagepng($img); 
?>
--
Best Regards,
Sergey Korolev
www.SKDevelopment.com
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well, you have those warnings.. which should lead you somewhere. :P
Post Reply