在PHP中与PIL的Image.paste相对应 [英] counterpart to PIL's Image.paste in PHP

查看:144
本文介绍了在PHP中与PIL的Image.paste相对应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人要求我将Python应用程序移植到PHP(我不太喜欢PHP).

I was asked to port a Python application to PHP (and I'm not very fond of PHP).

我难以移植的部分使用了一组基于精美的地图图标的单色模板"图像集尼古拉斯·莫莱特收藏.这些模板图像用于创建具有自定义背景和前景色的图标. PIL的Image.paste用于使用模板图像作为Alpha蒙版粘贴"具有所选颜色的图标前景.例如:

The part I'm having trouble to port uses a set of monochromatic "template" images based on the wonderful Map Icons Collection by Nicolas Mollet. These template images are used to create an icon with custom background and foreground colors. PIL's Image.paste is used to "paste" the icon foreground with the selected color using the template Image as alpha mask. For example:

如何在PHP中复制它?除了逐像素进行处理之外,还有其他选择吗?

How can I replicate this in PHP? Is there any alternative besides doing it pixel-by-pixel?

[更新]

我不为自己的PHP技能感到骄傲...到目前为止我所拥有的:

I'm not proud of my PHP skills... What I've got so far:

<?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);
}

?>

它可以正常工作,而且性能还不错.

It's working and performance is not bad.

推荐答案

根据我的评论,ImageMagick可以做到这一点.但是,您已经指出,对于您的用例,这可能并非最佳选择,因此请考虑使用GD2.关于如何执行图像合并上的演示PHP网站.

As per my comments, ImageMagick would be able to do this. However you've indicated that this may not be non-optimal for your use case, so consider using GD2. There's a demo on how to do image merging on the PHP site.

我想这可以在任何(相当新的)默认PHP安装上完成.

I would guess that this can be done on any (fairly recent) default PHP installation.

这篇关于在PHP中与PIL的Image.paste相对应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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