使用 Intervention/image 对 Laravel 项目中的图片进行处理
说明
Intervention/image 是为 Laravel 定制的图片处理工具, 它提供了一套易于表达的方式来创建、编辑图片。
本文章由 The EST Group 成员 @monkey 撰写, 首发地为PHPHub 社区.
Demo 代码请见: https://github.com/zhengjinghua/est-image-demo
Demo
Demo 截图
Demo 运行
请参照文档 如何利用 Homestead 快速运行一个 Laravel 项目 .
文章概览
- 安装;
- 修改配置信息;
- 基础用法;
- 特色功能.
接下来是详细解说.
1. 安装
1). 使用 composer 安装:
composer require intervention/image
上面的命令会
2). 修改 app/config/app.php 添加 ServiceProvider:
// 将下面代码添加到 providers 数组中'providers' => [ // ... Intervention\Image\ImageServiceProvider::class, // ... ],// 将下面代码添加到 aliases 数组中'aliases' => [ // ... 'Image' => Intervention\Image\Facades\Image::class, // ... ],
2. 图片处理库的配置
此扩展包默认使用 PHP 的 GD 库来进行图像处理, 但由于 GD 库对图像的处理效率要稍逊色于 imagemagick 库, 因此这里推荐替换为 imagemagick 库来进行图像处理.
开始之前, 你得先确定本地已经安装好 GD 或 Imagick.
在使用 Intervention Image 的时候, 你只需要给 ImageManager 传一个数组参数就可以完成 GD 和 Imagick 库之间的互相切换.
如下所示:
// 引入 composer autoloadrequire 'vendor/autoload.php';// 导入 Intervention Image Manager Classuse Intervention\Image\ImageManager;// 通过指定 driver 来创建一个 image manager 实例$manager = new ImageManager(array('driver' => 'imagick'));// 最后创建 image 实例$image = $manager->make('public/foo.jpg')->resize(300, 200);
另外你也可以使用 ImageManager 的静态版本, 如下所示:
// 引入 composer autoloadrequire 'vendor/autoload.php';// 导入 Intervention Image Manager Classuse Intervention\Image\ImageManagerStatic as Image;// 通过指定 driver 来创建一个 image manager 实例 (默认使用 gd)Image::configure(array('driver' => 'imagick'));// 最后创建 image 实例$image = Image::make('public/foo.jpg')->resize(300, 200);
生成 config/image.php 配置文件:
php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravel5"
运行上面的命令后, 会在项目中生成 config/image.php 配置文件, 打开此文件并将 driver 修改成 imagick :
return array( 'driver' => 'imagick');
到此, 此拓展包即安装成功 :beers: :beers: :beers:
3. 基础用法
// 修改指定图片的大小$img = Image::make('images/avatar.jpg')->resize(200, 200);// 插入水印, 水印位置在原图片的右下角, 距离下边距 10 像素, 距离右边距 15 像素$img->insert('images/watermark.png', 'bottom-right', 15, 10);// 将处理后的图片重新保存到其他路径$img->save('images/new_avatar.jpg');/* 上面的逻辑可以通过链式表达式搞定 */$img = Image::make('images/avatar.jpg')->resize(200, 200)->insert('images/new_avatar.jpg', 'bottom-right', 15, 10);
4. 特色功能
除上文介绍的基本用法之外, 此扩展包还支持:
- 图片上传功能;
- 图片缓存功能;
- 图片过滤功能: 将图片按照统一规则进行转换;
- 图片动态处理: 根据访问图片的 URL 参数自动调整图片大小
更多的例子请移步 官方文档 参考.
欢迎关注 LaravelTips , 一个专注于为 Laravel 开发者服务, 致力于帮助开发者更好的掌握 Laravel 框架, 提升开发效率的微信公众号.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
