我想通过将用户上传的图像与背景颜色合并来将其转换为页面背景。
要实现这种效果,我想将图像设置为灰度,然后替换颜色以匹配我的背景颜色(橙色),所以我想替换颜色(黑色与我的背景颜色匹配,然后每个灰度都消失了到另一个更清晰的值)。
grescale()方法还可以,但是我不知道如何进行替换。我尝试了colorize函数,但是即使我之前使用对比度()来使图像变暗,它似乎也只能使图像变亮。 LimitColors()也不起作用。
如何获得想要的效果?
我设法通过这种方法获得了想要的显示:
使用intervention's filters使其在其他地方更容易使用,这是我的结果:
<?php
namespace App\ImageFilters;
use Intervention\Image\Filters\FilterInterface;
use Intervention\Image\Image;
use Intervention\Image\ImageManager;
class MyFilter implements FilterInterface
{
/** @var ImageManager */
private $imageManager;
const BACKGROUND_COLOR = '#FCB65B';
public function __construct()
{
$this->imageManager = new ImageManager();
}
public function applyFilter(Image $image)
{
$height = $image->getHeight();
$width = $image->getWidth();
return $this->imageManager
->canvas($width, $height)
->fill(self::BACKGROUND_COLOR)
->mask($image, false);
}
}
在我的图像管理脚本中是这样使用的:
$image = $image->filter(New MyFilter());
发布前编辑:
greyscale()
将主图像转换为黑白图像,但是在省略它之后,可能会影响遮罩功能。在将其作为遮罩使用之前,我先将主图像弄亮了一点,我认为它最适合我的用途。
/ *用这个替换掩码呼叫* /-> mask($ image-> brightness(25),false);