thinkphp图像的裁剪、缩放、加水印

ThinkPHP 图片处理函数,需要文字水印字体,可在windows下 控制面板 > 大图标(右上角) > 字体 找到需要的字体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 图像的裁剪、缩放、加水印
* @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 ThinkImage();
    $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', ThinkImage::IMAGE_WATER_SOUTHEAST,-10);
    }
    // 未操作则不保存
    if($do){
        $image->save($path);
    }
}
原文地址:https://www.cnblogs.com/chinalorin/p/5855467.html