Home  >  Article  >  Backend Development  >  Sample code sharing for PHP to implement the function of randomly generating watermark images

Sample code sharing for PHP to implement the function of randomly generating watermark images

黄舟
黄舟Original
2017-03-23 09:13:441931browse

This article mainly introduces the relevant information of PHP to generate random watermark images for everyone. It has certain reference value. Interested friends can refer to it

Based on PHP GD graphics library, generate a picture yourself. Only for those who are new to the GD library and can learn from examples.

1. Requirements

The layout of the website uses a style similar to the MOOC course list. Each course is a banner picture, picture Below is the title and introduction. Because there are a large number of courses, there are no special banners designed for all courses, so you need to generate images yourself according to certain rules (I originally planned to use p layout to solve the problem, but p+img is in Responsive Layout Not very easy to control).

Generated renderings:

2. Tools & Materials

1. Open PHP GD graphics library extension
2. Prepare multiple small watermark images
3. Get the background color RGB value of the pre-generated image

3. Code

The process of generating images is detailed in the code.

class GenerateRandomImage
{
  /** @var integer 图片宽度 */
  public $imgWidth = 272;
  /** @var integer 图片高度 */
  public $imgHeight = 162;
  /** @var 根据type不同来生成不同的背景颜色,目前留个type分别为蓝色、紫色、黄色、绿色、灰色、土黄色 */
  public $type = '';
  /** @var 图片上要显示的文字 */
  public $text = '';
  /** @var integer 图片上文字的字体大小 */
  public $fontSize = 16;

  public function construct($type, $text)
  {
    $this->type = $type;
    $this->text = $text;
  }
  /**
   * 创建生成随机图片
   * @author bignerd
   * @since 2017-03-21T14:49:41+0800
   */
  public function createImg()
  {
    /** @var 创建一个指定图片大小的空调色板
    $image = imagecreate($this->imgWidth, $this->imgHeight);
    $rgb  = $this->getBackground($this->type);
    /** @var 为图片创建一个背景色 */
    $backgroundColor = imagecolorallocate($image, $rgb['r'], $rgb['g'], $rgb['b']);
    /** @var 创建文字白色字体 */
    $textColor = imagecolorallocate($image, 255, 255, 255);
    /** @var 字体文件路径 */
    $font = $_SERVER['DOCUMENT_ROOT'].'/public/font/simhei.ttf';

    $x = 18;//文字起始位置x坐标
    $y = 50;//文字起始位置y坐标
    /** 文字写入图片 */
    $angle = 0;//角度0
    imagettftext($image, $this->fontSize, $angle, $x, $y, $textColor, $font, $this->text);
    /** @var 水印图片路径 **/
    $waterImgPath = $this->randWaterImage();
    /** @var 获取图片信息,返回值$waterInfo[2] 为图片类型常量 */
    $waterInfo  = getimagesize($waterImgPath);
    /** @var 将图片类型常量转换为真正的类型,如png */
    $waterType  = image_type_to_extension($waterInfo[2], false);//获取文件类型

    $createImageFunc = 'imagecreatefrom'.$waterType;
    /** @var 创建一个水印图片的副本 $createImageFunc 为根据图片类型来动态生成预调用的创建图片函数*/
    $mask = $createImageFunc($waterImgPath);
    $posX = $this->imgWidth - $waterInfo[0];//水印图片,在目标图片中的位置的x坐标
    $posY = $this->imgHeight - $waterInfo[1];//水印图片,在目标图片中的位置的y坐标
    /** http请求响应类型设置为 image/png 以便直接显示为图片 */
    header("Content-Type:image/png");
    /** 水印图片复制到创建的image */
    imagecopy($image, $mask, $posX, $posY, 0, 0, $waterInfo[0], $waterInfo[1]);
    imagepng($image);//输入图片到浏览器或者文件
    imagedestroy($image);//销毁图片
  } 
  /**
   * 图片背景颜色的rgb值
   * @author bignerd
   * @since 2017-03-21T14:50:16+0800
   */
  public function getBackground()
  {
    $background = [
      '1'=>['r'=>0, 'g'=>160,'b'=>233],
      '2'=>['r'=>198,'g'=>0, 'b'=>110],
      '3'=>['r'=>237,'g'=>109,'b'=>0],
      '4'=>['r'=>33, 'g'=>148,'b'=>75],
      '5'=>['r'=>63, 'g'=>58, 'b'=>57],      
      '6'=>['r'=>202,'g'=>162,'b'=>101],
    ];
    return $background[$this->type];
  }
  /**
   * 随机水印图片路径
   * @author bignerd
   * @since 2017-03-21T14:51:00+0800
   * @return 路径
   */
  public function randWaterImage()
  {
    $folder = [
      '1'=>'product','2'=>'team','3'=>'architecture','4'=>'developer','5'=>'test','6'=>'engineer'
    ];
    $targetFolder = $_SERVER['DOCUMENT_ROOT'].'/public/images/role/'.$folder[$this->type].'/'.rand(1,38).'.png';
    return $targetFolder;
  }
}

$image = new GenerateRandomImage(1,"扛得住的MySql数据架构");
$image->createImg();

So we can use b4485fdabe9c58439955263af7e4c137 to display the image directly on the page.

Note: I encountered a problem during the process: if the watermark image is a transparent png image, when you copy the watermark image to image, it will appear as The white background cannot be transparently blended with the image background we set, so the same color processing needs to be done for random watermark images.

4. Summary

This small example uses simple steps to generate an image, which can be displayed directly in the browser, or it can be given to imagepng Add the second parameter, which is the path, to save the image. Therefore, by learning several methods in the GD library in the examples, you can create pictures, add text watermarks, or image watermarks to pictures.

The above is the detailed content of Sample code sharing for PHP to implement the function of randomly generating watermark images. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn