使用PHP Imagine应用蒙版

15

我有以下内容:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine;

class UploadController extends Controller {

    public function processImage($request) {
        $file = $request->file('file');

        $path = '/images';
        $fileName = 'image.png';

        if ($file) {
            $file->move('../public' . $path, $fileName);
            $gThumb = $this->createThumbnail(219, 300, '../public/images', 'image', 'png', 'thumb', true);
            $pThumb = $this->createThumbnail(300, 300, '../public/images', 'image', 'png', 'pthumb');
            return response()->json([
                'gallery_thumbnail' => $path . '/' . $gThumb,
                'upload_thumbnail' => $path . '/' . $pThumb
            ]);
        }
    }

    function createThumbnail($height, $width, $path, $filename, $extension, $postfix = null, $mask = null)
    {
        $mode = ImageInterface::THUMBNAIL_OUTBOUND;
        $size = new Box($width, $height);
        $postfix = $postfix ? $postfix : 'thumb';


        $thumbnail = Imagine::open("{$path}/{$filename}.{$extension}")->thumbnail($size, $mode);
        if ($mask) {
            $mask = Imagine::open('../public/images/masks/bubble-splash.png');
            $thumbnail->applyMask($mask);
        }
        $destination = "{$filename}" . "." . $postfix . "." . "{$extension}";

        $thumbnail->save("{$path}/{$destination}");
        return $destination;
    }
}

它将图像按预期保存,但不会将蒙版应用于缩略图。

我错在哪里了(我正在使用Laravel 5)?


此外,当脚本运行时,它需要大约1分钟才能完成,因此它正在做某些事情,但是输出的图像仍然没有应用蒙版。


最终,我想使用这些人 https://www.imgix.com/


这可能是个愚蠢的问题,但你确定在 createThumbnail() 函数的 $mask 参数中传递了一个真值吗? - Ben Claar
@Ben 是的,如果你往上看,你会看到我将'true'作为第七个参数传递,只是作为一个测试。通常这将是一个包含要应用的掩码名称的字符串。 - imperium2335
另外,你确定相对路径对你有效吗?使用绝对路径不是更好吗?比如 $mask = Imagine::open(__DIR__ . '/../public/images/masks/bubble-splash.png'); - Pedro Cordeiro
@PedroCordeiro 我已确认路径正确,图像也已被打开,所以这不是问题所在。 - imperium2335
那个库缺乏文档,源代码中也缺少注释,而 applyMask 的结果令人困惑:http://i.stack.imgur.com/cs2fc.png - spenibus
看起来好像反了 :s - imperium2335
1个回答

4

更新 2015-08-04 11:32 +0000

事实证明,Imagine库中选择了白色透明度作为掩码逻辑。
https://github.com/avalanche123/Imagine/pull/449#issuecomment-127516157

原文

这很可能是Imagine库中的一个错误。我找到了以下信息:

我无法使GD\Image::applyMask()像Reflection示例中描述的那样工作,所以我进行了一些修复。

  1. 它仍然只支持掩码的RGB调色板,但现在考虑了颜色之间的平均值。
  2. 如果透明度小于0.5,则会更改图像。

来自https://github.com/avalanche123/Imagine/pull/449

相关修复尚未提交:
https://github.com/kasuparu/Imagine/commit/66a36652c76f9b5ff640f465d8f970c563841ae6

我尝试了修复后的代码,似乎可以工作,只是掩码(在我的视角中)被反向应用,保留黑色部分并丢弃白色部分。我在拉取请求中对此问题发表了评论。

供参考,这是修复后的效果:

使用$blackAmount: php-imagine-applymask-using-blackamount-20150731-1831-gmt

我的修复,使用$whiteAmount: php-imagine-applymask-using-whiteamount-20150731-1831-gmt


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接