Ty *_*y W 5 php png transparency gamma gd2
我对图像缩放的讨论很感兴趣,后来发现我用来从上传的图像创建缩略图的PHP代码遇到了同样的问题.我决定尝试在底部附近发布的PHP修补程序(将伽玛值从2.2转换为1.0,调整图像大小,将伽玛值从1.0转换回2.2).这有助于解决文章中提到的问题,但是对代码的这种修改具有淘汰PNG alpha通道透明度的不幸副作用.
这是我对伽马校正的代码.
<?php
$image = imagecreatefrompng($source_file);
$resized_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($resized_image, false);
imagesavealpha($resized_image, true);
imagegammacorrect($image, 2.2, 1.0);
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagegammacorrect($resized_image, 1.0, 2.2);
imagepng($resized_image, $dest_file);
?>
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何调整图像大小,采用伽马校正技巧,同时保持原始图像的alpha通道透明度?
样本图片:
在尝试更正伽玛之前,您可以看到透明度很好.(查看透明度的最简单方法是检查包裹在图像周围的段落标记,并通过FireBug或类似方法添加背景:black;到其样式属性.)
原始图像http://ender.hosting.emarketsouth.com/images/test-image.png 没有伽马校正http://ender.hosting.emarketsouth.com/images/test-image-resized-no-gamma.png 伽玛纠正 - 没有透明度http://ender.hosting.emarketsouth.com/images/test-image-resized.png
这是一些有效的代码。基本上,它分离出 alpha 通道,使用 gamma 校正调整图像大小,在没有gamma 校正的情况下调整 alpha 通道的大小,然后将 alpha 通道复制到使用 gamma 校正完成的调整大小的图像。
我的猜测是 imagegamma Correct() 函数有一个错误。也许 gamma 仅适用于 RGB,而 GD 也尝试对 alpha 通道进行相同的计算?色彩理论不是我的强项。
无论如何,这是代码。不幸的是,我找不到比逐一循环像素更好的方法来分离通道。那好吧...
<?php
// Load image
$image = imagecreatefrompng('test-image.png');
// Create destination
$resized_image = imagecreatetruecolor(100, 100);
imagealphablending($resized_image, false); // Overwrite alpha
imagesavealpha($resized_image, true);
// Create a separate alpha channel
$alpha_image = imagecreatetruecolor(200, 200);
imagealphablending($alpha_image, false); // Overwrite alpha
imagesavealpha($alpha_image, true);
for ($x = 0; $x < 200; $x++) {
for ($y = 0; $y < 200; $y++) {
$alpha = (imagecolorat($image, $x, $y) >> 24) & 0xFF;
$color = imagecolorallocatealpha($alpha_image, 0, 0, 0, $alpha);
imagesetpixel($alpha_image, $x, $y, $color);
}
}
// Resize image to destination, using gamma correction
imagegammacorrect($image, 2.2, 1.0);
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, 100, 100, 200, 200);
imagegammacorrect($resized_image, 1.0, 2.2);
// Resize alpha channel
$alpha_resized_image = imagecreatetruecolor(200, 200);
imagealphablending($alpha_resized_image, false);
imagesavealpha($alpha_resized_image, true);
imagecopyresampled($alpha_resized_image, $alpha_image, 0, 0, 0, 0, 100, 100, 200, 200);
// Copy alpha channel back to resized image
for ($x = 0; $x < 100; $x++) {
for ($y = 0; $y < 100; $y++) {
$alpha = (imagecolorat($alpha_resized_image, $x, $y) >> 24) & 0xFF;
$rgb = imagecolorat($resized_image, $x, $y);
$r = ($rgb >> 16 ) & 0xFF;
$g = ($rgb >> 8 ) & 0xFF;
$b = $rgb & 0xFF;
$color = imagecolorallocatealpha($resized_image, $r, $g, $b, $alpha);
imagesetpixel($resized_image, $x, $y, $color);
}
}
imagepng($resized_image, 'test-image-scaled.png');
?>
Run Code Online (Sandbox Code Playgroud)
当然,用变量替换硬编码值...这是我使用您的图像和我的代码得到的结果:

(来源:jejik.com)