Image special effects in PHP and their implementation methods

WBOY
Release: 2023-06-22 13:58:01
Original
802 people have browsed it

In website development, image special effects can increase the beauty of the page, attract users' attention, and provide users with a better experience. As a powerful back-end language, PHP also provides many methods to achieve image special effects. This article will introduce commonly used image effects in PHP and their implementation methods.

  1. Scale the image

Scale the image is one of the common methods to achieve responsive design of the website. The imagecopyresampled() function is provided in PHP to complete the operation of scaling images. The prototype of this function is as follows:

bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

Among them, $dst_image is the target image resource, $src_image is the source image resource, $dst_x and $dst_y are the upper left corner positions of the target image, $src_x and $scr_y are the upper left corner of the source image Angular position, $dst_w and $dst_h are the width and height of the target image, $src_w and $src_h are the width and height of the source image.

By adjusting the values of $dst_w and $dst_h, you can achieve image scaling. For example, to reduce an image to 50% of its size, the code is as follows:

$src_img = imagecreatefromjpeg('test.jpg'); $dst_img = imagecreatetruecolor(imagesx($src_img) / 2, imagesy($src_img) / 2); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, imagesx($dst_img), imagesy($dst_img), imagesx($src_img), imagesy($src_img)); imagejpeg($dst_img, 'test_resized.jpg', 90); imagedestroy($src_img); imagedestroy($dst_img);
Copy after login
  1. Crop image

Crop an image means to remove a certain part of the image while retaining it. The rest of the operation. In PHP, you can use the imagecrop() function to complete the image cropping operation. The prototype of this function is as follows:

resource imagecrop (resource $image, array $rect)

Among them, $image is the image resource to be cropped, $rect is an array representing the cropping area, including The four elements are the x coordinate of the upper left corner, the y coordinate of the upper left corner, the width of the cropping area, and the height of the cropping area.

For example, if you want to crop an image into a square and use the center of the image as the basis for cropping, the code is as follows:

$src_img = imagecreatefromjpeg('test.jpg'); $src_w = imagesx($src_img); $src_h = imagesy($src_img); $dst_img = imagecrop($src_img, [ $src_w > $src_h ? ($src_w - $src_h) / 2 : 0, $src_w > $src_h ? 0 : ($src_h - $src_w) / 2, min($src_w, $src_h), min($src_w, $src_h) ]); imagejpeg($dst_img, 'test_cropped.jpg', 90); imagedestroy($src_img); imagedestroy($dst_img);
Copy after login
  1. Image rotation

Image rotation can change the direction and angle of the image to suit different needs. In PHP, you can use the imagerotate() function to complete image rotation. The prototype of this function is as follows:

resource imagerotate ( resource $image , float $angle , int $bgd_color [, int $ignore_transparent = 0 ] )

where $image is the image to be rotated Resource, $angle is the angle of rotation, $bgd_color is the background color, $ignore_transparent indicates whether to ignore transparent colors.

For example, to rotate an image 90 degrees counterclockwise, the code is as follows:

$src_img = imagecreatefromjpeg('test.jpg'); $dst_img = imagerotate($src_img, 90, 0); imagejpeg($dst_img, 'test_rotated.jpg', 90); imagedestroy($src_img); imagedestroy($dst_img);
Copy after login
  1. Image watermark

Image watermark refers to the mark on the image Add some text or images to indicate ownership of the image, etc. In PHP, you can use the imagestring() function or imagecopy() function to add a watermark. Among them, the imagestring() function can be used to add text watermarks. The specific usage is as follows:

bool imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )

Among them, $image is the image resource to be added with watermark, $font is the font, $x and $y are the positions of the text in the image, $string is the string to be displayed, and $color is the color of the text.

For example, to add a text watermark to the upper left corner of an image, the code is as follows:

$src_img = imagecreatefromjpeg('test.jpg'); imagestring($src_img, 5, 10, 10, 'Watermark', 0xFFFFFFFF); imagejpeg($src_img, 'test_watermarked.jpg', 90); imagedestroy($src_img);
Copy after login

The above are some methods of implementing image special effects in PHP. Through these methods, you can add more visual effects to the web page, make the web page more lively and interesting, and better meet the needs of users.

The above is the detailed content of Image special effects in PHP and their implementation methods. 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
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!