Summary and application of PHP image processing functions

WBOY
Release: 2023-06-16 06:36:01
Original
746 people have browsed it

PHP is a programming language widely used in Web development, where image processing is a common task. PHP provides a wealth of image processing functions that can easily process images. This article will give you an overview of the usage and application scenarios of PHP image processing functions.

  1. Get image information

In PHP, you can get the basic information of the image through the getimagesize function, including the width, height, type and file size of the image, etc. The following is a sample code:

<?php
// 图片路径
$imgPath = 'example.jpg';

// 获取图片信息
$imgInfo = getimagesize($imgPath);

// 输出图片信息
echo 'Width: ' . $imgInfo[0] . 'px<br>';
echo 'Height: ' . $imgInfo[1] . 'px<br>';
echo 'Type: ' . $imgInfo['mime'] . '<br>';
echo 'Size: ' . filesize($imgPath) . 'bytes<br>';
?>
Copy after login
  1. Resize the image

In PHP, you can use the imagecreate function to create a blank image object, and then use the imagecopyresampled function to copy the original image Scale to specified size. The following is a sample code:

<?php
// 原始图片路径
$srcPath = 'example.jpg';

// 缩放后图片路径
$dstPath = 'example_resized.jpg';

// 目标宽度和高度
$dstWidth = 200;
$dstHeight = 150;

// 创建空白图片对象
$dstImg = imagecreatetruecolor($dstWidth, $dstHeight);

// 加载原始图片
$srcImg = imagecreatefromjpeg($srcPath);

// 缩放图片
imagecopyresampled($dstImg, $srcImg, 0, 0, 0, 0, $dstWidth, $dstHeight, imagesx($srcImg), imagesy($srcImg));

// 保存缩放后图片
imagejpeg($dstImg, $dstPath);

// 释放图片资源
imagedestroy($dstImg);
imagedestroy($srcImg);
?>
Copy after login
  1. Image cropping

In PHP, use the imagecopy function to copy part of an image to another image. The following is a sample code:

<?php
// 原始图片路径
$srcPath = 'example.jpg';

// 裁剪后图片路径
$dstPath = 'example_cropped.jpg';

// 目标宽度和高度
$dstWidth = 200;
$dstHeight = 150;

// 创建空白图片对象
$dstImg = imagecreatetruecolor($dstWidth, $dstHeight);

// 加载原始图片
$srcImg = imagecreatefromjpeg($srcPath);

// 计算裁剪位置
$srcX = (imagesx($srcImg) - $dstWidth) / 2;
$srcY = (imagesy($srcImg) - $dstHeight) / 2;

// 裁剪图片
imagecopy($dstImg, $srcImg, 0, 0, $srcX, $srcY, $dstWidth, $dstHeight);

// 保存裁剪后图片
imagejpeg($dstImg, $dstPath);

// 释放图片资源
imagedestroy($dstImg);
imagedestroy($srcImg);
?>
Copy after login
  1. Adding watermarks to images

In PHP, you can use the imagestring function to add text watermarks to images, or you can use the imagecopy function to add text watermarks to images. Picture watermark is added to the picture. The following is a sample code:

<?php
// 原始图片路径
$srcPath = 'example.jpg';

// 水印图片路径
$watermarkPath = 'watermark.png';

// 输出图片路径
$dstPath = 'example_watermarked.jpg';

// 加载原始图片和水印图片
$srcImg = imagecreatefromjpeg($srcPath);
$watermarkImg = imagecreatefrompng($watermarkPath);

// 计算水印位置
$x = imagesx($srcImg) - imagesx($watermarkImg) - 10;
$y = imagesy($srcImg) - imagesy($watermarkImg) - 10;

// 添加水印
imagecopy($srcImg, $watermarkImg, $x, $y, 0, 0, imagesx($watermarkImg), imagesy($watermarkImg));

// 输出水印图片
imagejpeg($srcImg, $dstPath);

// 释放图片资源
imagedestroy($srcImg);
imagedestroy($watermarkImg);
?>
Copy after login
  1. Image Filter

In PHP, you can use the imagefilter function to add various filter effects, including edge detection, relief, and blur etc. The following is a sample code:

<?php
// 原始图片路径
$srcPath = 'example.jpg';

// 滤镜后图片路径
$dstPath = 'example_filtered.jpg';

// 加载原始图片
$srcImg = imagecreatefromjpeg($srcPath);

// 添加滤镜
imagefilter($srcImg, IMG_FILTER_EDGEDETECT);

// 输出滤镜后图片
imagejpeg($srcImg, $dstPath);

// 释放图片资源
imagedestroy($srcImg);
?>
Copy after login

In summary, PHP provides a wealth of image processing functions that can help us process images conveniently. In practical applications, different image processing functions can be selected according to specific needs to achieve the best results.

The above is the detailed content of Summary and application of PHP image processing functions. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!