dongxuandong2045 2014-11-19 17:51
浏览 72
已采纳

PNG透明度问题

I have the following problem. I have a PNG file (http://meteopage.com/beta/image.png), which is already partially transparent and I would like to make the "pink" border also transparent. I have read some of the instructions here and came up with a code, which does replace the border with transparent point, but all the rest, which was already transparent, turns into black: http://meteopage.com/beta/radar2.php

My code looks like this:

<?php
$file="image.png";

$im = imagecreatefrompng($file);

imagealphablending($im, false);
$new = imagecolorclosest($im, 255, 0, 255);
imagecolortransparent($im, $new);
imagesavealpha($im, true);


header('Content-type: image/png');

imagepng($im);


?>

I tried adding those imagesavealpha and imagealphablending commands, but still no luck, it is black as you can see in the link Ive attached. Would anyone know how to maintain the original transparency and just "add" those pink pixels to it?

  • 写回答

1条回答 默认 最新

  • doubiao7267 2014-11-19 21:24
    关注

    Your image is palette-based, not true colour, so the calls to imagealphablending() and imagesavealpha() aren't needed.

    The simplest way to do this is to replace the pink colour in the palette-index with a transparent one, like so:

    $file = 'image.png';
    
    $im = imagecreatefrompng($file);
    
    $new = imagecolorclosest($im, 255, 0, 255);
    imagecolorset($im, $new, 255, 0, 255, 127);
    
    header('Content-type: image/png');
    imagepng($im);
    

    Note that this requires PHP >= 5.4 otherwise imagecolorset() won't accept the alpha parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败