PHP將圖片剪切成圓形

 免費API接口:http://www.wangshuwen.com/portal/article/detail/id/12.html 

 

   /**
     * @param  string $imgpath 要處理的圖片路徑
     * @return 圖片數據
     */
private function changeCircularImg($imgpath) {
        $ext     = pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpg':
                $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);
                }
            }
        }
        return $img;
    }
將返回的數據傳入進行保存:imagepng($img,$img_path);

保存後銷燬數據:imagedestroy($img);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章