PHP gd library implements server-side image cropping and thumbnail preview functions

php中世界最好的语言
Release: 2023-03-27 07:42:01
Original
1766 people have browsed it

This time I will bring you the php gd library to implement the server-side picturecropping and thumbnailpreview function, and the php gd library to implement the server-side image cropping and thumbnail preview functionNotesWhat are the actual cases? Let’s take a look at them.

Example, cropping images in PHP mainly uses the imagecopyresampled method of the gd library.
Example of cropping a picture:
PHP gd library implements server-side image cropping and thumbnail preview functions

The final cropped picture: PHP gd library implements server-side image cropping and thumbnail preview functions2

The dotted line box is the picture to be cropped, and it is finally saved as 100 wide picture.

<?php
$src_path = &#39;1.jpg&#39;;
//创建源图的实例
$src = imagecreatefrom
string
(
file_get_contents
($src_path));
//裁剪开区域左上角的点的坐标
$x = 100;
$y = 12;
//裁剪区域的宽和高
$width = 200;
$height = 200;
//最终保存成图片的宽和高,和源要等比例,否则会变形
$final_width = 100;
$final_height = round($final_width * $height / $width);
//将裁剪区域复制到新图片上,并根据源和目标的宽高进行缩放或者拉升
$new_image = imagecreatetruecolor($final_width, $final_height);
imagecopyresampled($new_image, $src, 0, 0, $x, $y, $final_width, $final_height, $width, $height);
//输出图片
header(&#39;Content-Type: image/jpeg&#39;);
imagejpeg($new_image);
imagedestroy($src);
imagedestroy($new_image);
Copy after login

In fact, if the coordinates are (0,0) and the width and height of the cropping area are consistent with the width and height of the source image, then it is the function of generating thumbnails.
Summary
Only examples of php cropping images are listed here, which are server-side functions.
If the client needs it, we recommend a jquery plug-in imageAreaSelect, which has very good compatibility.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

php array function shuffle() and array_rand() detailed explanation of the steps to use the random function

php array Find a summary of how to use functions

The above is the detailed content of PHP gd library implements server-side image cropping and thumbnail preview 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