欢迎访问宙启技术站
智能推送

如何使用PHP函数库实现图像处理?

发布时间:2023-06-16 16:30:15

PHP函数库提供了许多用于图像处理的函数,可以在图像上进行各种操作,例如调整大小、旋转、剪裁、缩放、添加水印、转换格式等。下面详细介绍如何使用PHP函数库实现图像处理。

1. 图像处理函数库

PHP函数库提供了多种图像处理函数,例如:

- imagecreate():创建一个空白图像资源

- imagecreatetruecolor():创建一个真彩色图像资源

- imagecopyresampled():复制并变换图像大小

- imagecopy():拷贝部分图像并调整大小

- imagefilledrectangle():绘制填充矩形

- imagecolorallocate():分配一个颜色

- imagestring():在图像上绘制一个字符串

- imagerotate():旋转图像

等等。

2. 图像处理实例

下面以一个图像处理实例介绍如何使用PHP函数库,实现图像按比例缩放和添加水印的功能。

(1)原图

首先,准备一个原始图片(如下图所示),我们将利用PHP函数库对其进行处理,生成新的缩放和加水印后的图片。

![image1](https://raw.githubusercontent.com/jiandandaoxingfu/php-article-image/main/image1.jpg)

(2)按比例缩放

使用的函数为imagecopyresampled(),具体代码如下:

// 按比例缩放图片
function resizeImage($file_path, $new_width, $new_height, $ratio = true) {
    // 获取原图信息
    list($width, $height, $type) = getimagesize($file_path);
    // 如果不按比例缩放,则不保持比例
    if (!$ratio) {
        $new_width = $new_width ? $new_width : $width;
        $new_height = $new_height ? $new_height : $height;
    }
    // 计算新大小
    if ($width > $new_width || $height > $new_height) {
        $scale = $width / $height;
        if ($new_width / $new_height > $scale) {
            $new_width = $new_height * $scale;
        } else {
            $new_height = $new_width / $scale;
        }
    } else {
        $new_width = $width;
        $new_height = $height;
    }
    // 创建新的空画布
    $new_image = imagecreatetruecolor($new_width, $new_height);
    switch ($type) {
        case 1: // GIF
            $source = imagecreatefromgif($file_path);
            imagecopyresampled($new_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagegif($new_image, $file_path);
            break;
        case 2: // JPEG
            $source = imagecreatefromjpeg($file_path);
            imagecopyresampled($new_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagejpeg($new_image, $file_path);
            break;
        case 3: // PNG
            $source = imagecreatefrompng($file_path);
            imagecopyresampled($new_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            imagepng($new_image, $file_path);
            break;
    }
    // 释放资源
    imagedestroy($new_image);
    imagedestroy($source);
}

使用方法为:

// 按比例缩放图片
resizeImage('image1.jpg', 400, 0);

运行后,生成的缩放图片如下图所示:

![image2](https://raw.githubusercontent.com/jiandandaoxingfu/php-article-image/main/image2.jpg)

(3)添加水印

使用的函数为imagestring(),具体代码如下:

// 添加文字水印
function addTextWatermark($file_path, $text, $font_size = 16) {
    // 获取原图信息
    list($width, $height, $type) = getimagesize($file_path);
    switch ($type) {
        case 1: // GIF
            $image = imagecreatefromgif($file_path);
            break;
        case 2: // JPEG
            $image = imagecreatefromjpeg($file_path);
            break;
        case 3: // PNG
            $image = imagecreatefrompng($file_path);
            break;
    }
    // 添加水印
    $color = imagecolorallocate($image, 255, 0, 0);
    imagestring($image, $font_size, 10, $height - 30, $text, $color);
    // 输出并保存
    switch ($type) {
        case 1: // GIF
            imagegif($image, $file_path);
            break;
        case 2: // JPEG
            imagejpeg($image, $file_path);
            break;
        case 3: // PNG
            imagepng($image, $file_path);
            break;
    }
    // 释放资源
    imagedestroy($image);
}

使用方法为:

// 添加文字水印
addTextWatermark('image1.jpg', 'watermark');

运行后,生成的加水印图片如下图所示:

![image3](https://raw.githubusercontent.com/jiandandaoxingfu/php-article-image/main/image3.jpg)

以上是使用PHP函数库实现图像处理的基本方法,结合具体业务需求可以扩展实现更多的图像处理功能。