PHP將圖片處理成圓角

文章摘要:

上一篇文章,我說了關於php把文字畫在圖片上的換行方法,這篇說說項目中圖片圓角的處理

我們可能在很多項目中,需要對圖片進行圓角處理,例如HTML5中,例如Android中;

這裏我們說說用PHP對圖片進行圓角處理的方法;

圓角處理的方法有很多,這裏我就只說說我的項目中是怎麼對處理圓角的;

效果圖如下:

樂萌漢字卡片

如圖可見,不論是插圖,還是文字這背景,我們都做了圓角處理,下面我看看在PHP中,我們是怎麼實現的吧。

這裏我先說說實現的思路,我們是先畫出直角的圖,然後再用一個圓角去覆蓋在直角上面,這樣看起來就是一個圓角了。

一、背景圖圓角處理

方法調用:
//整個圖,也就是白色背景
$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;//距離白色大背景頂端的距離

//這裏調用函數,繪製淡色的圓角背景,
imagebackgroundmycard($im, $dst_x, $y, $image_width, $image_height, $radius);

以上是調用函數的說明,下面我們給出函數方法:

方法實現:

/**
 * 畫一個帶圓角的背景圖
 * @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);
}

上面函數方法依賴的函數:


/** 畫圓角
 * @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)
{
    // 創建一個正方形的圖像
    $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"//圖片資源目錄
$img = imagecreatefrompng($filename);
//第一個參數是上面已經用過的大的背景圖,也就我們的畫板,
//第二個參數:上面這個目錄拿到的capy用的資源文件了
//第三個單數距離大卡片左邊的距離
//第三個單數距離大卡片上邊的距離
//第三第四是資源圖片開始拷貝的位置,這裏我是從左上角開始copy的,所以是0和0;
//第五第六個參數是圖片拷過去的大小
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 是一個自定義的函數,上面背景處理中已經列出該函數的具體實現,這裏不再重複,下面給出myradus函數的具體實現:

/**
 * @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);
}

是不是覺得下面這個代碼已經寫過了呢?是的,上面有一樣的代碼。

這樣我們就實現的背景的圓角處理,也實現了圖片的圓角處理。

發佈了27 篇原創文章 · 獲贊 7 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章