Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将彩色像素替换为透明像素_Php_Image Processing_Gd_Alpha Transparency - Fatal编程技术网

PHP将彩色像素替换为透明像素

PHP将彩色像素替换为透明像素,php,image-processing,gd,alpha-transparency,Php,Image Processing,Gd,Alpha Transparency,我想用一种简单的方法制作一个有着坚实背景和透明背景的徽标,所以我决定使用第一个像素,并将所有图像上的颜色设置为透明,我知道这不是最好的解决方案,但我认为可以覆盖大多数情况 问题在于像素颜色为黑色而不是透明,这是我的代码: $im = $this->loadImage($targetFile); $this->replaceImageColor($im, imagecolorat($im, 0, 0), imageColorAllocateAlpha($im, 255, 255, 25

我想用一种简单的方法制作一个有着坚实背景和透明背景的徽标,所以我决定使用第一个像素,并将所有图像上的颜色设置为透明,我知道这不是最好的解决方案,但我认为可以覆盖大多数情况


问题在于像素颜色为黑色而不是透明,这是我的代码:

$im = $this->loadImage($targetFile);
$this->replaceImageColor($im, imagecolorat($im, 0, 0), imageColorAllocateAlpha($im, 255, 255, 255, 127));
imagepng($im, 'test.png');
我的班级功能:

function loadImage($imagePath) {
    $resource = false;
    if( strstr($imagePath, '.jpg') || strstr($imagePath, '.jpeg') )
        $resource = @imagecreatefromjpg($imagePath);
    if( strstr($imagePath, '.png') )
        $resource = @imagecreatefrompng($imagePath);

    return $resource;
}

function replaceImageColor($img, $from, $to) {
    // pixel by pixel grid.
    for ($y = 0; $y < imagesy($img); $y++) {
        for ($x = 0; $x < imagesx($img); $x++) {
            // find hex at x,y
            $at = imagecolorat($img, $x, $y);
            // set $from to $to if hex matches.
            if ($at == $from) 
                imagesetpixel($img, $x, $y, $to);
        }
    }
}
函数loadImage($imagePath){
$resource=false;
if(strstrstr($imagePath,'.jpg')| | strstr($imagePath,'.jpeg'))
$resource=@imagecreatefromjpg($imagePath);
if(strstr($imagePath,.png'))
$resource=@imagecreatefrompng($imagePath);
返回$resource;
}
函数replaceImageColor($img,$from,$to){
//逐像素网格。
对于($y=0;$y
最后我用这种方法解决了这个问题

$im = $this->loadImage($targetFileIcon);
$out = $this->transparentColorImage($im, imagecolorat($im, 0, 0));
imagepng($out, 'test.png');
imagedestroy($im);
imagedestroy($out);

function loadImage($imagePath) {
    $resource = false;
    if( strstr($imagePath, '.jpg') || strstr($imagePath, '.jpeg') )
        $resource = @imagecreatefromjpg($imagePath);
    if( strstr($imagePath, '.png') )
        $resource = @imagecreatefrompng($imagePath);

    return $resource;
}

function transparentColorImage($img, $color) {
    // pixel by pixel grid.
    $out = ImageCreateTrueColor(imagesx($img),imagesy($img));
    imagesavealpha($out, true);
    imagealphablending($out, false);
    $white = imagecolorallocatealpha($out, 255, 255, 255, 127);
    imagefill($out, 0, 0, $white);
    for ($y = 0; $y < imagesy($img); $y++) {
        for ($x = 0; $x < imagesx($img); $x++) {
            // find hex at x,y
            $at = imagecolorat($img, $x, $y);
            // set $from to $to if hex matches.
            if ($at != $color)
                imagesetpixel($out, $x, $y, $at);
        }
    }
    return $out;
}
$im=$this->loadImage($targetFileIcon);
$out=$this->transparentColorImage($im,imagecolorat($im,0,0));
imagepng($out,'test.png');
图像处理(港币);;
(a)支出(美元);
函数loadImage($imagePath){
$resource=false;
if(strstrstr($imagePath,'.jpg')| | strstr($imagePath,'.jpeg'))
$resource=@imagecreatefromjpg($imagePath);
if(strstr($imagePath,.png'))
$resource=@imagecreatefrompng($imagePath);
返回$resource;
}
函数透明彩色图像($img,$color){
//逐像素网格。
$out=ImageCreateTrueColor(imagesx($img),imagesy($img));
imagesavealpha($out,true);
ImageAlphaBling($out,false);
$white=imagecolorallocatealpha($out,255,255,255,127);
图像填充($FILL,0,0,$white);
对于($y=0;$y
我用alpha通道创建了一个真实的图像,没有alpha混合


BR

最后我用这种方法解决了这个问题

$im = $this->loadImage($targetFileIcon);
$out = $this->transparentColorImage($im, imagecolorat($im, 0, 0));
imagepng($out, 'test.png');
imagedestroy($im);
imagedestroy($out);

function loadImage($imagePath) {
    $resource = false;
    if( strstr($imagePath, '.jpg') || strstr($imagePath, '.jpeg') )
        $resource = @imagecreatefromjpg($imagePath);
    if( strstr($imagePath, '.png') )
        $resource = @imagecreatefrompng($imagePath);

    return $resource;
}

function transparentColorImage($img, $color) {
    // pixel by pixel grid.
    $out = ImageCreateTrueColor(imagesx($img),imagesy($img));
    imagesavealpha($out, true);
    imagealphablending($out, false);
    $white = imagecolorallocatealpha($out, 255, 255, 255, 127);
    imagefill($out, 0, 0, $white);
    for ($y = 0; $y < imagesy($img); $y++) {
        for ($x = 0; $x < imagesx($img); $x++) {
            // find hex at x,y
            $at = imagecolorat($img, $x, $y);
            // set $from to $to if hex matches.
            if ($at != $color)
                imagesetpixel($out, $x, $y, $at);
        }
    }
    return $out;
}
$im=$this->loadImage($targetFileIcon);
$out=$this->transparentColorImage($im,imagecolorat($im,0,0));
imagepng($out,'test.png');
图像处理(港币);;
(a)支出(美元);
函数loadImage($imagePath){
$resource=false;
if(strstrstr($imagePath,'.jpg')| | strstr($imagePath,'.jpeg'))
$resource=@imagecreatefromjpg($imagePath);
if(strstr($imagePath,.png'))
$resource=@imagecreatefrompng($imagePath);
返回$resource;
}
函数透明彩色图像($img,$color){
//逐像素网格。
$out=ImageCreateTrueColor(imagesx($img),imagesy($img));
imagesavealpha($out,true);
ImageAlphaBling($out,false);
$white=imagecolorallocatealpha($out,255,255,255,127);
图像填充($FILL,0,0,$white);
对于($y=0;$y
我用alpha通道创建了一个真实的图像,没有alpha混合


BR

你的问题是?问题是它的像素是黑色的,而不是透明的,4行。你的问题是?问题是它的像素是黑色的,而不是透明的,4线。