php 把圖片變成圓形,邊框透明, 頭像改成圓形


比如頭像是方形的

 

 

我要一個圓形的  邊框透明的

(我這個是截圖的.png   實際是透明邊框)

 

 

代碼如下

function circular_img($imgurl,$dest_path){
        //第一個參數為網絡圖片 或者改一下為本地圖片

            // $src = imagecreatefromstring(file_get_contents('http://xiaohe520.club/Public/Home/default/images/qr.png')); //獲取網絡資源文件
    
    
        //本地圖片改一下參數
            // $ext=pathinfo($img);
            // $src = null;
            // switch ($ext['extension']) {
            //     case 'jpg':
            //         $src=imagecreatefromjpeg($imgurl);
            //         break;
            //     case 'png':
            //         $src=imagecreatefrompng($img);
            //         break;
            // }

            $src = imagecreatefromstring(file_get_contents($img)); //獲取網絡資源文件
            $wh= getimagesize($img);
            $w=$wh[0];
            $h=$wh[1];
            $w=min($w,$h);
            $h= $w;
    
            $newpic = imagecreatetruecolor($w,$h); 
            //建立的是一幅大小為 x和 y的黑色圖像(默認為黑色),如想改變背景顏色則需要用填充顏色函數imagefill($img,0,0,$color);
            // $img = imagecreatetruecolor(100,100);    //創建真彩圖像資源
            // $color = imagecolorAllocate($img,200,200,200);   //分配一個灰色
            // imagefill($img,0,0,$color);
    
    
            // 啟用混色模式
            imagealphablending($newpic,false); //設定圖像的混色模式
    
            //imagealphablending() 允許在真彩色圖像上使用兩種不同的繪畫模式。
            // 在混色(blending)模式下,alpha 通道色彩成分提供給所有的繪畫函數,例如 imagesetpixel() 決定底層的顏色應在何種程度上被允許照射透過。作為結果,GD 自動將該點現有的顏色和畫筆顏色混合,並將結果儲存在圖像中。結果的像素是不透明的。
            // 在非混色模式下,畫筆顏色連同其 alpha 通道信息一起被拷貝,替換掉目標像素。混色模式在畫調色板圖像時不可用。
            // 如果 blendmode 為 TRUE,則啟用混色模式,否則關閉。成功時返回 TRUE, 或者在失敗時返回 FALSE。
    
    
            $transparent = imagecolorallocatealpha($newpic, 255, 255, 255, 127);//邊緣透明
    
            //imagecolorallocatealpha(resource $image , int $red , int $green , int $blue, int $alpha )
            // $image 圖像資源,通過創造的圖像功能,如,一返回imagecreatetruecolor()。
            // $red 紅色分量的價值。
            // $green 價值的綠色成分。
            // $blue 藍色成分的價值。
            // $alpha 一個介於0和127的價值。 0表示完全不透明,而127表示完全透明。
    
    
            $r=$w/2;
            for($x=0;$x<$w;$x++)
                for($y=0;$y<$h;$y++){
                    $c = imagecolorat($src,$x,$y);
                    $_x = $x - $w/2;
                    $_y = $y - $h/2;
                    if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){
                        imagesetpixel($newpic,$x,$y,$c);
                    }else{
                        imagesetpixel($newpic,$x,$y,$transparent);
                        //imagesetpixel() 在 image 圖像中用 color 顏色在 x,y 坐標(圖像左上角為 0,0)上畫一個點。
                    }
                }
    
            //imagesavealpha() 設置標記以在保存 PNG 圖像時保存完整的 alpha 通道信息(與單一透明色相反)
            imagesavealpha($newpic, true);
            // header('Content-Type: image/png');
            imagepng($newpic, $dest_path);
            imagedestroy($newpic);
            imagedestroy($src);
    
            // unlink() 函數刪除文件。
            // 若成功,則返回 true,失敗則返回 false。
            // unlink($url);
    
            return $dest_path;
}

echo
circular_img('http://xiaohe520.club/Public/Home/default/images/qr.png','QQ496631085.png');
 
        

本地圖片取消注釋改一下參數   

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM