使用PNG掩码的PHP图像层
我们正在尝试实现一个PHP服务器脚本,允许用户更改产品图像部分的颜色。例如,如果您有一把带红色织物座椅的木椅,并且只想更改织物颜色 我们以前在Flash中实现过一个系统;产品图像为JPG,颜色可编辑区域由存储为具有透明度的单独PNG的遮罩定义。使用遮罩的alpha值制作原始图像的副本,并将其放置在原始图像上方的新层上,实际上将颜色可编辑区域复制到新层上。然后,用户可以对该新图层进行颜色编辑 由于我们已经有了PNG掩码,我们正试图在PHP中实现相同的功能,以使更多的最终用户能够访问它。我试着从中修改脚本,并得出以下结论使用PNG掩码的PHP图像层,php,web,layer,mask,Php,Web,Layer,Mask,我们正在尝试实现一个PHP服务器脚本,允许用户更改产品图像部分的颜色。例如,如果您有一把带红色织物座椅的木椅,并且只想更改织物颜色 我们以前在Flash中实现过一个系统;产品图像为JPG,颜色可编辑区域由存储为具有透明度的单独PNG的遮罩定义。使用遮罩的alpha值制作原始图像的副本,并将其放置在原始图像上方的新层上,实际上将颜色可编辑区域复制到新层上。然后,用户可以对该新图层进行颜色编辑 由于我们已经有了PNG掩码,我们正试图在PHP中实现相同的功能,以使更多的最终用户能够访问它。我试着从中修
<?php
// Load source and mask
$source = imagecreatefromjpeg( 'source' );
$mask = imagecreatefrompng( 'destination' );
// Apply mask to source
imagealphamask( $source, $mask );
// Output
header( "Content-type: image/png");
imagepng( $source );
function imagealphamask( &$picture, $mask ) {
// 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 = $alpha['alpha'];
$color = imagecolorsforindex( $picture, imagecolorat( $picture, $x, $y ) );
//preserve alpha by comparing the two values
//if ($color['alpha'] > $alpha)
//$alpha = $color['alpha'];
//kill data for fully transparent pixels
if ($alpha == 127) {
$color['red'] = 0;
$color['blue'] = 0;
$color['green'] = 0;
}
imagesetpixel( $newPicture, $x, $y, imagecolorallocatealpha( $newPicture, $color[ 'red' ], $color[ 'green' ], $color[ 'blue' ], $alpha ) );
}
}
//TODO: colorize the new layer in some way here, e.g. imagefilter($newPicture, IMG_FILTER_COLORIZE, 150, 90, 0);
//place the new layer above the original image
imagecopymerge($picture,$newPicture,0,0,0,0,$xSize,$ySize,100);
}
?>
我不知道任何PHP,但上面的脚本似乎应该满足我们的需要。相反,它剪切出正确的区域,但用黑色将其包围
我真的不知道问题是什么,除非它与源图像是JPG而不是PNG有关。我尝试转换为truecolor,但它不影响输出
还有。。。这个代码太慢了。生成图像需要五秒钟以上的时间。我们是否应该采取完全不同的方法/使用不同的框架?我对web开发一无所知,但我被分配了这份工作,必须完成它。我应该阅读imagecopymerge的评论。文档错误地建议该函数支持alpha,但它不支持。评论中有一些解决方案,但生成图像仍需10秒以上,这是不可接受的,因此,如果有人在这方面有任何建议,我们将不胜感激。你找到解决方案了吗?我找到了,但老实说,已经很久了,我记不起解决方案了。他们不再按时给我工资,我也不再为他们工作,所以我再也无法访问这些文件。