PHP中的imagecolortransparent无法正常工作 [英] imagecolortransparent in PHP not working

查看:225
本文介绍了PHP中的imagecolortransparent无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改图片中的白色( http:/ /www.arso.gov.si/vreme/napovedi%20in%20podatki/radar.gif )透明。我认为代码看起来正确,没有错误日志,但图片保持不变。我仔细检查图像中的颜色是否真的是白色而且是。请帮忙。

I want to change white color in image (http://www.arso.gov.si/vreme/napovedi%20in%20podatki/radar.gif) to transparent. I think that code looks right, there is no error logs but picture stays unchanged. I double checked if color in image is really white and it is. Please help.

<?php
    $im = imagecreatefromgif("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/radar.gif");

    imagealphablending($im, false);
    imagesavealpha($im, true);

    $white = imagecolorallocate($im, 255, 255, 255);

    imagecolortransparent($im, $white);

    imagegif($im, './image_radar_tran.gif');
    imagedestroy($im);
?>
<body style="background-color: lightgoldenrodyellow;">
    <img src="./image_radar_tran.gif" />
</body>


推荐答案

更改:

$white = imagecolorallocate($im, 255, 255, 255);

收件人:

$white = imagecolorexact($im, 255, 255, 255);

它会起作用。原因是颜色'white'已在您使用的gif的索引中定义,因此您无法为该颜色分配新索引。相反,使用 imagecolorexact ,您将获得要使用的白色的现有索引,然后可以将其更改为透明。

And it will work. The reason is that the color 'white' is already defined in the index of the gif you are using, so you can't allocate a new index for that color. Instead, using imagecolorexact, you get the existing index for the white color to use and can then change it to transparent.

这篇关于PHP中的imagecolortransparent无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆