php 图片透明,PHP怎么把一张图片透明化

这篇博客探讨了如何使用PHP将一张图片中指定的RGB颜色范围设置为透明。作者提供的代码示例原本旨在透明化指定颜色,但实际效果只移除了白色。问题可能在于代码逻辑只处理了超过起始RGB值的颜色,而未正确实现透明化功能。博客还提及了一个第三方库Intervention Image作为替代解决方案。
摘要由CSDN通过智能技术生成

b1f48e10138f5399044d7b19cc992bdd.png

PHP怎么把一张图片透明化?

具体问题:

把一张图片(根据指定的RGB颜色范围)透明化。但是实际处理当中,下面的代码值移除了白色,请教是怎么回事?$o_pic = '1.jpg';

//要处理的色阶起始值

$begin_r = 215;

$begin_g = 215;

$begin_b = 215;

list($src_w,$src_h,$src_type) = getimagesize($o_pic);// 获取原图像信息

$file_ext = get_ext($o_pic);//获取扩展名

$target_im = imagecreatetruecolor($src_w,$src_h);//新图

if($file_ext == 'jpg') //转换JPG 开始

{

$src_im = ImageCreateFromJPEG($o_pic);

imagecopymerge($target_im,$src_im,0,0,0,0,$src_w,$src_h,100);

for($x = 0; $x < $src_w; $x++)

{

for($y = 0; $y < $src_h; $y++)

{

$rgb = imagecolorat($src_im, $x, $y);

$r = ($rgb >> 16) & 0xFF;

$g = ($rgb >> 8) & 0xFF;

$b = $rgb & 0xFF;

if($r > $begin_r && $g > $begin_g && $b > $begin_b ){

imagecolortransparent($target_im, imagecolorallocate($target_im,$r, $g, $b));

}

}

}

}

方法:/**

* Created by PhpStorm.

* User: shellus

* Date: 2016-12-01

* Time: 23:12

*/

require 'vendor/autoload.php';

// import the Intervention Image Manager Class

use Intervention\Image\ImageManager;

// create an image manager instance with favored driver

$manager = new ImageManager(array('driver' => 'gd'));

$img = $manager->make('1.jpg');

for ($y = 0; $y < $img->height(); $y++)

{

for ($x = 0; $x < $img->width(); $x++)

{

$c = $img -> pickColor($x, $y, 'array');

if(abs($c[0] - 205) < 50 && abs($c[1] - 223) < 50 && abs($c[2] - 211) < 50 ){

$c[0] = $c[1] = $c[2] = 255;

$img -> pixel($c, $x, $y);

更多相关技术文章,请访问PHP中文网!

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值