新聞中心
這篇文章主要講解了“php如何將圖片設(shè)置為圓形圖片”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php如何將圖片設(shè)置為圓形圖片”吧!
創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的察哈爾右翼前網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
php將圖片設(shè)置為圓形圖片的方法:1、創(chuàng)建一個PHP示例文件;2、創(chuàng)建一張透明的圖片;2、通過“function yuan_img($imgpath) {...}”方法把圖片處理成圓形即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php怎么將圖片設(shè)置為圓形圖片?
php 圖片圓形化處理:
用到的php gd庫函數(shù)有
imagecolorat imagesetpixel
首先是把圖片處理成圓形的:
原圖如下:
處理過之后效果:
用以下公式計算
(x-a)*(x-a)+(y-b)*(y-b)公式成立說明當(dāng)前x,y點(diǎn)在圓內(nèi)
x,y為當(dāng)前的坐標(biāo)
a,b為圓的圓心位置
r為半徑
先創(chuàng)建一張透明的圖片,
然后一行一行的掃描原圖如圖像素點(diǎn)在圓內(nèi)就畫出這個像素不在的就保持透明色就可以
function yuan_img($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); //拾取一個完全透明的顏色,最后一個參數(shù)127為全透明 $bg = imagecolorallocatealpha($img, 255, 255, 255, 127); imagefill($img, 0, 0, $bg); $r = $w / 2; //圓半徑 $y_x = $r; //圓心X坐標(biāo) $y_y = $r; //圓心Y坐標(biāo) 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; }感謝各位的閱讀,以上就是“php如何將圖片設(shè)置為圓形圖片”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對php如何將圖片設(shè)置為圓形圖片這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
分享文章:php如何將圖片設(shè)置為圓形圖片
文章網(wǎng)址:http://fisionsoft.com.cn/article/pjjehc.html