使用PHP GD对一个图像进行遮罩 [英] Masking one image against another using PHP GD

查看:40
本文介绍了使用PHP GD对一个图像进行遮罩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下问题: PHP GD使用一张图片掩盖另一张图片,包括透明度-此处的规则要求用户创建新问题,而不是重新访问旧问题并寻求支持

Please see this question: PHP GD Use one image to mask another image, including transparency - The rules around here say for users to create new questions rather than revisit old ones and ask for support

我一直在使用此脚本来启用透明遮罩-(可能的)区别是源图像具有透明性,但是下面的代码似乎仅在输入PNG不透明的情况下才有效.有人可以看看我做错了什么吗?

I've been working with this script to enable transparent masking - the (possible) difference being that the source image has transparency, but it seems like the code below only works if the input PNGs have no transparency. Can someone have a look and see if I'm doing anything wrong?

我正在尝试以下操作: 1.获取$ source图像 2.调整大小并将其另存为pjg.png,保持透明度(可以正常运行) 3.使用另一个PNG遮罩生成的图像.

What I'm trying to do below: 1. Grab a $source image 2. Resize it and save it locally as pjg.png, maintaining transparency (this works ok) 3. Mask the resultant image with another PNG.

信息:

  • image.png具有透明度.
  • mask1.png是黑色背景上的白色椭圆形,没有透明度
  • 保存在最末端的图像上应该是黑色的,这时它应该始终保持透明.

  • image.png has transparency.
  • mask1.png is a white oval on black background, no transparency
  • The image saved at the very end has black on it, when it should maintain transparency throughout.

<?php
$data       = file_get_contents('assets/img/image.png');
$source     = imagecreatefromstring( $data);


// Set the percentage resize

$percent = 0.5;

// Get new dimensions
list($width, $height) = getimagesize('assets/img/image.png');
$new_width = $width * $percent;
$new_height = $height * $percent;

$image_p = imagecreatetruecolor($new_width, $new_height);
imagealphablending($image_p, false);
imagesavealpha( $image_p, true );

imagecopyresampled($image_p, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagepng($image_p, "assets/img/pjg.png");

$mask_id = 1;


create_mask( $image_p, $mask_id ); 


function create_mask( &$picture, $mask_id) {
// Image masking using PHP
// https://stackoverflow.com/questions/7203160/php-gd-use-one-image-to-mask-another-image-including-transparency

$mask = imagecreatefrompng( 'assets/img/masks/mask'.$mask_id.'.png' );   // The mask is a white-on-black png



// Get sizes and set up new picture
$xSize = imagesx( $picture );
$ySize = imagesy( $picture );
$newPicture = imagecreatetruecolor( $xSize, $ySize );

imagesavealpha($newPicture, true);  
imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) );


// Resize mask if necessary
if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) {
    $tempPic = imagecreatetruecolor( $xSize, $ySize );
    imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) );
    imagedestroy( $mask );
    $mask = $tempPic;
}

// Perform pixel-based alpha map application
for( $x = 0; $x < $xSize; $x++ ) {
    for( $y = 0; $y < $ySize; $y++ ) {
        $alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) );
        $alpha = 127 - floor( $alpha[ 'red' ] / 2 );
        $color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );
        imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) );
    }
}

$salt = random_string('alnum', 8); // Another function generating a string, not important
$now = time();
$new_filename = $now."_".$salt .".png";

// Save it Locally using a unique name
imagepng($newPicture, "assets/img/uploads/cropped/".$new_filename);


// Copy back to original picture
imagedestroy( $picture );
$picture = $newPicture;

}

如果任何人都可以指出为什么输出图像不能保持其透明度,那么其中就有一杯冰镇啤酒供您使用.

If anyone could point out why the output image is not keeping its transparency, there'd be a nice cold beer in it for you.

谢谢!

PJG

推荐答案

我已经解决了.原始脚本未检查源图像的透明度.下面的脚本检查源图像的像素透明度,并采取相应的措施.下面的脚本在PNG图像上执行形状屏蔽,并保持源图像的透明度.

I've worked it out. The original script was not checking for the transparency of the source image. The below script checks the source image for pixel transparency, and acts accordingly. The below script preforms a shap mask on a PNG image, and maintains the source image's transparency.

<?php
$data       = file_get_contents('assets/img/image.png');
$source     = imagecreatefromstring( $data);


// Set the percentage resize

$percent = 0.5;

// Get new dimensions
list($width, $height) = getimagesize('assets/img/image.png');
$new_width = $width * $percent;
$new_height = $height * $percent;

$image_p = imagecreatetruecolor($new_width, $new_height);
imagealphablending($image_p, false);
imagesavealpha( $image_p, true );

imagecopyresampled($image_p, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

imagepng($image_p, "assets/img/pjg.png");

$mask_id = 1;


create_mask( $image_p, $mask_id ); 


function create_mask( &$picture, $mask_id) {
// Image masking using PHP
// http://stackoverflow.com/questions/7203160/php-gd-use-one-image-to-mask-another-image-including-transparency

$mask = imagecreatefrompng( 'assets/img/masks/mask'.$mask_id.'.png' );   // The mask is a white-on-black png



// Get sizes and set up new picture
$xSize = imagesx( $picture );
$ySize = imagesy( $picture );
$newPicture = imagecreatetruecolor( $xSize, $ySize );

imagesavealpha($newPicture, true);  
imagefill( $newPicture, 0, 0, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) );


// Resize mask if necessary
if( $xSize != imagesx( $mask ) || $ySize != imagesy( $mask ) ) {
    $tempPic = imagecreatetruecolor( $xSize, $ySize );
    imagecopyresampled( $tempPic, $mask, 0, 0, 0, 0, $xSize, $ySize, imagesx( $mask ), imagesy( $mask ) );
    imagedestroy( $mask );
    $mask = $tempPic;
}

// Perform pixel-based alpha map application
for( $x = 0; $x < $xSize; $x++ ) {
    for( $y = 0; $y < $ySize; $y++ ) {
         $alpha = imagecolorsforindex( $mask, imagecolorat( $mask, $x, $y ) );

            if(($alpha['red'] == 0) && ($alpha['green'] == 0) && ($alpha['blue'] == 0) && ($alpha['alpha'] == 0))
            {
                // It's a black part of the mask
                imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) ); // Stick a black, but totally transparent, pixel in.
            }
            else
            {

                // Check the alpha state of the corresponding pixel of the image we're dealing with.    
                $alphaSource = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );

                if(($alphaSource['alpha'] == 127))
                {
                    imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 ) ); // Stick a black, but totally transparent, pixel in.
                } 
                else
                {
                    $color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );
                    imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $color['alpha'] ) ); // Stick the pixel from the source image in
                }


            }
    }
}

$salt = random_string('alnum', 8); // Another function generating a string, not important
$now = time();
$new_filename = $now."_".$salt .".png";

// Save it Locally using a unique name
imagepng($newPicture, "assets/img/uploads/cropped/".$new_filename);


// Copy back to original picture
imagedestroy( $picture );
$picture = $newPicture;

}
?>

阅读的欢呼声-看来我以后会给自己买啤酒:)

Cheers for reading - looks like I'll be buying myself a beer later :)

PG

这篇关于使用PHP GD对一个图像进行遮罩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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