当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

laravel-image-optimizer

Optimize images in your Laravel app
授权协议 MIT License
开发语言 PHP
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 封烈
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Optimize images in your Laravel app

This package is the Laravel 6.0 and up specific integration of spatie/image-optimizer. It can optimize PNGs, JPGs, SVGs and GIFs by running them through a chain of various image optimization tools. The package will automatically detect which optimization binaries are installed on your system and use them.

Here's how you can use it:

use ImageOptimizer;

// the image will be replaced with an optimized version which should be smaller
ImageOptimizer::optimize($pathToImage);

// if you use a second parameter the package will not modify the original
ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage);

You don't like facades you say? No problem! Just resolve a configured instance of Spatie\ImageOptimizer\OptimizerChain out of the container:

app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);

The package also contains a middleware to automatically optimize all images in an request.

Don't use Laravel you say? No problem! Just use the underlying spatie/image-optimizer directly.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-image-optimizer

The package will automatically register itself.

The package uses a bunch of binaries to optimize images. To learn which ones on how to install them, head over to the optimization tools section in the readme of the underlying image-optimizer package. That readme also contains info on what these tools will do to your images.

The package comes with some sane defaults to optimize images. You can modify that configuration by publishing the config file.

php artisan vendor:publish --provider="Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider"

This is the contents of the config/image-optimizer file that will be published:

use Spatie\ImageOptimizer\Optimizers\Svgo;
use Spatie\ImageOptimizer\Optimizers\Optipng;
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
use Spatie\ImageOptimizer\Optimizers\Pngquant;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
use Spatie\ImageOptimizer\Optimizers\Cwebp;

return [
    /**
     * When calling `optimize` the package will automatically determine which optimizers
     * should run for the given image.
     */
    'optimizers' => [

        Jpegoptim::class => [
            '-m85', // set maximum quality to 85%
            '--strip-all',  // this strips out all text information such as comments and EXIF data
            '--all-progressive'  // this will make sure the resulting image is a progressive one
        ],

        Pngquant::class => [
            '--force' // required parameter for this package
        ],

        Optipng::class => [
            '-i0', // this will result in a non-interlaced, progressive scanned image
            '-o2',  // this set the optimization level to two (multiple IDAT compression trials)
            '-quiet' // required parameter for this package
        ],

        Svgo::class => [
            '--disable=cleanupIDs' // disabling because it is know to cause troubles
        ],

        Gifsicle::class => [
            '-b', // required parameter for this package
            '-O3' // this produces the slowest but best results
        ],
        
        Cwebp::class => [
            '-m 6', // for the slowest compression method in order to get the best compression.
            '-pass 10', // for maximizing the amount of analysis pass.
            '-mt', // multithreading for some speed improvements.
            '-q 90', //quality factor that brings the least noticeable changes.
        ],
    ],

    /**
     * The maximum time in seconds each optimizer is allowed to run separately.
     */
    'timeout' => 60,

    /**
     * If set to `true` all output of the optimizer binaries will be appended to the default log.
     * You can also set this to a class that implements `Psr\Log\LoggerInterface`.
     */
    'log_optimizer_activity' => false,
];

If you want to automatically optimize images that get uploaded to your application add the \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class in the http kernel.

// app/Http/Kernel.php
protected $routeMiddleware = [
   ...
   'optimizeImages' => \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class,
];

Usage

You can resolve a configured instance of Spatie\ImageOptimizer\OptimizerChain out of the container:

// the image will be replaced with an optimized version which should be smaller
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);

// if you use a second parameter the package will not modify the original
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage, $pathToOptimizedImage);

Using the facade

use ImageOptimizer;

// the image will be replaced with an optimized version which should be smaller
ImageOptimizer::optimize($pathToImage);

// if you use a second parameter the package will not modify the original
ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage);

You don't like facades you say? No problem! Just resolve a configured instance of Spatie\ImageOptimizer\OptimizerChain out of the container:

app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);

Using the middleware

All images that in requests to routes that use the optimizeImages-middleware will be optimized automatically.

Route::middleware('optimizeImages')->group(function () {
    // all images will be optimized automatically
    Route::post('upload-images', 'UploadController@index');
});

Adding your own optimizers

To learn how to create your own optimizer read the "Writing custom optimizers" section in the readme of the underlying spatie/image-optimizer package.

You can add the fully qualified classname of your optimizer as a key in the optimizers array in the config file.

Example conversions

Here are some example conversions that were made by the optimizer.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Postcardware

You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

The idea of a middleware that optimizes all files in a request is taken from approached/laravel-image-optimizer.

License

The MIT License (MIT). Please see License File for more information.

  • 我们使用Composer在命令行安装最新版本的Intervention Image: composer require intervention/image 安装好Intervention Image后,打开config/app.php,注册如下服务提供者到$providers数组: Intervention\Image\ImageServiceProvider::class 然后添加如下门面到$

  • 安装: 需求: PHP >= 5.4 Fileinfo 扩展 GD库 >= 2.0 Imagick 扩展 >=6.5.7 composer安装: composer require intervention/image laravel配置: 1.编辑 config/app.php $providers 添加 'Intervention\Image\ImageSer

  • Laravel压缩图片 安装 Intervention Image 之前,需要确保 PHP 版本 >=5.4 并且安装了 Fileinfo 扩展,以及 GD 库(>=2.0)或者 Imagick 扩展(>=6.5.7)。 1.通过composer安装Intervention Image composer require intervention/image 2.在config/app.php中,注

  • 说明# Intervention/image 是为 Laravel 定制的图片处理工具, 它提供了一套易于表达的方式来创建、编辑图片。 Demo 代码请见:https://github.com/zhengjinghua/est-image-demo Demo# Demo 截图# Demo 运行# 请参照文档 如何利用 Homestead 快速运行一个 Laravel 项目. 文章概览# 安装; 修

  • Intervention/image 是为 Laravel 定制的图片处理工具, 它提供了一套易于表达的方式来创建、编辑图片。 官方文档http://Intervention\Image\ImageServiceProviderLaravel5 1、安装扩展包 composer require intervention/image 2、生成配置文件 php artisan vendor:publi

  • 最近偶然发现了Laravel可用的图片处理包intervention-image。 文档地址:http://image.intervention.io 安装起来也很简单。 composer require intervention/image 然后到config/app.php的 $providers中添加 Intervention\Image\ImageServiceProvider::cla

  • 前文为PHP安装imagick,现在在laravel 5.5中使用intervention-image  安装: 需求: PHP >= 5.4 Fileinfo 扩展 GD库 >= 2.0 Imagick 扩展 >=6.5.7 composer安装: composer require intervention/image laravel配置: 1.编辑 config/

  • 系统需求 PHP >= 5.3 Fileinfo Extension GD Library (>=2.0) … or … Imagick PHP extension (>=6.5.7) 安装部署 Integration/image 在 composer.json [require] 节增加,之后执行 composer update "intervention/image": "2.0.15" La

  • 系统需求  PHP >= 5.3  Fileinfo Extension  GD Library (>=2.0) … or …  Imagick PHP extension (>=6.5.7) 安装部署 Integration/image 在 composer.json [require] 节增加,之后执行 composer update "intervention/image": "2.0.15

  • 首先是安装: composer require intervention/image 安装他需要你的 php版本大于等于 5.4 还需要有 fileinfo 扩展,你可以通过 phpinfo() 查看扩展是否安装,也可以通过命令: php --ri fileinfo 查看 我使用的是 lnmp 1.4 的一键安装,没有 fileinfo扩展 那么首先安装这个扩展: 1.首先确认你的系统中有 fil

  • form 表单必须添加 enctype="multipart/form-data"创建一个上传图片handle类 :getClientOriginalExtension() )?: 'png'; // $file -> getClientOriginalExtension(); //上传文件的后缀 $filename = $file_prefix . '_' . time() . '_' . st

  • 在安装intervention/image图像处理扩展 报错fileinfo is missing 报错信息如下:   \blog>composer require intervention/image Using version ^2.3 for intervention/image ./composer.json has been updated Loading composer reposi

  • 1.安装 使用Composer在命令行安装最新版本的Intervention Image: composer require intervention/image 2.集成到Laravel 安装好Intervention Image后,打开config/app.php,注册如下服务提供者到$providers数组: Intervention\Image\ImageServiceProvider::

 相关资料
  • 我试图使用命令提示符中的命令“composer require interference/image”将interference/image安装到我的Laravel项目中。然而,一条错误消息显示“内存不足”。有人知道如何解决这个问题吗? 我的Laravel版本是7.15.0。 任何帮助都将不胜感激,因为我尝试了多种方法,但都没有成功。 先谢谢你。

  • 描述 图片展示组件,类似于 HTML image 标签,但提供了更丰富的功能,使用时需指定样式宽高值。 安装 $ npm install rax-image --save 属性 属性 类型 默认值 必填 描述 支持 source Object: {uri: String} - ✔️ 设置图片的 uri style Object: { width: Number height: Number } -

  • 图片操作. 支持 安装 $ npm install universal-image --save 方法 choose(options) 拍照或从本地相册中选择图片。 参数 属性 类型 默认值 必选 描述 支持 count Number 1 x 最大可选照片数 sizeType String Array ['original', 'compressed'] x original 原图,compres

  • 简介 <image> 用于在界面中显示单个图片。 TIP 在代码中请使用 <image> 标签, <img> 的存在只是因为兼容性原因,在将来的版本中可能删除。 Weex 没有内置的图片库,因为一些开源项目如 SDWebImage 和Picasso已经能很好的解决这个问题, 所以在使用 <image> 之前,请在 native 侧先接入相应的 adapter 或者 handler。参见: Andr

  • Random.image( size?, background?, foreground?, format?, text? ) Random.image() Random.image( size ) Random.image( size, background ) Random.image( size, background, text ) Random.image( size, backgrou

  • 这用于将图像添加到图形中。 语法 (Syntax) 以下是添加图像的简单语法。 xtype: 'draw', type: 'image' 例子 (Example) 以下是一个显示用法的简单示例。 <!DOCTYPE html> <html> <head> <link href = "https://cdnjs.cloudflare.com/ajax/libs/extjs/6.0