使用gd库实现php服务端图片裁剪和生成缩略图功能分享_PHP
裁剪示例:
最终裁剪成的图片:
其中虚线框内就是要裁剪出来的图片,最终保存成100宽的图片。代码如下:
复制代码 代码如下:
$src_path = '1.jpg';
//创建源图的实例
$src = imagecreatefromstring(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('Content-Type: image/jpeg');
imagejpeg($new_image);
imagedestroy($src);
imagedestroy($new_image);
其实如果坐标为(0,0),裁剪区域的宽高和源图的宽高一致,那么就是生成缩略图的功能了。
总结
这里只列出了php裁剪图片的示例,属于服务端的功能。如果客户端有需要,推荐一个jquery的插件imageAreaSelect,兼容性非常不错。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. What is the GD library? The GD library is a set of library functions for creating and processing various image formats. It is one of the most commonly used image processing libraries in PHP. 2. Install the GD library Install the GD library under CentOS/RedHat 1. Install PHP’s GD extension library yuminstallphp-gd 2. Restart the web server servicehttpdrestart 3. Check the GD library version supported by PHP php-i | grep-igd in Ubunt

How to implement image cropping function in JavaScript? With the rapid development of mobile Internet, image cropping functions have become more and more common in many websites and mobile applications. As a front-end development language, JavaScript provides many libraries and technologies to implement image cropping functions. This article will introduce how to use JavaScript to implement the image cropping function and provide specific code examples. 1. HTML structure design First, we need to create a container in the page to display pictures and cropping boxes

How to use Vue and ElementPlus to implement image cropping and rotation functions Introduction: As web applications have higher and higher requirements for user experience, image processing has become a part that cannot be ignored. As a popular JavaScript framework, Vue, combined with ElementPlus, an excellent UI component library, provides us with a wealth of tools and functions to quickly and easily crop and rotate images. This article will be based on Vue and ElementPlus to introduce you to

The solution to the problem that PHP cannot open the gd library: 1. Find and open the php.ini configuration file; 2. Remove the comment symbol ";" in front of "extension_dir"; 3. Change its value to the absolute path of the ext folder. .

Vue.js is a popular JavaScript front-end framework. The latest version - Vue3 has been launched. The new version of Vue has improved performance, size and development experience, and is welcomed by more and more developers. This article will introduce how to use Vue3 to make a simple image cropper. First, we need to create a Vue project and install the required plugins. You can use VueCLI to create a project, or you can build it manually. Here we take the method of using VueCLI as an example: #

How to implement image cropping and uploading function in JavaScript? In web development, we often encounter the need for users to upload and crop images, such as avatar uploading, image editing, etc. JavaScript provides a wealth of APIs and functions that can help us implement such functions. This article will introduce how to use JavaScript to implement image cropping and uploading functions, and provide specific code examples. First, we need to add an element for displaying pictures in the HTML file, such as an

Introduction to the method of realizing rounded corner images using PHP and GD libraries. In web design, sometimes it is necessary to use rounded corner images to beautify the appearance of the page. This article will introduce how to use PHP and GD library to implement rounded images. The GD library is one of the PHP extension libraries and provides a series of functions for processing images. By using the GD library, we can crop, resize, add filters, etc. to images. To achieve rounded images, we need to use some functions in the GD library for image processing. The following are the specific steps to achieve rounded corner images.

How to implement image rotation using PHP and GD libraries Image rotation is a common image processing requirement. By rotating images, you can achieve some special effects or meet user needs. In PHP, you can use the GD library to implement the image rotation function. This article will introduce how to use PHP and the GD library to implement image rotation, with code examples. First, make sure your PHP environment has the GD library extension installed. Enter php-m on the command line to check if there is a gd module. If not, you need to install it first. Here is a simple
