orbitale/imagemagick-php

一个允许向 exec() 函数发送命令以使用 ImageMagick 强大功能的系统。

v3.3.3 2024-06-10 20:34 UTC

README

PHP应用的ImageMagick "exec"组件。

安装

使用 Composer 进行安装,这是您能拥有的最好的包管理器

composer require orbitale/imagemagick-php

要求

  • PHP 7.2 或更高版本
  • 必须在您的服务器上安装 ImageMagick 7,并且二进制文件必须可由运行 PHP 进程的用户执行。

设置

设置不多,但当你实例化一个新的 Command 对象时,你可以在构造函数中直接指定 ImageMagick 的可执行目录,例如

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();

构造函数将自动搜索 magick 可执行文件,测试它,如果不可用则抛出异常。

⚠️ 确保您的 ImageMagick 二进制文件可执行。

用法

首先,我们建议您在 官方文档 中记录所有可能的与 ImageMagick 一起使用的脚本

这些对应于 "传统二进制文件",如果您熟悉或舒适地使用它们,则可以使用它们。

截至 ImageMagick 7,这些不是强制性的,但此包与它们兼容。

带有 ImageMagick 基本标志的基本图像类型转换器

阅读注释

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');
}

调整图像大小

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');
}

当前支持选项

有许多命令行选项,每个都有自己的验证系统。

这就是为什么现在只实现了“一些”,以确保每个都进行验证。

注意:如果某个选项未在 Command 类中实现,您可以创建一个问题或发起一个 Pull Request 来实现新选项!

如果您需要更多,请随时提问/创建问题!

一些为您做魔法的效果别名

  • $command->text():此方法使用添加到 -annotate 中的多个选项来生成文本块。您必须指定其位置和大小,但可以指定颜色和使用的字体文件。

  • $command->ellipse():(检查源代码以获取重原型!)此方法使用 -stroke-fill-draw 选项在您的图片上创建椭圆/圆/圆盘。 注意:建议您检查源代码和文档,以确保您所做的事情。

有用的链接