Little correction to marc-gutt's regural expression to validate hex color values. You can use this little function:
<?php
// Validate an hex color string. Return true if valid, false otherwise
function validateHexColor($color) {
$color = substr($color, 0, 7);
return preg_match('/#[0-9a-fA-F]{6}/', $color);
}
?>
imagecolorallocate
(PHP 4, PHP 5)
imagecolorallocate — 画像で使用する色を作成する
説明
int imagecolorallocate
( resource $image
, int $red
, int $green
, int $blue
)
指定した RGB を配色とする色の ID を返します。
imagecolorallocate() は image で表される画像上で使用する各々の色を作成する際にコールする必要があります。
注意: imagecolorallocate() の最初のコールで パレットをもとにした画像 (imagecreate() を使用して作成した画像) で背景色がセットされます。
パラメータ
- image
-
imagecreatetruecolor() のような画像作成関数が返す画像リソース。
- red
-
赤コンポーネントの値。
- green
-
緑コンポーネントの値。
- blue
-
青コンポーネントの値。
返り値
色の ID、あるいは作成に失敗したい場合に FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.1.3 より前 | 作成に失敗した場合には -1 を返します。 |
例
例1 imagecolorallocate() の例
<?php
$im = imagecreate(100, 100);
// 背景色を赤にします
$background = imagecolorallocate($im, 255, 0, 0);
// その他の色を設定します
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
// 十六進で指定します
$white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
$black = imagecolorallocate($im, 0x00, 0x00, 0x00);
?>
imagecolorallocate
federicot at gmail dot com
21-May-2008 11:08
21-May-2008 11:08
djcassis at gmail dot com
22-Nov-2007 05:06
22-Nov-2007 05:06
I also find the following useful when dealing with HTML hexadecimal colors :
<?php
$color = '#FFffFF';
print_r(sscanf($color, '#%2x%2x%2x'));
?>
outputs :
Array
(
[0] => 255
[1] => 255
[2] => 255
)
Vassilis Dourdounis
05-Nov-2007 03:17
05-Nov-2007 03:17
Regarding the problem people mention during the creation of gradients. Color allocation can only be done 255 times if your image is being created with imagecreate().
Use imagecreatetruecolor() if you want more colors.
Ludo
25-Oct-2007 06:35
25-Oct-2007 06:35
When working on an existant GIF images, if the number of different colours has reached the limits of the GIF format, imagecolorallocate will not use to the colour you ask her within the parameters, she will apply black !
That's a problem when generating images "on-the-fly" with many manipulations, from a GIF image.
To go round the problem, you have to convert the GIF image into a PNG one, and then you can work on the PNG and everything will be ok.
For example :
<?php
// first, convert into a PNG image
$handle = imagecreatefromgif('my_image.gif');
imagepng($handle, 'my_image.png');
imagedestroy($handle);
// then, you can work on it
$handle = imagecreatefrompng('my_image.png');
/*
* work on the image
*/
imagegif($handle);
?>
William Barath
29-Sep-2007 08:15
29-Sep-2007 08:15
Lots of hsv2rgb commentary but no working example, so here's mine:
<?php // hsv2rgb example translated from ImageMagick C code
function hsv2rgb($h, $s, $v)
{
$s /= 256.0;
if ($s == 0.0) return array($v,$v,$v);
$h /= (256.0 / 6.0);
$i = floor($h);
$f = $h - $i;
$p = (integer)($v * (1.0 - $s));
$q = (integer)($v * (1.0 - $s * $f));
$t = (integer)($v * (1.0 - $s * (1.0 - $f)));
switch($i) {
case 0: return array($v,$t,$p);
case 1: return array($q,$v,$p);
case 2: return array($p,$v,$t);
case 3: return array($p,$q,$v);
case 4: return array($t,$p,$v);
default: return array($v,$p,$q);
}
}
$image = ImageCreateTrueColor(256,128);
for ($y=0; $y<64; $y++) for($x=0; $x<256; $x++){
list($r,$g,$b) = hsv2rgb($x | 7,255,($y*4) |7);
$color = ($r << 16 ) | ($g << 8) | $b;
imagesetpixel($image, $x, $y-4, $color);
}
for ($y=64; $y<128; $y++) for($x=0; $x<256; $x++){
list($r,$g,$b) = hsv2rgb($x|7,((127-$y)*4)|7,255);
$color = ($r << 16) | ($g << 8) | $b;
imagesetpixel($image, $x, $y-4, $color);
}
for ($y=120; $y<128; $y++) for($x=0; $x<256; $x++){
$color = (($x |7) << 16) | (($x |7) << 8) | ($x |7);
imagesetpixel($image, $x, $y, $color);
}
header("Content-Type: image/png");
imagepng($image);
?>
timo dot witte at kwick dot de
23-Aug-2007 01:44
23-Aug-2007 01:44
Do not use the hsv2rgb function from tyberis it calculates invalid output! There are 3 jumps in the resulting colorbar if you loop do a 0-360 for hue, saturation and value is calculated correctly.
info at marc-gutt dot de Marc Gutt
04-Aug-2007 09:50
04-Aug-2007 09:50
rgb from hex without creating a new function or using split features:
<?php
$color = 'FF9900';
$color = imagecolorallocate($im, hexdec('0x' . $color{0} . $color{1}), hexdec('0x' . $color{2} . $color{3}), hexdec('0x' . $color{4} . $color{5}));
?>
You can validate $color with preg_match('#[a-z0-9]{6}#i', $color) if necessary.
picklecj at rose-hulman dot edu
14-Jun-2007 12:15
14-Jun-2007 12:15
Another solution to color limitation issues when creating gradients. This file takes width (px) and left and right colors (hex) and makes a gradient while only allocating 250 colors.
<?php
$leftR = hexdec(substr($_GET["left"],0,2));
$leftG = hexdec(substr($_GET["left"],2,2));
$leftB = hexdec(substr($_GET["left"],4,2));
$rightR = hexdec(substr($_GET["right"],0,2));
$rightG = hexdec(substr($_GET["right"],2,2));
$rightB = hexdec(substr($_GET["right"],4,2));
$image=imagecreate($_GET["width"],1);
for($i=0;$i<250;$i++) {
$colorset[$i] = imagecolorallocate($image, $leftR + ($i*(($rightR-$leftR)/250)), $leftG + ($i*(($rightG-$leftG)/250)), $leftB + ($i*(($rightB-$leftB)/250)));
}
for($i=0;$i<($_GET["width"]);$i++) {
imagesetpixel ($image, $i, 0, $colorset[(int)($i/($_GET["width"]/250))] );
}
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
example: gradient.php?width=640&left=000000&right=FF0000
Makes a 640px-wide image that fades from black to red.
LPEspecial at home dot nl
20-Sep-2006 12:04
20-Sep-2006 12:04
This might help someone who want to use 'ImageColorAllocate ' with a hex value
function ImageColorAllocateFromHex ($img, $hexstr)
{
$int = hexdec($hexstr);
return ImageColorAllocate ($img,
0xFF & ($int >> 0x10),
0xFF & ($int >> 0x8),
0xFF & $int);
}
behun at webconsult dot sk
27-Aug-2006 05:56
27-Aug-2006 05:56
Also, when you need more then 256 colors, use imagecreatetruecolor function. With this function you can use unlimited number of colors.
erik at phpcastle dot com
03-Apr-2006 04:45
03-Apr-2006 04:45
Hi,
During developing a gradient method I discovered that there can only be 255 colors allocated somehow...
So the first 255 are going ok and after these the colorallocate function returns -1. My work around for this problem was to put the allocated color in an array and check if this variable exists before calling imagecolorallocate again.
Did not had this problem again and my gradient scaled well after discovering this :)
This is the piece of code that created a gradient PNG. The code is top down programmed and not put in a function or class yet.
Because the code is 124 lines of code I've put the script at my website: http://beta.phpcastle.com/snippets/PHP/Image+Gradient (website still beta :P)
I hope someone can use this addition
SW
14-Jan-2006 07:00
14-Jan-2006 07:00
This works! A Black-Image with vertical centered white Aliased Arial-Text and same left and right margin - used for Menu-Buttons.
<?php
function createImgText ($string="", $fontsize=0, $marginX=0, $imgH=0 , $fontfile="", $imgColorHex="", $txtColorHex=""){
if($string!=""){
Header("Content-type: image/png");
//
$spacing = 0;
$line = array("linespacing" => $spacing);
$box = @imageftbbox($fontsize,0,$fontfile,$string,$line)
or die("ERROR");
$tw=$box[4]-$box[0]; //image width
$marginY = $imgH - (($imgH - $fontsize) / 2);
$imgWidth = $tw + (2*$marginX);
$im = ImageCreate($imgWidth, $imgH);
$int = hexdec($imgColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$black = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
$int = hexdec($txtColorHex);
$arr = array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
$white = ImageColorAllocate($im, $arr["red"], $arr["green"], $arr["blue"]);
ImageFtText($im, $fontsize, 0, $marginX, $marginY, $white, $fontfile, $string, array());
ImagePng($im);
ImageDestroy($im);
}else{
echo "ERROR!";
}
}
createImgText ("Hello World", 9, 10, 18, "arial.ttf", "000000", "FFFFFF");
?>
mlse
15-Nov-2005 06:49
15-Nov-2005 06:49
Another more general variation on the theme using the same naming conventions as the hexdec and dechex built-in functions ...
Prototype:
array hexrgb ( string hex_string )
Returns:
An associative array of the RGB components specified in hex_string.
hexrgb() example:
<?php
$rgb = hexrgb("0xAABBCC");
print_r($rgb);
?>
Output is:
Array
(
[red] => 170
[green] => 187
[blue] => 204
)
Implementation:
<?php
function hexrgb ($hexstr)
{
$int = hexdec($hexstr);
return array("red" => 0xFF & ($int >> 0x10),
"green" => 0xFF & ($int >> 0x8),
"blue" => 0xFF & $int);
}
?>
The output of hexdec can then be passed to imagecolorallocate and manipulated as required.
miked at m80im dot com
08-Nov-2005 10:40
08-Nov-2005 10:40
improvement upon fromhex function by avi at amarcus dot com
<?php
function fromhex($string,&$image) {
sscanf($string, "%2x%2x%2x", $red, $green, $blue);
return ImageColorAllocate($image,$red,$green,$blue);
}
//Example for calling a hex color:
ImageFill($image, 0, 0, fromhex('FFFFFF'));
?>
Zigbigidorlu at hotmail dot com
07-Oct-2005 04:05
07-Oct-2005 04:05
Here's a very simple and very effective code to change a HEX color to RGB.
-------------------------
function HEX2RGB($color){
$color_array = array();
$hex_color = strtoupper($color);
for($i = 0; $i < 6; $i++){
$hex = substr($hex_color,$i,1);
switch($hex){
case "A": $num = 10; break;
case "B": $num = 11; break;
case "C": $num = 12; break;
case "D": $num = 13; break;
case "E": $num = 14; break;
case "F": $num = 15; break;
default: $num = $hex; break;
}
array_push($color_array,$num);
}
$R = (($color_array[0] * 16) + $color_array[1]);
$G = (($color_array[2] * 16) + $color_array[3]);
$B = (($color_array[4] * 16) + $color_array[5]);
return array($R,$G,$B);
unset($color_array,$hex,$R,$G,$B);
}
-------------------------
mail at kailashnadh dot name
28-Jun-2005 11:47
28-Jun-2005 11:47
This nifty function will produce the negative of a given image!
<?php
/********************************
Code by Kailash Nadh
http://kailashnadh.name
usage:
img2neg("my_pic.jpg");
*********************************/
function img2neg($pic) {
header("Content-type: image/jpeg");
$source=imagecreatefromjpeg($pic); // Source
$width=imagesx($source); $height=imagesy($source);
$im = imagecreatetruecolor($width, $height); // Our negative img in the making
for($y=0; $y < $height; $y++) {
for($x=0; $x < $width; $x++) {
$colors=imagecolorsforindex($source, imagecolorat($source, $x,$y));
// this is what makes the colors negative
$r=255-$colors['red'];
$g=255-$colors['green'];
$b=255-$colors['blue'];
$test=imagecolorallocate($im, $r,$g,$b);
imagesetpixel($im,$x, $y, $test);
}
}
imagejpeg($im);
imagedestroy($im);
}
?>
andreoli dot carlo at libero dot it
08-Jun-2005 10:38
08-Jun-2005 10:38
hsl to RGB
(not yet optimized but it function)
function hslToRgb ($h, $s, $l) {
if ($h>240 || $h<0) return array(0,0,0);
if ($s>240 || $s<0) return array(0,0,0);
if ($l>240 || $l<0) return array(0,0,0);
if ($h<=40) {
$R=255;
$G=(int)($h/40*256);
$B=0;
}
elseif ($h>40 && $h<=80) {
$R=(1-($h-40)/40)*256;
$G=255;
$B=0;
}
elseif ($h>80 && $h<=120) {
$R=0;
$G=255;
$B=($h-80)/40*256;
}
elseif ($h>120 && $h<=160) {
$R=0;
$G=(1-($h-120)/40)*256;
$B=255;
}
elseif ($h>160 && $h<=200) {
$R=($h-160)/40*256;
$G=0;
$B=255;
}
elseif ($h>200) {
$R=255;
$G=0;
$B=(1-($h-200)/40)*256;
}
$R=$R+(240-$s)/240*(128-$R);
$G=$G+(240-$s)/240*(128-$G);
$B=$B+(240-$s)/240*(128-$B);
if ($l<120) {
$R=($R/120)*$l;
$G=($G/120)*$l;
$B=($B/120)*$l;
}
else {
$R=$l*((256-$R)/120)+2*$R-256;
$G=$l*((256-$G)/120)+2*$G-256;
$B=$l*((256-$B)/120)+2*$B-256;
}
if ($R<0) $R=0;
if ($R>255) $R=255;
if ($G<0) $G=0;
if ($G>255) $G=255;
if ($B<0) $B=0;
if ($B>255) $B=255;
return array((int)$R,(int)$G,(int)$B);
}
mv at brazil dot com
16-Mar-2005 04:34
16-Mar-2005 04:34
<?
/**
* Create a image bar using lib GD
* Ege. <img src="color_sample.php?color=FF0000" width="10 height="30">
*/
// Split the HTML color representation
$hexcolor = str_split($_GET["color"], 2);
// Convert HEX values to DECIMAL
$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
11-Mar-2005 08:24
11-Mar-2005 08:24
The first color allocated will not fill the background if you use a true color image from imagecreatetruecolor().
19-Jan-2005 07:23
When you are using truecolor images, you can also use bitwise operations to generate the color:
<?php
$color = ($r << 16) | ($g << 8) | $b; // 2261213
?>
This is identical to the imagecolorallocate() function, in truecolor images!
aaron at parecki dot com
22-Dec-2004 07:10
22-Dec-2004 07: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
14-Dec-2004 01:27
14-Dec-2004 01:27
Here is an easier way to set the color without use imagecolorallocate();
<?php
$image = imagecreatetruecolor();
imagefill($image, 0, 0, 0xFFFFFF); // white color (#FFFFFF)
?>
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 09:58
28-Nov-2004 09:58
2 functions to convert from HSV colorspace (hue/saturation/brightness) to RGB (red/green/blue) colorspace and back.
<?php
// $c = array($red, $green, $blue)
// $red=[0..1], $green=[0..1], $blue=[0..1];
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);
}
// $c = array($hue, $saturation, $brightness)
// $hue=[0..360], $saturation=[0..1], $brightness=[0..1]
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 03:21
20-Aug-2004 03: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);
}
//Example for calling a hex color:
ImageFill($image, 0, 0, fromhex('FFFFFF'));
?>
smoli at paranoya dot ch
20-Jul-2004 03:10
20-Jul-2004 03: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 07:38
04-Apr-2004 07: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
06-Mar-2004 03:43
06-Mar-2004 03: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
14-Sep-2003 09:44
14-Sep-2003 09:44
<?php
/*
An example combining the use of ImageColorAllocate, Imagesetpixel, Imagecopyresized and some basic Trig
*/
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);
//GENERATE GRAY SCALE PALLETE
for($c=0;$c<256;$c++){
ImageColorAllocate($imA, $c, $c, $c);
}
//PRODUCE DATA
$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];
}
}
//SET PIXELS
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);
}
}
//RESIZE IMAGE FOR DISPLAY
Imagecopyresized ($imB, $imA, 0, 0, 0, 0, $width*4, $height*4, $width, $width);
ImagePNG($imB);
?>
BruceB
20-May-2003 01:39
20-May-2003 01: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
16-Apr-2003 11:31
16-Apr-2003 11: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
21-Jan-2003 10:20
21-Jan-2003 10:20
a patch to php 4.3.0 to add imagecolorallocatealpha is here:
http://pointless.net/~jasper/php-4.3.0-imagecolorallocatealpha.patch
cd php-4.3.0/ext/gd
patch < path/to/patch
and recompile and reinstall.
bisqwit at iki dot fi
17-Feb-2002 01:30
17-Feb-2002 01: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
17-Feb-2002 06:20
17-Feb-2002 06: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 03:17
12-Jan-2002 03: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.
