orbitale/imagemagick-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

orbitale/imagemagick-php

Composer 安装命令:

composer require orbitale/imagemagick-php

包简介

A system that allows creating commands to send to the exec() function to use ImageMagick's powerful features.

README 文档

README

An ImageMagick "exec" component for PHP apps.

Installation

Install with Composer, it's the best packages manager you can have :

composer require orbitale/imagemagick-php

Requirements

  • PHP 7.2 or higher
  • ImageMagick 7 has to be installed on your server, and the binaries must be executable by the user running the PHP process.

Settings

There are not many settings, but when you instantiate a new Command object, you may specify ImageMagick's executable directory directly in the constructor, for example :

use Orbitale\Component\ImageMagick\Command;

// Default directory for many Linux distributions
$command = new Command('/usr/bin/magick');

// Or in Windows, depending of the install directory
$command = new Command('C:\ImageMagick\magick.exe');

// Will try to automatically discover the path of ImageMagick in your system
// Note: it uses Symfony's ExecutableFinder to find it in $PATH
$command = new Command();

The constructor will automatically search for the magick executable, test it, and throw an exception if it's not available.

⚠️ Make sure your ImageMagick binary is executable.

Usage

First, we recommend you to note all possible scripts that you can use with ImageMagick in the official docs:

These correspond to the "legacy binaries", and you can use them if you are familiar or comfortable with them.

As of ImageMagick 7, these are not mandatory, but this package is compatible with them.

Basic image type converter with ImageMagick's basic logo

Read the comments :

require_once 'vendor/autoload.php';

use Orbitale\Component\ImageMagick\Command;

// Create a new command
$command = new Command();

$response = $command
    // The command will search for the "logo.png" file. If it does not exist, it will throw an exception.
    // If it does, it will create a new command with this source image.
    ->convert('logo.png')

    // The "output()" method will append "logo.gif" at the end of the command-line instruction as a filename.
    // This way, we can continue writing our command without appending "logo.gif" ourselves.
    ->output('logo.gif')

    // At this time, the command shall look like this :
    // $ "{ImageMagickPath}convert" "logo.png" "logo.gif"

    // Then we run the command by using "exec()" to get the CommandResponse
    ->run()
;

// Check if the command failed and get the error if needed
if ($response->hasFailed()) {
    throw new Exception('An error occurred:'.$response->getError());
} else {
    // If it has not failed, then we simply send it to the buffer
    header('Content-type: image/gif');
    echo file_get_contents('logo.gif');
}

Resizing an image

require_once 'vendor/autoload.php';

use Orbitale\Component\ImageMagick\Command;

// Create a new command
$command = new Command();

$response = $command

    ->convert('background.jpeg')
    
    // We'll use the same output as the input, therefore will overwrite the source file after resizing it.
    ->output('background.jpeg')

    // The "resize" method allows you to add a "Geometry" operation.
    // It must fit to the "Geometry" parameters in the ImageMagick official documentation (see links below & phpdoc)
    ->resize('50x50')

    ->run()
;

// Check if the command failed and get the error if needed
if ($response->hasFailed()) {
    throw new Exception('An error occurred:'.$response->getError());
} else {
    // If it has not failed, then we simply send it to the buffer
    header('Content-type: image/gif');
    echo file_get_contents('logo.gif');
}

Currently supported options:

There are a lot of command-line options, and each have its own validation system.

This is why a "few" ones are implemented now, to make sure validation is possible for each of them.

Note: If an option is not implemented in the Command class, you can create an issue or make a Pull Request that implements the new option!

Feel free to ask/create an issue if you need more!

Some aliases that do magic for you:

  • $command->text(): This method uses multiple options added to the -annotate one to generate a text block. You must specify its position and size, but you can specify color and the font file used.

  • $command->ellipse(): (check source code for the heavy prototype!) This method uses the -stroke, -fill and -draw options to create an ellipse/circle/disc on your picture. Note: I recommend to check both the source code and the documentation to be sure of what you are doing.

Useful links

orbitale/imagemagick-php 适用场景与选型建议

orbitale/imagemagick-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 421.42k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2015 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 orbitale/imagemagick-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 42
  • Watchers: 2
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-07