How to use PHP functions to implement image processing?

王林
Release: 2023-07-24 19:30:01
Original
1508 people have browsed it

How to use PHP functions to implement image processing?

Abstract: Image processing is a very common task in modern web applications. The PHP language provides a wealth of functions and extensions to implement image processing functions. This article will introduce how to use PHP functions to implement common image processing operations, including scaling, cropping, rotating, watermarking, etc.

1. Basic use of image processing functions

In PHP, you can use imagecreatefromjpeg(), imagecreatefrompng(), imagecreatefromgif() and other functions to create image resources. Select the corresponding function according to the format of the original image. For example:

$img = imagecreatefromjpeg('original.jpg');
Copy after login

Then, you can use the imagecopyresized() function to perform image scaling operations, which accepts parameters such as source image resources, target image resources, and the width and height of the target image. For example:

$newWidth = 200;
$newHeight = 150;
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($newImage, $img, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($img), imagesy($img));
Copy after login

2. Crop the image

If you need to crop the image, you can use the imagecopy() function to achieve it. This function accepts parameters such as source image resources, target image resources, and starting point coordinates of the target image. For example:

$newWidth = 200;
$newHeight = 150;
$srcX = 50;
$srcY = 50;
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopy($newImage, $img, 0, 0, $srcX, $srcY, $newWidth, $newHeight);
Copy after login

3. Rotate the image

To rotate the image, you can use the imagerotate() function. This function accepts parameters such as source image resources and rotation angle. For example:

$degree = 45;
$newImage = imagerotate($img, $degree, 0);
Copy after login

4. Add watermark

If you need to add a watermark to an image, you can use the imagecopy() function to achieve it. First, use the imagefttext() function to write the watermark text into the target image. This function accepts parameters such as target image resources, watermark text size, and watermark text angle. For example:

$fontSize = 20;
$textAngle = 0;
$textX = 10;
$textY = 10;
$textColor = imagecolorallocate($newImage, 255, 255, 255); // 水印文字颜色
$fontPath = 'font.ttf'; // 字体文件路径
imagefttext($newImage, $fontSize, $textAngle, $textX, $textY, $textColor, $fontPath, 'Watermark');
Copy after login

Then, use the imagecopy() function to merge the original image and the target image. For example:

$srcX = 0;
$srcY = 0;
$dstX = 0;
$dstY = 0;
$dstWidth = imagesx($newImage);
$dstHeight = imagesy($newImage);
imagecopy($img, $newImage, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight);
Copy after login

5. Save and output images

Finally, you can use imagejpeg(), imagepng(), imagegif() and other functions to save the image in different formats file, or output directly in the browser. For example:

$outputPath = 'output.jpg';
imagejpeg($newImage, $outputPath);
Copy after login

or:

header('Content-Type: image/jpeg');
imagejpeg($newImage);
Copy after login

Conclusion:

Using PHP functions, we can easily implement various image processing operations, such as scaling, cropping, rotating and adding watermarks wait. The functions introduced above are only common image processing operations. In fact, the PHP function library also provides more powerful image processing functions, which developers can choose to use according to actual needs. Mastering the basic usage of these functions will allow us to process images more flexibly and improve user experience when developing network applications.

Reference link:

  • PHP official documentation: https://www.php.net/manual/zh/book.image.php

The above is the detailed content of How to use PHP functions to implement image processing?. For more information, please follow other related articles on the PHP Chinese website!

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!