与PIL'对应;PHP中的s Image.paste
我被要求将Python应用程序移植到PHP(我不太喜欢PHP) 我很难移植的部分使用了一组单色的“模板”图像,这些图像基于Wonder by。这些模板图像用于创建具有自定义背景和前景颜色的图标。PIL的Image.paste用于使用模板图像作为alpha遮罩,以选定的颜色“粘贴”图标前景。例如: 我如何在PHP中复制它?除了逐像素进行外,还有其他选择吗 [更新] 我并不以我的PHP技能为荣。。。到目前为止,我得到的是:与PIL'对应;PHP中的s Image.paste,php,python,gd,python-imaging-library,Php,Python,Gd,Python Imaging Library,我被要求将Python应用程序移植到PHP(我不太喜欢PHP) 我很难移植的部分使用了一组单色的“模板”图像,这些图像基于Wonder by。这些模板图像用于创建具有自定义背景和前景颜色的图标。PIL的Image.paste用于使用模板图像作为alpha遮罩,以选定的颜色“粘贴”图标前景。例如: 我如何在PHP中复制它?除了逐像素进行外,还有其他选择吗 [更新] 我并不以我的PHP技能为荣。。。到目前为止,我得到的是: <?php header('Content-type: image
<?php
header('Content-type: image/png');
// read parameters: icon file, foreground and background colors
$bgc = sscanf(empty($_GET['bg']) ? 'FFFFFF' : $_GET['bg'], '%2x%2x%2x');
$fgc = sscanf(empty($_GET['fg']) ? '000000' : $_GET['fg'], '%2x%2x%2x');
$icon = empty($_GET['icon']) ? 'base.png' : $_GET['icon'];
// read image information from template files
$shadow = imagecreatefrompng("../static/img/marker/shadow.png");
$bg = imagecreatefrompng("../static/img/marker/bg.png");
$fg = imagecreatefrompng("../static/img/marker/" . $icon);
$base = imagecreatefrompng("../static/img/marker/base.png");
imagesavealpha($base, true); // for the "shadow"
// loop over every pixel
for($x=0; $x<imagesx($base); $x++) {
for($y=0; $y<imagesy($base); $y++) {
$color = imagecolorsforindex($bg, imagecolorat($bg, $x, $y));
// templates are grayscale, any channel serves as alpha
$alpha = ($color['red'] >> 1) ^ 127; // 127=transparent, 0=opaque.
if($alpha != 127) { // if not 100% transparent
imagesetpixel($base, $x, $y, imagecolorallocatealpha($base, $bgc[0], $bgc[1], $bgc[2], $alpha));
}
// repeat for foreground and shadow with foreground color
foreach(array($shadow, $fg) as $im) {
$color = imagecolorsforindex($im, imagecolorat($im, $x, $y));
$alpha = ($color['red'] >> 1) ^ 127;
if($alpha != 127) {
imagesetpixel($base, $x, $y, imagecolorallocatealpha($base, $fgc[0], $fgc[1], $fgc[2], $alpha));
}
}
}
}
// spit image
imagepng($base);
// destroy resources
foreach(array($shadow, $fg, $base, $bg) as $im) {
imagedestroy($im);
}
?>
它工作正常,性能也不错。根据我的评论,ImageMagick可以做到这一点。但是,您已经指出,对于您的用例来说,这可能不是非最佳的,所以请考虑使用GD2。在PHP网站上有一个演示如何做
我猜这可以在任何(相当新的)默认PHP安装上完成。不确定在要求PHP开发人员帮助您的同时还说您不喜欢他们的语言是否明智;-)如果这些是PNG图像,如果GD2不能做到这一点,ImageMagick将是一个不错的选择。@halfer:他们可以自由地憎恨Python:-)嘿,真的!让我们知道你是如何使用IM的——或者如果你有任何GD2代码不起作用。是吗?@halfer:我试图避免使用ImageMagick,因为它没有安装在部署服务器上,而且赞助商对更改环境非常挑剔。ImageMagick听起来是个不错的建议,谢谢。