我有以下几点:
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;
}
}
它会按预期保存图像,但不会将 mask 应用于缩略图。
我哪里出错了(我使用的是 Laravel 5)?
此外,当脚本运行时,它实际上需要大约 1 分钟才能完成,所以它正在做一些事情,但图像仍然在没有应用 mask 的情况下输出。
最后我想我要用这些家伙https://www.imgix.com/
最佳答案
更新时间 2015-08-04 11:32 +0000
事实证明,白色透明度是 Imagine 中选择的 mask 逻辑。
https://github.com/avalanche123/Imagine/pull/449#issuecomment-127516157
原创
这很可能是 Imagine 库中的错误。我发现了以下内容:
I could not make GD\Image::applyMask() to work as described in Reflection example in http://www.slideshare.net/avalanche123/introduction-toimagine so I made some fixes.
- It still supports only RGB palette for mask, but now accounts average between colors.
- It does change image if its transparency is less than 0.5.
来自 https://github.com/avalanche123/Imagine/pull/449
相关修复尚未提交:
https://github.com/kasuparu/Imagine/commit/66a36652c76f9b5ff640f465d8f970c563841ae6
我尝试了固定代码,它似乎可以正常工作,除了掩码(从我的角度来看)向后应用,保留黑色部分并丢弃白色部分。我在拉取请求中评论了这个问题。
作为引用,这是正在执行的修复:
使用 $blackAmount:
我的修复方法是使用 $whiteAmount:
关于php - 使用 PHP Imagine 应用蒙版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31636085/