php slaveok_imagecolorsforindex

这篇博客包含了一系列关于PHP图像处理的代码片段,涉及颜色转换、灰度处理、RGB到HSL的转换以及sepia色调的应用。作者们讨论了如何优化函数以提高效率,并警告了在处理不同图像类型时需要注意的陷阱。示例代码涵盖了从真彩色图像到调色板图像的转换,以及如何处理透明度和色彩强度。
摘要由CSDN通过智能技术生成

用户评论:

[#1]

hofstadler dot andi at gmx dot at [2008-10-07 12:36:36]

I have optimized the rgb2hsl function from slepichev a bit, so that it is a bit shorter and hopefully a bit faster:

if (0==$deltaMax){$H=0;$S=0;

}

else{

if (0.5>$L){$S=$deltaMax/ ($clrMax+$clrMin);

}

else{$S=$deltaMax/ (510-$clrMax-$clrMin);

}

if ($clrMax==$clrR) {$H= ($clrG-$clrB) / (6.0*$deltaMax);

}

else if ($clrMax==$clrG) {$H=1/3+ ($clrB-$clrR) / (6.0*$deltaMax);

}

else {$H=2/3+ ($clrR-$clrG) / (6.0*$deltaMax);

}

if (0>$H)$H+=1;

if (1

}

return array($H,$S,$L);

}?>

[#2]

matrebatre [2008-08-07 11:11:17]

Be aware that

$rgba=imagecolorat($image,$x,$y);$r= ($rgba>>16) &0xFF;$g= ($rgba>>8) &0xFF;$b=$rgba&0xFF;$a= ($rgba&0x7F000000) >>24;?>

will only work for truecolor images. With eg GIF images, this will have strange results. For GIF images, you should always use imagecolorsforindex().

[#3]

derek at idreams dot co dot uk [2007-11-30 12:45:12]

The earlier microsoft sepia example seemed to have a factor in which made it pinky... here is a modified example which uses just the Microsoft sepia (as per the wiki sepia entry)

if (!($t=imagecolorstotal($img))) {$t=256;imagetruecolortopalette($img,true,$t);

}$total=imagecolorstotal($img);

for ($i=0;$i

if ($red>255) {$red=255; }

if ($green>255) {$green=255; }

if ($blue>255) {$blue=255; }imagecolorset($img,$i,$red,$green,$blue);

}

}?>

[#4]

joe dot scylla at gmail dot com [2007-11-28 06:35:53]

While it's quite easy and intuitive to get the alpha transparency of a pixel with:

$rgba=imagecolorsforindex($image,imagecolorat($image,$x,$y));$alpha=$rgba["alpha"];?>

you should use the return value of the command imagecolorat to get the alpha transparency with the code below because it's much faster and will have a major impact if you process every pixel of an image:

$rgba=imagecolorat($image,$x,$y);$alpha= ($rgba&0x7F000000) >>24;?>

[#5]

tim at leethost dot com [2007-09-09 22:01:50]

Here's a better grayscale, sepia, and general tinting function.  This function is better because:

1) Works with true color images (the other sepia code didn't).

2) Provides a more gooder grayscale conversion (yes, I said "more gooder").  The other grayscale code used imagetruecolortopalette, which just doesn't work well for grayscale conversion.

3) The other sepia code was really colorful, a little too much for my taste.  This function allows you to optionally set the tinting of the grayscale to anything you wish.

4) Single function for grayscale, sepia, and any other tinting you can dream up.

Here's some examples:

imagegrayscaletint ($img);  // Grayscale, no tinting

imagegrayscaletint ($img,304,242,209);  // What I use for sepia

imagegrayscaletint ($img,0,0,255);  // A berry blue image

The RGB values for tinting are normally from 0 to 255.  But, you can use values larger than 255 to lighten and "burn" the image.  The sepia example above does this a little, the below example provides a better example of lightening the image and burning the light areas out a little:

imagegrayscaletint ($img,400,400,400);  // Lighten image

imagegrayscaletint ($img,127,127,127);  // Darken image

for ($i=0;$i<256;$i++)imagecolorallocate($dest,$i,$i,$i);imagecopyresized($dest,$img,0,0,0,0,$width,$height,$width,$height);

for ($i=0;$i<256;$i++)imagecolorset($dest,$i,min($i*abs($tint_r) /255,255),min($i*abs($tint_g) /255,255),min($i*abs($tint_b) /255,255));$img=imagecreate($width,$height);imagecopy($img,$dest,0,0,0,0,$width,$height);imagedestroy($dest);

}?>

[#6]

slepichev at yahoo dot com [2007-09-06 03:50:41]

If you would like to change the intensity or lightness level of a specific color, you will need to convert the color format from RGB to HSL.

following function convert RGB array(red,green,blue) to HSL array(hue, saturation, lightness)

if (0==$deltaMax){$H=0;$S=0;

}

else{

if (0.5>$L){$S=$deltaMax/ ($clrMax+$clrMin);

}

else{$S=$deltaMax/ (2-$clrMax-$clrMin);

}$deltaR= ((($clrMax-$clrR) /6) + ($deltaMax/2)) /$deltaMax;$deltaG= ((($clrMax-$clrG) /6) + ($deltaMax/2)) /$deltaMax;$deltaB= ((($clrMax-$clrB) /6) + ($deltaMax/2)) /$deltaMax;

if ($clrR==$clrMax){$H=$deltaB-$deltaG;

}

else if ($clrG==$clrMax){$H= (1/3) +$deltaR-$deltaB;

}

else if ($clrB==$clrMax){$H= (2/3) +$deltaG-$deltaR;

}

if (0>$H)$H+=1;

if (1

}

return array($H,$S,$L);

}?>

[#7]

admin at phpgfx dot com [2007-08-26 09:54:30]

this is a sepia filter using microsoft's definition

for ($i=0;$i

}

}?>

[#8]

adspeed.com [2005-08-23 16:05:02]

To correct m4551 at abasoft dot it example:

ImageTrueColorToPalette($im,1,$t);

might give less colors than $t, so the for loop should call "$i

[#9]

strozek(a)deas()harvard()edu [2004-06-25 13:32:28]

Regarding m4551's method of conversion -- the actual CCIR-approved RGB-to-grayscale conversion is as follows:

grayscale component = 0.2125*R + 0.7154*G + 0.0721*B

(cf. CCIR Recommendation 709 for modern monitors)

[#10]

m4551 at abasoft dot it [2004-04-23 09:23:40]

here's a function to greyscale an image even from a truecolor source (jpeg or png).

slightly poor quality, but very fast...

function imagegreyscale(&$img, $dither=1) {

if (!($t = imagecolorstotal($img))) {

$t = 256;

imagetruecolortopalette($img, $dither, $t);

}

for ($c = 0; $c 

$col = imagecolorsforindex($img, $c);

$min = min($col['red'],$col['green'],$col['blue']);

$max = max($col['red'],$col['green'],$col['blue']);

$i = ($max+$min)/2;

imagecolorset($img, $c, $i, $i, $i);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值