TP6 圖片處理為圓形透明圖片

 /**
     * 處理圓角圖片
     * @param  integer $radius  圓角半徑長(zhǎng)度默認(rèn)為15,處理成圓型
     * @return [type]           [description]
     */
    public function radius($radius = 15) {
        // $wh = getimagesize($imgpath);
        $w = $this->width();
        $h = $this->height();
        // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
        $img = imagecreatetruecolor($w, $h);
        //這一句一定要有
        imagesavealpha($img, true);
        //拾取一個(gè)完全透明的顏色,最后一個(gè)參數(shù)127為全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r = $radius; //圓 角半徑
        for ($x = 0; $x < $w; $x++) {
            for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($this->im, $x, $y);
                if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
                    //不在四角的范圍內(nèi),直接畫(huà)
                    imagesetpixel($img, $x, $y, $rgbColor);
                } else {
                    //在四角的范圍內(nèi)選擇畫(huà)
                    //上左
                    $y_x = $r; //圓心X坐標(biāo)
                    $y_y = $r; //圓心Y坐標(biāo)
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                        continue;
                    }
                    //上右
                    $y_x = $w - $r; //圓心X坐標(biāo)
                    $y_y = $r; //圓心Y坐標(biāo)
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                        continue;
                    }
                    //下左
                    $y_x = $r; //圓心X坐標(biāo)
                    $y_y = $h - $r; //圓心Y坐標(biāo)
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                        continue;
                    }
                    //下右
                    $y_x = $w - $r; //圓心X坐標(biāo)
                    $y_y = $h - $r; //圓心Y坐標(biāo)
                    if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
                        imagesetpixel($img, $x, $y, $rgbColor);
                        continue;
                    }
                }
            }
        }
        $this->im = $img;
        return $this;
    }

放到使用TP5開(kāi)發(fā)手冊(cè)雜項(xiàng)中圖像處理插件
使用方法:

Image::open('./logo.png')->thumb(100,100,\think\Image::THUMB_SOUTHEAST)->radius(20)->save('./logo.png','png');

注意:save保存時(shí)一定要加第二個(gè)參數(shù),否則不是透明的

附:獲取url大圖片處理方法

 $ch = curl_init($imgUrl);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $logoImg = curl_exec($ch);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容