iglesias/image-converter
Composer 安装命令:
composer require iglesias/image-converter
包简介
PHP Image Converter with GD
README 文档
README
A lightweight, efficient PHP class for converting images between formats using the GD extension. It supports dynamic detection of GD capabilities, resizing, quality adjustment, and output to disk or Base64-encoded strings. Ideal for web applications needing on-the-fly image optimization, like reducing file sizes for faster loading without external dependencies.
This library prioritizes performance by leveraging GD's native functions and avoiding unnecessary overhead. It's designed for production use but can be optimized further for high-volume scenarios.
Requirements
- PHP 7.4+ (compatible with 8.x)
- GD extension enabled (compile with
--with-gdor install via package manager likeapt install php-gd) - Memory limit: At least 128MB recommended for large images; GD can be memory-hungry during resizing.
Verify GD support: Run php -i | grep GD or use ImageConverter::getTypesSupported() in your code.
Installation
- Clone the repo:
git clone https://github.com/yourusername/imageconverter.git - Include the class in your project:
require_once 'ImageConverter.php'; - Using Composer: composer
require iglesias/image-converter
Usage
Convert to Disk
Save an image to a new file in the desired format, with optional quality and resize.
ImageConverter::convertToDisk('input.jpg', 'output.webp', 'webp', 80, 800, 600);
- Parameters:
$file_path: Path to source image (string, required).$save_path: Path to save converted image (string, required).$format: Target format (string, default 'webp'; e.g., 'png', 'jpeg').$quality: Compression level (int, 0-100, default 80; lower = smaller file, more lossy).$width: New width in pixels (int, default 0 = no resize).$height: New height in pixels (int, default 0 = no resize).
Convert to Base64
Generate a Base64 string for inline use (e.g., in HTML <img src>).
$base64 = ImageConverter::convertToBase64('input.jpg', 'webp', 80, 800, 600); echo '<img src="' . $base64 . '">';
- Parameters: Same as
convertToDisk, minus$save_path. - Returns:
data:image/format;base64,...string.
Get Supported Formats
Dynamically list formats GD supports on your system.
print_r(ImageConverter::getTypesSupported()); // e.g., ['image/jpeg', 'image/png', 'image/webp']
Supported Formats
Depends on your GD build. Common ones: JPEG, PNG, GIF, WebP, BMP, AVIF. The class auto-detects via gd_info() and filters to: image/jpeg, image/png, image/gif, image/webp, image/bmp, image/avif.
Quality scales differently:
- JPEG/WebP/AVIF: 0-100
- PNG: 0-9 (compression level)
- GIF/BMP: No quality param (lossless)
Error Handling
Methods throw Exception on issues like:
- GD not loaded
- Unsupported format/file type
- File not found/invalid
- Image creation failure
Wrap calls in try-catch for robustness:
try { ImageConverter::convertToDisk(...); } catch (Exception $e) { echo "Error: " . $e->getMessage(); }
License
MIT License. See LICENSE file.
iglesias/image-converter 适用场景与选型建议
iglesias/image-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 iglesias/image-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iglesias/image-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-10