承接 phpsamurai/image-conversion 相关项目开发

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

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

phpsamurai/image-conversion

Composer 安装命令:

composer require phpsamurai/image-conversion

包简介

A simple laravel package for converting media images to WebP format

README 文档

README

  • This package converts images in your Laravel application to WebP format, helping to reduce file sizes and optimize loading speeds.

Author: Sief Hesham

Contact: dev.sief.hesham@gmail.com

1. Requirements

  • PHP: 8.0 or above
  • Laravel: 10 or above

Packages

  • Spatie Image: For image manipulation.
  • Spatie Laravel Media Library: For managing media files.

2. Installation

  • Step 1: Require the Package To install the package, run the following command in your Laravel project's root directory:
composer require phpsamurai/image-conversion:"^1.0.3" -W
  • step 2: Publish the Configuration File, Run the following command to publish the package configuration file:
php artisan vendor:publish --provider="phpsamurai\ImageConversion\ImageConversionServiceProvider"
  • Step 3: update the .env file with the following configuration:
IMAGE_FOLDER=youre-image-folder-name
WEBP_QUALITY=85
IMAGE_CONVERSION_USE_QUEUE=true

This will add the package to your project's dependencies and make it available in your application.

3. Configuration

  • Step 1: Set Up Queue Configuration (Optional) The package uses Laravel’s job dispatching system to convert images asynchronously. If you haven't already configured a queue driver, consider using database, redis, or another supported driver to handle background jobs.

  • Open the .env file and set your QUEUE_CONNECTION to a non-sync driver (e.g., database or redis).

QUEUE_CONNECTION=database

Run Migrations to create the necessary database table for queued jobs if you choose the database driver:

php artisan queue:table
php artisan migrate

Start the Queue Worker to process jobs:

php artisan queue:work

This step is optional but recommended for large numbers of images, as the package will otherwise process conversions synchronously, which could impact performance.

4. Usage

Step 1: Run the Conversion Command

Once the package is installed, you can use the following Artisan command to convert images in your media library to WebP format.

php artisan media:convert-to-webp

Command Description

Purpose

  • Converts all images in the media library to WebP format.

Process

  • Fetches all media files with image MIME types (image/jpeg, image/png, etc.) that aren’t already in WebP format.

  • Dispatches a job (ConvertImageToWebPJob) for each image, converting it to WebP format and updating the database record.

  • Output: Displays a confirmation message once all jobs have been dispatched.

  • Note: Each image is converted and stored as a WebP file, replacing the original format in the database and on disk.

Step 2: Monitor Job Progress (If Using Queues)

  • If you're running the command asynchronously with a queue driver (e.g., database or redis), the conversion jobs will be added to your queue. You can monitor the progress of these jobs via your preferred queue monitoring tool or by viewing the queue database table.

  • Example Queue Worker Command To run the queue worker in the background:

php artisan queue:work

5. Additional Information

Image Conversion Details

  • The package uses the Spatie Image package to convert images to WebP format. During conversion, the following steps occur:

  • The original image file path is identified and processed. A new WebP file is generated.

  • The original image file is replaced by the WebP file in the storage location. The media record in the database is updated with the new file name and MIME type (image/webp).

Important Notes

  • Image Formats Supported: The package will convert JPEG, JPG, and PNG formats to WebP. Any other formats will be ignored.

  • Data Integrity: The database records for each media file are updated to reflect the new file name and MIME type after conversion.

  • Overwriting Files: Each converted WebP file will replace the original image file, so make sure you have backups if you need to keep original formats.

6. Troubleshooting

Common Issues

  • Queue Not Processing Jobs: Make sure your queue worker is running (php artisan queue:work). Also, check that your QUEUE_CONNECTION in .env is correctly set to database, redis, or another valid driver.

  • File Permission Errors: Ensure that your storage directory has the correct permissions, especially if you're using local storage (storage/app).

  • Image Conversion Failures: If an image cannot be converted, ensure that Spatie Image is properly installed and that the image file is accessible.

7. License

This package is open-sourced software licensed under the MIT license.

phpsamurai/image-conversion 适用场景与选型建议

phpsamurai/image-conversion 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「images」 「png」 「laravel」 「jpeg」 「jpg」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 phpsamurai/image-conversion 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 phpsamurai/image-conversion 我们能提供哪些服务?
定制开发 / 二次开发

基于 phpsamurai/image-conversion 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-07