zoukankan      html  css  js  c++  java
  • php GD图片四角圆形处理

     1 <?php
     2  3 /**
     4  * blog:http://www.zhaokeli.com
     5  * 处理四角圆图片
     6  * @param  string  $imgpath 源图片路径
     7  * @param  integer $radius  圆角半径长度默认为15,处理成圆型
     8  * @return [type]           [description]
     9  */
    10 function radius_img($imgpath = './code_png/share.jpg', $radius = 15) {
    11     $ext     = pathinfo($imgpath);
    12     $src_img = null;
    13     switch ($ext['extension']) {
    14         case 'jpg':
    15             $src_img = imagecreatefromjpeg($imgpath);
    16             break;
    17         case 'png':
    18             $src_img = imagecreatefrompng($imgpath);
    19             break;
    20     }
    21     $wh = getimagesize($imgpath);
    22     $w  = $wh[0];
    23     $h  = $wh[1];
    24     // $radius = $radius == 0 ? (min($w, $h) / 2) : $radius;
    25     $img = imagecreatetruecolor($w, $h);
    26     //这一句一定要有
    27     imagesavealpha($img, true);
    28     //拾取一个完全透明的颜色,最后一个参数127为全透明
    29     $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
    30     imagefill($img, 0, 0, $bg);
    31     $r = $radius; //圆 角半径
    32     for ($x = 0; $x < $w; $x++) {
    33         for ($y = 0; $y < $h; $y++) {
    34             $rgbColor = imagecolorat($src_img, $x, $y);
    35             if (($x >= $radius && $x <= ($w - $radius)) || ($y >= $radius && $y <= ($h - $radius))) {
    36                 //不在四角的范围内,直接画
    37                 imagesetpixel($img, $x, $y, $rgbColor);
    38             } else {
    39                 //在四角的范围内选择画
    40                 //上左
    41                 $y_x = $r; //圆心X坐标
    42                 $y_y = $r; //圆心Y坐标
    43                 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
    44                     imagesetpixel($img, $x, $y, $rgbColor);
    45                 }
    46                 //上右
    47                 $y_x = $w - $r; //圆心X坐标
    48                 $y_y = $r; //圆心Y坐标
    49                 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
    50                     imagesetpixel($img, $x, $y, $rgbColor);
    51                 }
    52                 //下左
    53                 $y_x = $r; //圆心X坐标
    54                 $y_y = $h - $r; //圆心Y坐标
    55                 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
    56                     imagesetpixel($img, $x, $y, $rgbColor);
    57                 }
    58                 //下右
    59                 $y_x = $w - $r; //圆心X坐标
    60                 $y_y = $h - $r; //圆心Y坐标
    61                 if (((($x - $y_x) * ($x - $y_x) + ($y - $y_y) * ($y - $y_y)) <= ($r * $r))) {
    62                     imagesetpixel($img, $x, $y, $rgbColor);
    63                 }
    64             }
    65         }
    66     }
    67     return $img;
    68 }
    69 header("content-type:image/png");
    70 $imgg = radius_img();
    71 imagepng($imgg);
    72 imagedestroy($imgg);

    如果本文章已帮助到您!

  • 相关阅读:
    HDU 1394 Minimum Inversion Number
    HDU 4931 Happy Three Friends
    BZOJ 1089 严格n元树 (递推+高精度)
    BZOJ 1088 扫雷Mine (递推)
    BZOJ 3038 上帝造题的七分钟2 (并查集+树状数组)
    BZOJ 3211 花神游历各国 (树状数组+并查集)
    BZOJ 1087 互不侵犯King (位运算)
    BZOJ 1002 轮状病毒 (基尔霍夫矩阵)
    BZOJ 1005 明明的烦恼 (组合数学)
    BZOJ 1058 报表统计 (STL)
  • 原文地址:https://www.cnblogs.com/handle/p/9530982.html
Copyright © 2011-2022 走看看