Log in

View Full Version : Resolved Is there a limit on how many timed the GD function 'imageline' can be called?



legomanlachlan
12-13-2008, 08:14 AM
I've made a file that returns an image of 1px multicoloured vertical lines.
The default height, width, colour and variance can be overridden with url variables.

The image can be called like this:

<img src="linebar.php" />
With variables:

<img src="linebar.php?w=300&h=100&c[]=123&c[]=43&c[]=204&v[]=19&v[]=65&v[]=37" />

When I use this file to create an image, the lines only go to a width of 255px, the rest of the image is filled with black (the background colour).
Is this the max number of times imageline can be called?

The file used for generating the image is here:


<?php

#height
if(!($h=@$_GET["h"]))$h=50;
#width
if(!($w=@$_GET["w"]))$w=200;
#colour
if(!($c=@$_GET["c"]))$c=array(255,255,255);
#variance (array of {how much red varies, green's variance, ...} )
if(!($v=@$_GET["v"]))$v=array(255,255,255);

#get a random variance of a colour
function randomColour($colour,$variance){
#this is keeping the colour in acceptable margins (0 <= colour <= 255)
if($colour-$variance<0){
$colour+=rand(0,$variance);
}else if($colour+$variance>255){
$colour-=rand(0,$variance);
}else{
$colour+=rand(-$variance,$variance);
};
return $colour;
};

#tell browser to expect a jpeg
header('Content-Type: image/jpeg');

#make the base image
$image = imagecreate($w,$h);

#fill image with black
imagecolorallocate($image, 0, 0, 0);

#for each pixel in width
for($i=0;$i<$w;$i++){
#draw a vertical line with a randomed colour
imageline($image, $i, 0, $i, $h, imagecolorallocate($image, randomColour($c[0],$v[0]), randomColour($c[1],$v[1]), randomColour($c[2],$v[2])));
};

#output the jpeg
imagejpeg($image);

#destroy the image to free memory
imagedestroy($image);

?>

maneetpuri
12-15-2008, 12:09 PM
Hi,

There is no limit to the number of times you can call imageline(), to me it sounds like that there is some problem with the randomColor(), try to change the range you have specified in randomly creating the code for the color. As after a stage it start getting codes which will only produce black color.

Cheers,

~Maneet
Lexolution IT Services
Web Design Services (http://www.lexolutionit.com/services.php)

legomanlachlan
12-16-2008, 12:08 AM
I found out why, it's because I made the image with imagecreate and not imagecreatetruecolor. imagecreate must have a 256 color limit.

djr33
12-16-2008, 12:23 AM
Good that you figured it out, and I can confirm there is a 256 color limit.