PHP:如何使图像中的绿色区域(背景)透明? [英] PHP: How to make a green area (background) in an image transparent?

查看:166
本文介绍了PHP:如何使图像中的绿色区域(背景)透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Stackoverflow的新手。



我非常想知道是否可以使用PHP脚本使图像的特定颜色透明。如果是的话,该如何去做。



假设这是背景颜色(绿色)必须完全透明的图像:

first所有图片应该是 png 因为 jpeg 不支持透明度,那么代码是这样的:

 <?php 
$ image ='test.png';
$ im = imagecreatefrompng($ image);
//如果您确切知道RGB颜色索引
// $ rgb = imagecolorexact($ im,0,0,0);
//或按位置保持rgb颜色,因此在前0位左0
$ rgb = imagecolorat($ im,0,0);
imagecolortransparent($ im,$ rgb);
header(Content-type:image / png);
//直接显示图片
imagepng($ im);
//或保存
// imagepng($ im,'test-to-transparent.png');
imagedestroy($ im);
?>


I'm new here on Stackoverflow.

I would very much like figure out if it's possible to make a specific color of an image transparent using a PHP script. And if it is, how to go about doing it.

Let's say this is the image of which the background color (green) has to be completely transparent: French Hotdog w/ green background

I've got absolutely no idea what a script like this would look like or even where to begin. I imagine that you'd propably have to set the unwanted RGB color and then "scan" every single pixel in image to determine which pixels have to be transparent. But other than that I'm clueless.

Seriously hope someone here is able to help me out on this one.

解决方案

first of all the image should be png cause jpeg don't support transparency then the code is like this:

<?php
   $image = 'test.png';
   $im = imagecreatefrompng($image); 
   //if you exactly know the RGB color indexes
   //$rgb = imagecolorexact($im, 0, 0, 0);
   //or keep the rgb color by position so at top 0 left 0
   $rgb = imagecolorat($im, 0, 0);
   imagecolortransparent($im, $rgb);
   header("Content-type: image/png");
   //display the image directly
   imagepng($im);
   // or save it
   // imagepng($im, 'test-to-transparent.png');
   imagedestroy($im); 
?>

这篇关于PHP:如何使图像中的绿色区域(背景)透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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