使用 Intervention/image 包进行图片处理(裁剪成圆形)。怎么把四周的空白去掉?

file

由于这个包没有裁剪成圆形的(自己一直没找到),然后通过扩展过滤器让他有裁剪成圆形的方法。现在可以裁剪成一个圆,但是我要把四周的白色也去掉(由于创建的是正方形的画布,php好像没有创建圆形的画布)。不知有遇到这样问题的小伙伴吗? 或者哪位大神可以指导一下

过滤器

    <?php

    namespace Intervention\Image\Filters;

    class DemoFilter implements FilterInterface
    {
        /**
         * Default size of filter effects
         */
        const DEFAULT_SIZE = 10;

        /**
         * Size of filter effects
         *
         * @var integer
         */
        private $size;
        public $image;
        /**
         * Creates new instance of filter
         *
         * @param integer $size
         */
        public function __construct($size = null)
        {
            $this->size = is_numeric($size) ? intval($size) : self::DEFAULT_SIZE;
        }
        function get_lt_rounder_corner($radius='200')
        {
            $img = imagecreatetruecolor($radius, $radius);  // 创建一个正方形的图像
            $bgcolor = imagecolorallocate($img, 255, 255, 255);   // 图像的背景
            $trans_colour = imagecolorallocatealpha($img, 255, 255, 255, 127);
            imageellipse($img,30,30,40,40,$bgcolor);
            $fgcolor = imagecolorallocate($img, 0, 0, 0);
            imagefill($img, 0, 0, $trans_colour);
            imagecolortransparent($img,$trans_colour);

            // $radius,$radius:以图像的右下角开始画弧
            // $radius*2, $radius*2:已宽度、高度画弧
            // 180, 270:指定了角度的起始和结束点
            // fgcolor:指定颜色
            imagefilledarc($img, $radius, $radius, $radius * 2, $radius * 2, 180, 270, $fgcolor, IMG_ARC_PIE);
            // 将弧角图片的颜色设置为透明
            imagecolortransparent($img, $fgcolor);

            return $img;
        }

        /**
         * Applies filter effects to given image
         *
         * @param  \Intervention\Image\Image $image
         * @return \Intervention\Image\Image
         */
        public function applyFilter(\Intervention\Image\Image $image)
        {
            $img=$image->getCore();
            $radius = 100;
    // lt(左上角)
            $lt_corner = $this->get_lt_rounder_corner($radius);
            imagecopymerge($img, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
    // lb(左下角)
            $lb_corner = imagerotate($lt_corner, 90, 0);
            imagecopymerge($img, $lb_corner, 0, 200 - $radius, 0, 0, $radius, $radius, 100);
    // rb(右上角)
            $rb_corner = imagerotate($lt_corner, 180, 0);
            imagecopymerge($img, $rb_corner, 200 - $radius, 200 - $radius, 0, 0, $radius, $radius, 100);
    // rt(右下角)
            $rt_corner = imagerotate($lt_corner, 270, 0);
            imagecopymerge($img, $rt_corner, 200 - $radius, 0, 0, 0, $radius, $radius, 100);
    //        $image->pixelate($this->size);
    //        $image->greyscale();

            return $image;
        }
    }

调用
file

Make everything simple instead of making difficulties as simple as possible
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 4

这是jpg格式导致,存为png试试,看是不是好了:smile:

6年前 评论
jcc123

@丁海军 引用的方法不对(图片四个角其实是白色的,不是透明的)

6年前 评论

@jc91715 那已经找到方法了?分享下

6年前 评论
jcc123

@丁海军 不记得在哪找了。制作圆形透明图像
呐 这是代码

    public $path;
    function yuan_img($imgpath = null) {
        $ext     = pathinfo($imgpath );
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpg':
                $src_img = imagecreatefromjpeg($imgpath);
                break;
            case 'jpeg':
                $src_img = imagecreatefromjpeg($imgpath);
                break;
            case 'png':
                $src_img = imagecreatefrompng($imgpath);
                break;
        }
        $wh  = getimagesize($imgpath);
        $w   = $wh[0];
        $h   = $wh[1];
        $w   = min($w, $h);
        $h   = $w;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r   = $w /2; //圆半径
        $y_x = $r; //圆心X坐标
        $y_y = $r; //圆心Y坐标
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                    imagesetpixel($img, $x, $y, $rgbColor);
                }
            }
        }
        $filename = date('Y-m-d-H-i-s').'-'.uniqid().'.png';

        imagepng($img,'storage/'.$filename);

        return $this->path='storage/'.$filename;
    }
6年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!