Home > Backend Development > PHP Tutorial > Detailed explanation of how to implement image cropping, scaling, and watermarking using the thinkPHP framework

Detailed explanation of how to implement image cropping, scaling, and watermarking using the thinkPHP framework

墨辰丷
Release: 2023-03-27 20:50:01
Original
1896 people have browsed it

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);
  }
}
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

phpCropDetailed explanation of the steps for fixing the image to a fixed size

php pictureCropExplanation on using thumbnails

##PHP without affecting the shape of the pictureCrop

The above is the detailed content of Detailed explanation of how to implement image cropping, scaling, and watermarking using the thinkPHP framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template