承接 walrussoup/laravel-shortpixel 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

walrussoup/laravel-shortpixel

最新稳定版本:1.0.1

Composer 安装命令:

composer require walrussoup/laravel-shortpixel

包简介

Shortpixel integration for laravel 9+

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Makes using the shortpixel reducer api with laravel slightly less painful.

Archiving

Archiving this to move to BunnyCDN. It's cheaper, and their API for using the on the fly image optimization is only $9.99 a month - literally cannot beat those prices.

Installation

You can install the package via composer:

composer require walrussoup/laravel-shortpixel

You can publish and run the migrations with:

php artisan vendor:publish --tag="laravel-shortpixel-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="laravel-shortpixel-config"

This is the contents of the published config file:

return [
    'api_key' => env('SHORTPIXEL_API_KEY'),
    'plugin_version' => env('SHORTPIXEL_PLUGIN_VERSION'),
    'log_channel' => env('SHORTPIXEL_LOG_CHANNEL', 'default')
];

Usage

I recommend setting a log channel specific to compression. I set this in logging & simply set the output to another log file.

$laravelShortpixel = new WalrusSoup\LaravelShortpixel();
// set this up yourself or just let the container do it
$laravelShortpixel->setApiKey('your api key');
$laravelShortpixel->setPluginVersion('your plugin version');
$laravelShortpixel->setLogChannel('your log channel');

// Create an image configuration that compresses the original format and also outputs a webp format
$configuration = (new CompressionConfig())
        ->resizeToCover(500, 400)
        ->addImage('https://images.unsplash.com/photo-1611457194403-d3aca4cf9d11')
        // or, add multiple images using addImages()
        ->useLosslessCompression()
        ->convertToWebp()
        ->retainOriginalFormat();

/** @var ShortpixelCompressionResult $results */
$results = $laravelShortpixel->callShortpixelAndWait($configuration);

foreach($results as $result) {
    ray($result->getOriginalUrl(), $result->getCompressedUrl());
}

Understanding The API... kinda

The Shortpixel API accepts the original configuration in it's entirety to keep track of compression results. If you are not using a long-running job with callShortpixelAndWait() you will need to store the full configuration somewhere. I recommend letting a job handle this via its serialization.

The other thing is keeping track of the original names. For the sake of keeping things consistent, I made some methods for these.

// If you requested a conversion to another format, this will help you get the original name
$originalName = $result->getOriginalFilenameWithNewExtension();
// WEBP and AVIF are split off into a separate key, so you can use this to get it I suppose
$originalNameWebp = $result->getOriginalFilenameWebpLossy();
// Again, these are names only. You will need to still download the URL from the other key, for instance:
file_get_contents($result->getWebpLosslessURL());

Job Example

This is how I use it. It could probably be better, but it works for my case since jobs can be retried later.

namespace App\Jobs;

use WalrusSoup\LaravelShortpixel\CompressionConfig;
use WalrusSoup\LaravelShortpixel\LaravelShortpixel;

class CompressImage implements ShouldQueue
{
    public function __construct(public CompressionConfig $config) {}
    
    public function handle(LaravelShortpixel $laravelShortpixel)
    {
        // Queue the job and forget about it for 5 minutes
        $laravelShortpixel->callShortPixel($this->config);
        // Fetch the result later
        CheckCompressionResults::dispatch($this->config)->delay(now()->addMinutes(5));
    }
}

// another job
class CheckCompressionResults implement ShouldQueue
{
    public $tries = 1;
    
    public function __construct(public CompressionConfig $config) {}
    
    public function handle(LaravelShortpixel $laravelShortpixel)
    {
        // Again, we have to give them the original config. It won't compress again, it will just return the results
        $results = $laravelShortpixel->callShortPixelAndWait($this->config);
        
        foreach($results as $result) {
            // do something with the result
        }
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固