繁体   English   中英

干预图像圆角上传

[英]Intervention Image rounded corners upload

我正在尝试将我的文件上传为圈子,但我无法使其正常工作。 我已经看到了一些关于将掩码应用于图像的主题,但是当我应用掩码时,它需要很长时间并且服务器关闭请求。

我正在使用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);
}

这是一个适合我的功能。 但是, 只有我使用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");

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM