小追兵專欄
文章摘要:
上一篇文章,我說了關于php把文字畫在圖片上的換行方法,這篇說說項目中圖片圓角的處理
我們可能在很多項目中,需要對圖片進行圓角處理,例如HTML5中,例如Android中;
這里我們說說用PHP對圖片進行圓角處理的方法;
圓角處理的方法有很多,這里我就只說說我的項目中是怎么對處理圓角的;
效果圖如下:
如圖可見,不論是插圖,還是文字這背景,我們都做了圓角處理,下面我看看在PHP中,我們是怎么實現(xiàn)的吧。
這里我先說說實現(xiàn)的思路,我們是先畫出直角的圖,然后再用一個圓角去覆蓋在直角上面,這樣看起來就是一個圓角了。
一、背景圖圓角處理
方法調用:
//整個圖,也就是白色背景
$im = imagecreatetruecolor(750, 3000);
$bgcolor = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $bgcolor);
//生成漢字的背景矩形
$image_width = 694;//圓角淡色背景的寬694px
$image_height = 368;//圓角淡色背景的高368px
//矩形上面加圓角
$radius = 10;//圓角的像素,值越大越圓
$dst_x = 28;//距離白色大背景左邊的距離
$y = 40;//距離白色大背景頂端的距離
//這里調用函數(shù),繪制淡色的圓角背景,
imagebackgroundmycard($im, $dst_x, $y, $image_width, $image_height, $radius);
以上是調用函數(shù)的說明,下面我們給出函數(shù)方法:
方法實現(xiàn):
/**
* 畫一個帶圓角的背景圖
* @param $im 底圖
* @param $dst_x 畫出的圖的(0,0)位于底圖的x軸位置
* @param $dst_y 畫出的圖的(0,0)位于底圖的y軸位置
* @param $image_w 畫的圖的寬
* @param $image_h 畫的圖的高
* @param $radius 圓角的值
*/
function imagebackgroundmycard($im, $dst_x, $dst_y, $image_w, $image_h, $radius)
{
$resource = imagecreatetruecolor($image_w, $image_h);
$bgcolor = imagecolorallocate($resource, 0xef, 0xef, 0xe1);//該圖的背景色
imagefill($resource, 0, 0, $bgcolor);
$lt_corner = get_lt_rounder_corner($radius, 255, 255, 255);//圓角的背景色
// lt(左上角)
imagecopymerge($resource, $lt_corner, 0, 0, 0, 0, $radius, $radius, 100);
// lb(左下角)
$lb_corner = imagerotate($lt_corner, 90, 0);
imagecopymerge($resource, $lb_corner, 0, $image_h - $radius, 0, 0, $radius, $radius, 100);
// rb(右上角)
$rb_corner = imagerotate($lt_corner, 180, 0);
imagecopymerge($resource, $rb_corner, $image_w - $radius, $image_h - $radius, 0, 0, $radius, $radius, 100);
// rt(右下角)
$rt_corner = imagerotate($lt_corner, 270, 0);
imagecopymerge($resource, $rt_corner, $image_w - $radius, 0, 0, 0, $radius, $radius, 100);
imagecopy($im, $resource, $dst_x, $dst_y, 0, 0, $image_w, $image_h);
}
上面函數(shù)方法依賴的函數(shù):
/** 畫圓角
* @param $radius 圓角位置
* @param $color_r 色值0-255
* @param $color_g 色值0-255
* @param $color_b 色值0-255
* @return resource 返回圓角
*/
function get_lt_rounder_corner($radius, $color_r, $color_g, $color_b)
{
// 創(chuàng)建一個正方形的圖像
$img = imagecreatetruecolor($radius, $radius);
// 圖像的背景
$bgcolor = imagecolorallocate($img, $color_r, $color_g, $color_b);
$fgcolor = imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgcolor);
// $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;
}
最后輸出圖片:
講淺色背景圓角處理的已經完成了,如果你想看看最后的效果,只要做下面一部,把圖片輸出就可以了。
//生成圖片
imagepng($im, "test.png");
imagedestroy($im);
二、插圖圓角處理:
和上面背景圓角處理完全相同的思路:就是對插圖的直角進行覆蓋,我就不多說了,下面附上插圖圓角處理的的代碼。
//這里我們吧準備好的插圖畫到背景圖上,此時還是直角的
$filename="img/test_1.png"http://圖片資源目錄
$img = imagecreatefrompng($filename);
//第一個參數(shù)是上面已經用過的大的背景圖,也就我們的畫板,
//第二個參數(shù):上面這個目錄拿到的capy用的資源文件了
//第三個單數(shù)距離大卡片左邊的距離
//第三個單數(shù)距離大卡片上邊的距離
//第三第四是資源圖片開始拷貝的位置,這里我是從左上角開始copy的,所以是0和0;
//第五第六個參數(shù)是圖片拷過去的大小
imagecopy($im, $img, 100, $y, 0, 0, 560, 288);
//畫圓角
$lt_corner = get_lt_rounder_corner($radius, 0xef, 0xef, 0xe1);
//圓角的背景色
myradus($im, 100, $y, $lt_corner, $radius, 288, 560);
上面是調用的方法,這里的get_lt_rounder_corner 是一個自定義的函數(shù),上面背景處理中已經列出該函數(shù)的具體實現(xiàn),這里不再重復,下面給出myradus函數(shù)的具體實現(xiàn):
/**
* @param $im 大的背景圖,也是我們的畫板
* @param $lt_corner 我們畫的圓角
* @param $radius 圓角的程度
* @param $image_h 圖片的高
* @param $image_w 圖片的寬
*/
function myradus($im, $lift, $top, $lt_corner, $radius, $image_h, $image_w)
{
/// lt(左上角)
imagecopymerge($im, $lt_corner, $lift, $top, 0, 0, $radius, $radius, 100);
// lb(左下角)
$lb_corner = imagerotate($lt_corner, 90, 0);
imagecopymerge($im, $lb_corner, $lift, $image_h - $radius + $top, 0, 0, $radius, $radius, 100);
// rb(右上角)
$rb_corner = imagerotate($lt_corner, 180, 0);
imagecopymerge($im, $rb_corner, $image_w + $lift - $radius, $image_h + $top - $radius, 0, 0, $radius, $radius, 100);
// rt(右下角)
$rt_corner = imagerotate($lt_corner, 270, 0);
imagecopymerge($im, $rt_corner, $image_w - $radius + $lift, $top, 0, 0, $radius, $radius, 100);
}
是不是覺得下面這個代碼已經寫過了呢?是的,上面有一樣的代碼。
這樣我們就實現(xiàn)的背景的圓角處理,也實現(xiàn)了圖片的圓角處理。
|