Home >Backend Development >PHP Tutorial >ThinkPHP framework implements image cropping, scaling, and watermarking methods

ThinkPHP framework implements image cropping, scaling, and watermarking methods

不言
不言Original
2018-06-07 11:40:522200browse

This article mainly introduces the thinkPHP framework's method of cropping, scaling, and adding watermarks to images. It also provides thinkPHP's custom functions for cropping, scaling, and adding watermarks to images in the form of examples. It has certain reference value and is needed. Friends can refer to the following

This article describes how the thinkPHP framework implements image cropping, scaling, and watermarking. Share it with everyone for your reference, the details are as follows:

ThinkPHP image processing function requires text watermark font, you can find the required font in Windows Control Panel>Large Icon (upper right corner)>Font

/**
* 图像的裁剪、缩放、加水印
* @param string $path    路径
* @param int $width   裁剪的宽度/限制的高度或宽度,当有$height值时此值为图片的宽度,否则为限制的宽度或高度
* @param int $height   [可选]裁剪的高度
* @param boolean $water   [可选]是否加水印
* @param int $word    [可选]水印文字
*/
function zoom_image($path,$width = 300,$height = null,$water = null,$word = 'water'){
  $image = new \Think\Image();
  $image->open($path);
  $imgWidth = $image->width();
  $imgHeight = $image->height();
  // 限制尺寸
  if($width and !$height){
    $maxSize = $width;
    // 宽度或高度大于规定尺寸时
    if($imgWidth > $maxSize or $imgHeight > $maxSize){
      $size = image_min_width($imgWidth,$imgHeight,$maxSize);
      $image->thumb($size['width'], $size['height']);
      $do = true;
      $dowater = true;
    }
  // 裁剪固定尺寸
  }else if($width and $height){
    $size = image_min_width($imgWidth,$imgHeight,$width);
    $image->thumb($size['width'], $size['height'])->crop($width, $height);
    $do = true;
    $dowater = true;
  }
  if($dowater and $water and $word){
    $image->text($word,'./Public/images/arial.ttf',20,'#dddddd', \Think\Image::IMAGE_WATER_SOUTHEAST,-10);
  }
  // 未操作则不保存
  if($do){
    $image->save($path);
  }
}

Related recommendations:

How to export excel tables in the YII2 framework

thinkPHP framework implements the method of generating barcodes

The above is the detailed content of ThinkPHP framework implements image cropping, scaling, and watermarking methods. 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