php GD library usage record

1. Add text watermark to pictures

//$src  图片的绝对路径名称
//$content   水印文字
function imgtag($src,$content){
    
    
    //获取图片信息
    $info = getimagesize($src);
    //获取图片扩展名
    $type = image_type_to_extension($info[2],false);
    //动态的把图片导入内存中
    $fun = "imagecreatefrom{
      
      $type}";
    $image = $fun($src);
    //指定字体颜色 和 透明度
    $col = imagecolorallocatealpha($image,119,119,119,60);
    //指定文字路径,绝对路径
    $font = ROOT_PATH.'/extend/org/verify/zhttfs/1.ttf';
    //给图片添加文字
    $font_x = $info[0]/3;
    $font_y = $info[1]/3;
    //40文字尺寸,-45文字偏转角度,font_x和font_y 文字起始位置的左上角坐标
    imagettftext($image, 40, -45, $font_x, $font_y, $col, $font, $content);

    $func = "image{
      
      $type}";
    //动态的输出图片到浏览器中
    //header('Content-type:'.$info['mime']);
    //$func($image);
    //保存到指定文件
    $func($image,$src);
    //销毁图片
    imagedestroy($image);
}

2. Merge two pictures

$QR = imagecreatefrompng('qrcode.png');   		//目标图象连接资源。
$logo = imagecreatefromjpeg('logo.jpg');   	//源图象连接资源

$QR_width = imagesx($QR);			//二维码图片宽度 ,二维码宽高一致,获取一个即可
$logo_width = imagesx($logo);		//logo图片宽度   ,log宽高一致,获取一个即可
$start = ($QR_width/2)-($logo_width/2);//获取logo放置左上角的坐标点
//方法一
//imagecopymerge($QR,$logo,$start,$start,0,0,$logo_width,$logo_width,100);
//方法二
imagecopyresampled($QR,$logo,$start,$start,0,0,50,50,$logo_width,$logo_width);

imagepng($QR, 'new_qrcode.png');
imagedestroy($QR);
imagedestroy($logo);

Both imagecopymerge and imagecopyresampled can combine two pictures together


imagecopymerge is more suitable for watermarking pictures, because the transparency of the source pictures can be adjusted.
Parameter description, in order:

  • The target image resource can be understood as the big picture as the background. It can be an image resource read from a file, or a canvas created with imagecreatetruecolor()
  • Source image resource, small image, same as above
  • The X-axis position of the coordinate point of the upper left corner of the source image on the target image
  • The Y-axis position of the coordinate point of the upper left corner of the source image on the target image
  • The X-axis coordinate of the starting point of the source image interception
  • The Y coordinate of the starting point of the source image interception. This means that a part of the source image can be intercepted and merged onto the larger image, or all of it can be merged onto the larger image. When these two parameters are 0, 0, it means to start the interception from the vertex position of the upper left corner of the source image.
  • Source image x-axis interception length
  • The Y-axis interception length of the source image. When the start position of the interception is 0,0, the length and width of the interception are consistent with the length and width of the source image, which means that the source image is merged into the target image.
  • Transparency 0-100, 100 means completely merged, the source image is opaque; 0 means, the source image is completely transparent

imagecopyresampled is more suitable for scenes where the source image size is not uniform and needs to be compressed and merged.
The parameters are basically similar to the above parameters, the difference is that there are two more parameters, used to specify the range of the source image on the target image.
Parameter description, in order:

  • Target image resource
  • Source image resource
  • The X-axis position of the coordinate point of the upper left corner of the source image on the target image
  • The Y-axis position of the coordinate point of the upper left corner of the source image on the target image
  • The X-axis coordinate of the starting point of the source image interception
  • Y-axis coordinate of the starting point of the source image interception
  • The target image allows the width of the source image
  • The target image allows the height of the source image. These two parameters, in conjunction with parameters 3 and 4, specify the position and size of the source image on the target image. If it exceeds this size, it will be compressed, and if it is not enough, it will be filled with black.
  • Source image X-axis interception length
  • Y-axis interception length of source image

The following is an illustration of the official document. On the left is the source image, on the right is the target image, put the big circle in the small circle
Insert picture description here

3. Generate a circular picture

When this method generates a circular image, some black images may be a little abnormal.
If the size of the circular image is small, do not generate a small image. If the image is too small, the round edges will be jagged. Set the width and height of the image when using the image. Can

/**
 * 生成圆形图片
 * @param $imgpath  图片地址/支持微信、QQ头像等没有后缀的网络图
 * @param $saveName string 保存文件名,默认空。
 * @return resource 返回图片资源或保存文件
 */
function yuan_img($imgpath,$saveName = '') {
    
    
    $src_img = imagecreatefromstring(file_get_contents($imgpath));
    $w = imagesx($src_img);$h = imagesy($src_img);
    $w = $h = min($w, $h);

    $img = imagecreatetruecolor($w, $h);
    //这一句一定要有
    imagesavealpha($img, true);
    //拾取一个完全透明的颜色,最后一个参数127为全透明
    $bg = imagecolorallocatealpha($img, 0, 0, 0, 127);
    imagefill($img, 0, 0, $bg);
    $r   = $w / 2; //圆半径
    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);
            }
        }
    }

    //返回资源
    if(!$saveName) return $img;
    //输出图片到文件
    imagepng ($img,$saveName);
    //释放空间
    imagedestroy($src_img);
    imagedestroy($img);
}

Guess you like

Origin blog.csdn.net/u012830303/article/details/106281154