PHP Allocate Color Without Image Resource

PHP allocate color without image resource

16711680 (decimal) is 0x00FF0000 (hexadecimal)

00 - Alpha value (0 dec)

FF - Red (255 dec)

00 - Green (0 dec)

00 - Blue (0 dec)

See http://www.php.net/manual/en/function.imagecolorallocatealpha.php to set the alpha byte

Edit:

Also, to answer your first question -- yes, you can create a color without an image resource (and, consequently without a call to imagecolorallocate):

$col1 = 0x00FF0000; // Red

$col2 = 0x0000FF00; // Green

// etc...

PHP: work with colors directly without imagecolorallocate?

If I am aware imagecolorallocate() only returns a hex representation to be used by the other GD functions, as the library is a bit lower level than PHP of course it works with hex values directly, so there raised a need for an intermediate function to assign a colour.

The following should be equivalent:

$im = imagecreatetruecolor("200", "100");
$white = imagecolorallocate($im, 255, 255, 255);
$white = 0x00FFFFFF;
$alphagreen = imagecolorallocatealpha($im, 0, 255, 0, 64);
$alphagreen = 0x4000FF00;

How can I give a color to imagecolorallocate?

Use hexdec() (exemple : hexdec("a0"))

http://fr2.php.net/manual/en/function.hexdec.php

PHP: How to set part of png file as transparent?

First, you need to set alpha channels to your image:
http://www.php.net/manual/en/function.imagealphablending.php
http://www.php.net/manual/en/function.imagesavealpha.php

Second, you need set transparent color to all pixels in your transparent area:
http://www.php.net/manual/en/function.imagecolorset.php

PHP GD multiple images with white background - warnings?

I have finally found the answer to my problem and thought I should post it here step by step so if someone should have the same problem one day, here is what I figured out:

Basically, I had $nimga_0=imagefill($nimgac_0,0,0,$nimgaa_0); in my code, whereas imagefill is a bool. So, imagefill was successful and I assigned its value 1 to $nimga_0, and then wanted to use 1 as a resource in imagecopyresized($nimga_0,$nimgb_0,0,0,0,0,$nwidth_0,$nheight_0,$width,$height);.

Of course, this cannot be a valid source. All I had to change was leaving out the vars and just do:

imagefill($nimgac_0,0,0,$nimgaa_0);

STEP BY STEP

I created a canvas with

$nimgac_0=imagecreatetruecolor($maxw_img0,$maxh_img0);

then the white color with

$nimgaa_0=imagecolorallocate($nimgac_0,255,255,255);

and then just had to fill the canvas with the color using

imagefill($nimgac_0,0,0,$nimgaa_0);

Then I copy the uploaded image with

$nimgb_0=imagecreatefromjpeg("../imga/".$_FILES['file']['name']);

then copy the uploaded image onto the (now white colored) canvas

imagecopyresized($nimgac_0,$nimgb_0,0,0,0,0,$nwidth_0,$nheight_0,$width,$height);

and save as jpg

imagejpeg($nimgac_0,"../imga/".$_FILES['file']['name'],80); 

and finally clear the cache

imagedestroy($nimgac_0);

Hope it helps someone else too :)

PHP GD palette colors

Check the return on imagecolorstotal, you are always getting 256 colors as the return no matter how high you set the number of colors to be dithered to. PNG-8 & GIF formats only support palettes of up to 256 colors. So even if you can use more than 256 in a palette, you'd have to save it back as true color for anyone to be able to use it, thus making the whole conversion process a waste of time. In other words imagetruecolortopallete has an upper limit of 256 colors, you cannot go higher.

Here's how you can do it in truecolor, though it's resource intensive. Maybe look into imagemagick if you want to do this more efficiently.

$im = imagecreatefrompng("lamb.png");
$img = imagecreatetruecolor(imagesx($im), imagesy($im));
$degrees = 0;
if ($degrees > 360) {$degrees = $degrees % 360 ;}

foreach (range(0, imagesx($im) - 1) as $x ) {
foreach (range(0, imagesy($im) - 1) as $y) {
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$hsv = rgbtohsv($r,$g,$b);
$rgb = hsvtorgb($degrees, $hsv['s'], $hsv['v']);
imagesetpixel($img, $x,$y,imagecolorallocate($img, $rgb['r'], $rgb['g'], $rgb['b']));
}
}

imagepng($img, 'lamb2.png');

Edit: Adding rgbtohsv & hsvtorgb functions. I did not write these functions.

function rgbtohsv ($R, $G, $B) {
// HSV Results:Number 0-1
$HSL = array();

$var_R = ($R / 255);
$var_G = ($G / 255);
$var_B = ($B / 255);

$var_Min = min($var_R, $var_G, $var_B);
$var_Max = max($var_R, $var_G, $var_B);
$del_Max = $var_Max - $var_Min;

$V = $var_Max;

if ($del_Max == 0)
{
$H = 0;
$S = 0;
}
else
{
$S = $del_Max / $var_Max;

$del_R = ( ( ( $var_Max - $var_R ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_G = ( ( ( $var_Max - $var_G ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;
$del_B = ( ( ( $var_Max - $var_B ) / 6 ) + ( $del_Max / 2 ) ) / $del_Max;

if ($var_R == $var_Max) $H = $del_B - $del_G;
else if ($var_G == $var_Max) $H = ( 1 / 3 ) + $del_R - $del_B;
else if ($var_B == $var_Max) $H = ( 2 / 3 ) + $del_G - $del_R;

if ($H<0) $H++;
if ($H>1) $H--;
}

$HSL['h'] = $H;
$HSL['s'] = $S;
$HSL['v'] = $V;

return $HSL;
}

function hsvtorgb ($H, $S, $V)
{
$RGB = array();

if($S == 0)
{
$R = $G = $B = $V * 255;
}
else
{
$var_H = $H * 6;
$var_i = floor( $var_H );
$var_1 = $V * ( 1 - $S );
$var_2 = $V * ( 1 - $S * ( $var_H - $var_i ) );
$var_3 = $V * ( 1 - $S * (1 - ( $var_H - $var_i ) ) );

if ($var_i == 0) { $var_R = $V ; $var_G = $var_3 ; $var_B = $var_1 ; }
else if ($var_i == 1) { $var_R = $var_2 ; $var_G = $V ; $var_B = $var_1 ; }
else if ($var_i == 2) { $var_R = $var_1 ; $var_G = $V ; $var_B = $var_3 ; }
else if ($var_i == 3) { $var_R = $var_1 ; $var_G = $var_2 ; $var_B = $V ; }
else if ($var_i == 4) { $var_R = $var_3 ; $var_G = $var_1 ; $var_B = $V ; }
else { $var_R = $V ; $var_G = $var_1 ; $var_B = $var_2 ; }

$R = $var_R * 255;
$G = $var_G * 255;
$B = $var_B * 255;
}

$RGB['r'] = $R;
$RGB['g'] = $G;
$RGB['b'] = $B;

return $RGB;
}


Related Topics



Leave a reply



Submit