|
|
 |
imagecolorallocate (PHP 3, PHP 4, PHP 5) imagecolorallocate -- Allocate a color for an image Descriptionint imagecolorallocate ( resource image, int red, int green, int blue )
imagecolorallocate() returns a color
identifier representing the color composed of the given
RGB components. The image
argument is the return from the imagecreate()
function. red, green and
blue are the values of the red, green and blue
component of the requested color respectively. These parameters are
integers between 0 and 255 or hexadecimals between 0x00 and 0xFF.
imagecolorallocate() must be called
to create each color that is to be used in the image represented
by image.
Note:
The first call to imagecolorallocate() fills
the background color.
Returns -1 if the allocation failed.
See also imagecolorallocatealpha() and
imagecolordeallocate().
User Contributed Notes
imagecolorallocate
mv at brazil dot com
15-Mar-2005 01:34
<?
$hexcolor = str_split($_GET["color"], 2);
$bincolor[0] = hexdec("0x{$hexcolor[0]}");
$bincolor[1] = hexdec("0x{$hexcolor[1]}");
$bincolor[2] = hexdec("0x{$hexcolor[2]}");
$im = ImageCreate(100, 100);
$colorallocate = ImageColorAllocate($im, $bincolor[0], $bincolor[1], $bincolor[2]);
ImageFilledRectangle($im, 0, 0, 100, 100, $colorallocate);
header('Content-Type: image/png');
ImagePNG($im);
?>
bob at hotmail dot com
10-Mar-2005 05:24
The first color allocated will not fill the background if you use a true color image from imagecreatetruecolor().
18-Jan-2005 04:23
When you are using truecolor images, you can also use bitwise operations to generate the color:
<?php
$color = ($r << 16) | ($g << 8) | $b; ?>
This is identical to the imagecolorallocate() function, in truecolor images!
aaron at parecki dot com
22-Dec-2004 04:10
This will let you tint an image to any specific color. The blacks of the source image become your specified color, and the whites remain white. Works best for colorizing greyscale images.
<?php
$r = 224;
$g = 192;
$b = 0;
$source_file = "picture.jpg";
$im_src = ImageCreateFromJpeg($source_file);
$im_tint = ImageCreate(imagesx($im_src),imagesy($im_src));
for ($c = 0; $c < 255; $c++) {
ImageColorAllocate($im_tint, max($r,$c), max($g,$c), max($b,$c));
}
ImageCopyMerge($im_tint,$im_src,0,0,0,0, imagesx($im_src), imagesy($im_src), 100);
ImageDestroy($im_src);
header("Content-type: image/jpeg");
imagejpeg($im_tint);
?>
carlosreche at yahoo dot com
13-Dec-2004 10:27
Here is an easier way to set the color without use imagecolorallocate();
<?php
$image = imagecreatetruecolor();
imagefill($image, 0, 0, 0xFFFFFF); ?>
Use hexadecimal values just like you do in HTML codes:
0xFF0000 = red
0x00FF00 = green
0x0000FF = blue
0xFFFF00 = yellow
... and so on
Hope this will help many people...
Carlos Reche
tyberis
28-Nov-2004 06:58
2 functions to convert from HSV colorspace (hue/saturation/brightness) to RGB (red/green/blue) colorspace and back.
<?php
function rgb2hsv($c) {
list($r,$g,$b)=$c;
$v=max($r,$g,$b);
$t=min($r,$g,$b);
$s=($v==0)?0:($v-$t)/$v;
if ($s==0)
$h=-1;
else {
$a=$v-$t;
$cr=($v-$r)/$a;
$cg=($v-$g)/$a;
$cb=($v-$b)/$a;
$h=($r==$v)?$cb-$cg:(($g==$v)?2+$cr-$cb:(($b==$v)?$h=4+$cg-$cr:0));
$h=60*$h;
$h=($h<0)?$h+360:$h;
}
return array($h,$s,$v);
}
function hsv2rgb($c) {
list($h,$s,$v)=$c;
if ($s==0)
return array($v,$v,$v);
else {
$h=($h%=360)/60;
$i=floor($h);
$f=$h-$i;
$q[0]=$q[1]=$v*(1-$s);
$q[2]=$v*(1-$s*(1-$f));
$q[3]=$q[4]=$v;
$q[5]=$v*(1-$s*$f);
return(array($q[($i+4)%5],$q[($i+2)%5],$q[$i%5]));
}
}
?>
avi at amarcus dot com
20-Aug-2004 01:21
quote from "tim at phantomrpg dot com"
Here is a simpler way of using a hex color with ImageColorAllocate().
sscanf("FFFF00", "%2x%2x%2x", $red, $green, $blue);
$stringColor=ImageColorAllocate($image,$red,$green,$blue);
I made it into a simple function:
<?php
function fromhex($string){
GLOBAL $image;
sscanf($string, "%2x%2x%2x", $red, $green, $blue);
return ImageColorAllocate($image,$red,$green,$blue);
}
ImageFill($image, 0, 0, fromhex('FFFFFF'));
?>
smoli at paranoya dot ch
19-Jul-2004 01:10
Some of you maybe want to use HSV color model for drawing color selectors and circles:
<?
function &colormap_hsv_to_rgb($h, $s, $v)
{
$ret = new stdClass();
if($s == 0)
{
$ret->r = $v;
$ret->g = $v;
$ret->b = $v;
return $ret;
}
else
{
$h = floatval($h) / 255.0;
$s = floatval($s) / 255.0;
$v = floatval($v) / 255.0;
$hue = $h;
if($hue == 1.0)
$hue = 0.0;
$hue *= 6.0;
$i = intval($hue);
$f = $hue - floatval($i);
$w = $v * (1.0 - $s);
$q = $v * (1.0 - ($s * $f));
$t = $v * (1.0 - ($s * (1.0 - $f)));
switch($i)
{
case 0: $ret->r = $v; $ret->g = $t; $ret->b = $w; break;
case 1: $ret->r = $q; $ret->g = $v; $ret->b = $w; break;
case 2: $ret->r = $w; $ret->g = $v; $ret->b = $t; break;
case 3: $ret->r = $w; $ret->g = $q; $ret->b = $v; break;
case 4: $ret->r = $t; $ret->g = $w; $ret->b = $v; break;
case 5: $ret->r = $v; $ret->g = $w; $ret->b = $q; break;
}
}
$ret->r = intval($ret->r * 255.0);
$ret->g = intval($ret->g * 255.0);
$ret->b = intval($ret->b * 255.0);
return $ret;
}
?>
tim at phantomrpg dot com
04-Apr-2004 05:38
Here is a simpler way of using a hex color with ImageColorAllocate().
sscanf("FFFF00", "%2x%2x%2x", $red, $green, $blue);
$stringColor=ImageColorAllocate($image,$red,$green,$blue);
Hope it helps someone.
jernberg at fairytale dot se
05-Mar-2004 12:43
this might help someone, how to allocate an color from an html color-definition:
$fg = "#ff0080";
$red = 100;
$green = 100;
$blue = 100;
if( eregi( "[#]?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $fg, $ret ) )
{
$red = hexdec( $ret[1] );
$green = hexdec( $ret[2] );
$blue = hexdec( $ret[3] );
}
$text_color = ImageColorAllocate( $img1, $red, $green, $blue );
chris at drunkenpirates dot co dot uk
13-Sep-2003 07:44
<?php
Header("Content-type: image/png");
$height = 128;
$width = 128;
$imA = ImageCreate($width, $height);
$imB = ImageCreate($width*4, $height*4);
$bckA = ImageColorAllocate($imA, 0,0,0);
$bckB = ImageColorAllocate($imB, 0,0,0);
for($c=0;$c<256;$c++){
ImageColorAllocate($imA, $c, $c, $c);
}
$m=rand(0,10);
for($c=0;$c<128;$c++){
$s= (sin( deg2rad($c*360*$m/128) )+1)*127;
$col_arr[$c]=$s;
}
for($y=0;$y<$height;$y++){
for($x=0;$x<$width;$x++){
$imgA[$x][$y]=$col_arr[$x];
}
}
for($y=0;$y<$height;$y++){
for($x=0;$x<$width;$x++){
$imgB[$x][$y]=$col_arr[$y];
}
}
for($y=0;$y<$height;$y++){
for($x=0;$x<$width;$x++){
$imgC[$x][$y]=$imgA[$x][$y]+$imgB[$x][$y];
$s=$imgC[$x][$y]/2;
Imagesetpixel($imA,$x,$y,$s);
}
}
Imagecopyresized ($imB, $imA, 0, 0, 0, 0, $width*4, $height*4, $width, $width);
ImagePNG($imB);
?>
BruceB
19-May-2003 11:39
Doiveo's suggestion for getting the font colors to behave with JPEG's does (at least for PHP 4.1.2 - GD version 1) stop the problem (that I had) with fonts becoming outlined instead of solid, however the font colors were still incorrect.
There is however a much simpler (and faster) solution, just use imageColorClosest instead of imageColorAllocate, e.g:
$near_white = imageColorClosest($img, 255,255,255);
imageTTFtext($img, 10, 0, 20, 20, $near_white, "font file path", "text to add to JPEG");
For most JPEG's there should be a fairly close matching color to the one you want to use.
leif at harmsen dot net
15-Apr-2003 09:31
I was unable to get any of the posted methods for converting colour to grayscale to work. The problem appears to be the way gd creates images from jpeg inconsistently over various versions. Eventually I wrote my own that works for me - this approach allocates the 256 color pallete first. You can also play with separate $r, $g, $b variables before using imagecolorallocate in order to tone or tint the image.
<?php
$resource = 'whatever.jpg';
$im_size = GetImageSize($resource);
$imageWidth = $im_size[0];
$imageHeight = $im_size[1];
$im = imageCreate($imageWidth,$imageHeight);
for ($c = 0; $c < 256; $c++) {
ImageColorAllocate($im, $c,$c,$c);
}
$im2 = ImageCreateFromJpeg($resource);
ImageCopyMerge($im,$im2,0,0,0,0, $imageWidth, $imageHeight, 100);
ImageDestroy($im2);
?>
go on using $im as your image, it is now grayscale ....
jasper at pointless dot net
20-Jan-2003 07:20
bisqwit at iki dot fi
16-Feb-2002 10:30
Actually, you can't allocate more than 256 colours for an paletted image (ImageCreate).
Use ImageCreateTrueColor instead. For it to work, you need libgd version 2 support in php though.
bisqwit at iki dot fi
16-Feb-2002 03:20
Looks like this function only allows allocating of 256 distinct colours.
So this code:
<?php
$im = ImageCreate(500,200);
for($a=0; $a<500; $a++)
{
$c = 128 + sin($a) * 127;
ImageLine($im, $a,0, $a,199, ImageColorAllocate($im, $c,$c,$c));
}
ob_start();
ImageJpeg($im, '', 75);
$s = ob_get_contents();
ob_end_clean();
header('Content-type: image/jpeg');
header('Content-length: '.strlen($s));
print $s;
will make an image that has changing colours at
the first 256 pixel columns only. The rest are gray.
doiveo at hotmail dot nospam
12-Jan-2002 12:17
This will help anyone trying to control font colors in a JPEG. You have to create a temporary image first, allocate the colors then merge the images before it works. Go figure.
$im_size = GetImageSize ( "MySource.jpg" );
$imageWidth = $im_size[0];
$imageHeight = $im_size[1];
$im = imageCreate( $imageWidth, $imageHeight );
// - or with GD 2+ -
// $im = imageCreateTrueColor( $imageWidth, $imageHeight );
// do all your color allocations here
$font_color_black = ImageColorAllocate( $im, 0,0,0);
$im2 = ImageCreateFromJPEG("MySource.jpg");
ImageCopy ($im,$im2,0,0,0,0, $imageWidth, $imageHeight);
ImageDestroy ($im2);
...finish as you please using the $im var.
| |