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

ThinkPHP framework implements image cropping, scaling, and watermarking methods

Jun 07, 2018 am 11:40 AM
thinkphp add watermark image Zoom Crop

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

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

Safari zoom issue on iPhone: Here's the fix Safari zoom issue on iPhone: Here's the fix Apr 20, 2024 am 08:08 AM

If you don't have control over the zoom level in Safari, getting things done can be tricky. So if Safari looks zoomed out, that might be a problem for you. Here are a few ways you can fix this minor zoom issue in Safari. 1. Cursor magnification: Select "Display" > "Cursor magnification" in the Safari menu bar. This will make the cursor more visible on the screen, making it easier to control. 2. Move the mouse: This may sound simple, but sometimes just moving the mouse to another location on the screen may automatically return it to normal size. 3. Use Keyboard Shortcuts Fix 1 – Reset Zoom Level You can control the zoom level directly from the Safari browser. Step 1 – When you are in Safari

How to zoom pages side by side in word How to zoom pages side by side in word Mar 19, 2024 pm 07:19 PM

When we use word documents to edit files, sometimes there are many pages. We want to display them side by side and check the overall effect. However, because we don’t know how to operate, we often need to scroll for a long time to view page by page. I don’t know if you have ever encountered a similar situation. In fact, we can easily solve it at this time as long as we learn how to set the word zoom pages side by side. Below, let’s take a look and learn together. First, we create and open a new page in the Word document, and then enter some simple content to make it easier to distinguish. 2. For example, if we want to realize word zoom and side-by-side display, we need to find [View] in the menu bar, and then select [Multiple Pages] in the view tool options, as shown in the figure below: 3. Find [Multiple Pages] and click,

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

How to crop crooked pictures in photoshop? PS crop and tilt photo tutorial How to crop crooked pictures in photoshop? PS crop and tilt photo tutorial Mar 25, 2024 pm 10:07 PM

Some users find that some things in the picture are crooked and cannot be directly selected and cropped. Is there any way to straighten the things in the picture? In fact, this operation is very simple for PS masters. Here, the editor will tell the novice PS users how to crop crooked pictures into straight ones in Photoshop. This method is very easy to operate. I hope it can help everyone. PS tutorial for cropping tilted photos 1. Open Photoshop, move the mouse to the cropping tool on the left, then right-click the mouse and select "Perspective Cropping Tool". 2. Select the picture that needs to be straightened and determine the four points. 3. Then press the Enter key to straighten it successfully. 4. In this way, the things in the photo will be corrected, and

Word document editing tips: Make two pages of content into one Word document editing tips: Make two pages of content into one Mar 25, 2024 pm 06:06 PM

In Microsoft Word documents, you often encounter the situation of merging two pages of content into one page, especially when you need to save paper or when you need to print a double-sided document. Several common methods to achieve this goal will be introduced below. Method 1: Adjust the page margins. First open the Word document and find the "Page Layout" option in the menu bar. After clicking, the page layout settings menu will pop up. Here you can adjust the page margins, including the top, bottom, left, and right margins. Generally speaking, making the top and bottom margins smaller will allow the content to fit within one page. you can taste

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

See all articles