Intervention Image圆角上传

7

我想把我的文件上传成圆形,但是无法实现。

我看到一些关于给图像应用蒙版的话题,但是当我应用蒙版时,它花费的时间太长了,服务器关闭了该请求。

我正在使用 Laravel 的 Intervention Image 库。

以下是我的代码:

$identifier = "{$this->loggedUser->id}" . str_random(9) . ".{$file->getClientOriginalExtension()}";
$mask = $this->createCircleMask(200, 200);
$thumbMask = $this->createCircleMask(40, 40);
Image::make($file->getRealPath())->mask($mask)->save(public_path("images/profile/{$identifier}"));
Image::make($file->getRealPath())->mask($thumbMask)->save(public_path("images/profile/thumbs/{$identifier}"));

createCircleMask 方法的代码如下:

public function createCircleMask($width, $height)
{
    $circle = Image::canvas($width, $height, '#000000');
    return $circle->circle($width - 1, $width / 2, $height / 2);
}
1个回答

13

这里有一个在我的情况下有效的函数。但是仅当我使用imagick驱动程序时才有效。标准的gd库非常非常慢,至少在我的测试计算机上是这样。 你可以查看vendor \ intervention \ image \ src \ Intervention \ Image \ Gd \ Commands \ MaskCommand.php找出原因。

public function upload() {

    $path = storage_path('app')."/";

    $image = \Image::make(\Input::file('image'));
    $image->encode('png');

    /* if you want to have a perfect and complete circle using the whole width and height the image
     must be shaped as as square. If your images are not guaranteed to be a square maybe you could
     use Intervention's fit() function */
    //  $image->fit(300,300);

    // create empty canvas
    $width = $image->getWidth();
    $height = $image->getHeight();
    $mask = \Image::canvas($width, $height);

    // draw a white circle
    $mask->circle($width, $width/2, $height/2, function ($draw) {
        $draw->background('#fff');
    });

    $image->mask($mask, false);
    $image->save($path."circled.png");

}

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