逐像素图像,透明变成黑色
我读了一些关于此的内容,但我不知道如何将其放入我的代码中。
这里是:
function go($image) {
// Open the image
$getimage=imagecreatefrompng($image);
//Get the width/height
$w = imagesx($getimage);
$h = imagesy($getimage);
//init Count variable, when it reach the width, skip a line
$count = 0;
// For each line
for($y=0;$y<$h;$y++) {
// For each column
for($x=0;$x<$w;$x++) {
$rgba = imagecolorat($getimage, $x, $y);
$r = ($rgba >> 16) & 0xFF;
$g = ($rgba >> 8) & 0xFF;
$b = $rgba & 0xFF;
$a = ($rgba & 0x7F000000) >> 24;
echo '<div class="pix" style="background-color: rgba('.$r.', '.$g.', '.$b.', '.$a.');"></div>';
$count++;
if($count==$w) {echo '<br>'; $count = 0; }
}
}
echo $pixel_gen;
}
如果 oyu 想看看它是什么样子,请点击这里: http://narks.xtreemhost.com/< /a>
双击任意图标,将出现弹出窗口。 (注意:在任何图标上双击都会显示相同的图像(我还没有修复这个问题)
知道如何使黑色像素看起来像带有 alpha 的真实像素吗?
感谢您的帮助!
已编辑 (新代码,我只放置了第一行,因为我想节省空间)
function go($image) {
// Open the image
$getimage=imagecreatefrompng($image);
imagealphablending($getimage, false);
imagesavealpha($getimage, true);
//Get the width/height
$w = imagesx($getimage);
$h = imagesy($getimage);
[...]
要查看它现在的样子,请访问上面的网站并双击图标。
编辑2 我刚刚尝试(进行测试):,
$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png');
imagealphablending($getimage, false);
imagesavealpha($getimage, true);
header("Content-type: image/png");
imagepng($getimage);
imagedestroy($getimage);
然后使用
$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png');
header("Content-type: image/png");
imagepng($getimage);
imagedestroy($getimage);
第一个没问题,第二个使像素变黑。所以当我获取每个像素的 RGB 颜色并显示它时。有人看到那里有错误吗?
I read a couple of thing about this, but I can't figure how to put it in my code.
Here it is :
function go($image) {
// Open the image
$getimage=imagecreatefrompng($image);
//Get the width/height
$w = imagesx($getimage);
$h = imagesy($getimage);
//init Count variable, when it reach the width, skip a line
$count = 0;
// For each line
for($y=0;$y<$h;$y++) {
// For each column
for($x=0;$x<$w;$x++) {
$rgba = imagecolorat($getimage, $x, $y);
$r = ($rgba >> 16) & 0xFF;
$g = ($rgba >> 8) & 0xFF;
$b = $rgba & 0xFF;
$a = ($rgba & 0x7F000000) >> 24;
echo '<div class="pix" style="background-color: rgba('.$r.', '.$g.', '.$b.', '.$a.');"></div>';
$count++;
if($count==$w) {echo '<br>'; $count = 0; }
}
}
echo $pixel_gen;
}
If oyu want to see what it looks like, clic here: http://narks.xtreemhost.com/
And double-click on any icon, popup will appear. (Note: dbl-clinking on any icon will show the same image (I didnt fixed this yet)
Any idea how i can make the black pixel appear like the real pixel with alpha) ?
Thanks for your help!
EDITED
(New code, i put only the first lines since i want to save space)
function go($image) {
// Open the image
$getimage=imagecreatefrompng($image);
imagealphablending($getimage, false);
imagesavealpha($getimage, true);
//Get the width/height
$w = imagesx($getimage);
$h = imagesy($getimage);
[...]
To see what it looks like now, visit the website above and double-click on an icon.
EDIT 2
I just tried (for a test) with :
$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png');
imagealphablending($getimage, false);
imagesavealpha($getimage, true);
header("Content-type: image/png");
imagepng($getimage);
imagedestroy($getimage);
and then with
$getimage=imagecreatefrompng('iconDB/lib/chat_fav_48.png');
header("Content-type: image/png");
imagepng($getimage);
imagedestroy($getimage);
The first is okay and the second make pixels black. So it is when i get each pixel's RGB colors and when I display it. Anyone see a mistake there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正确的工作代码,以便完成这个问题。
我希望它对某人有用
Here is the correct working code, in order to complete this question.
I hope it will be useful for someone
根据 imagecreatefrompng 页面上的注释,您需要调用 imagealphablending 和 imagesavealpha
该页面上还有其他关于 alpha 透明度和 PNG 的评论。
According to comments on the imagecreatefrompng page, you need to call imagealphablending and imagesavealpha
There are also other comments about alpha transparency and PNGs on that page.