php 验证 ImageMagick 安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4208253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:17:56  来源:igfitidea点击:

Verify ImageMagick installation

phpimagemagick

提问by Desmond Liang

My web hosting said ImageMagic has been pre-installed on the server. I did a quick search for "ImageMagick" in the output of phpinfo() and I found nothing. I can't SSH in the server so is there a way in PHP I can verify the installation?

我的虚拟主机说 ImageMagic 已预装在服务器上。我在 phpinfo() 的输出中快速搜索了“ImageMagick”,但一无所获。我无法在服务器中使用 SSH,所以在 PHP 中有一种方法可以验证安装吗?

采纳答案by wajiw

Try this:

尝试这个:

<?php
//This function prints a text array as an html list.
function alist ($array) {  
  $alist = "<ul>";
  for ($i = 0; $i < sizeof($array); $i++) {
    $alist .= "<li>$array[$i]";
  }
  $alist .= "</ul>";
  return $alist;
}
//Try to get ImageMagick "convert" program version number.
exec("convert -version", $out, $rcode);
//Print the return code: 0 if OK, nonzero if error. 
echo "Version return code is $rcode <br>"; 
//Print the output of "convert -version"    
echo alist($out); 
?>

回答by bcosca

This is as short and sweet as it can get:

这是尽可能简短和甜蜜的:

if (!extension_loaded('imagick'))
    echo 'imagick not installed';

回答by Nate Flink

EDIT: The info and script below only applies to iMagick class - which is not added by default with ImageMagick!!!

编辑:下面的信息和脚本仅适用于 iMagick 类 - 默认情况下不会添加 ImageMagick !!!

If I want to know if imagemagick is installed and actually working as a php extension, I paste this snippet into a web accessible file

如果我想知道 imagemagick 是否已安装并实际用作 php 扩展,我将此代码段粘贴到可访问的网络文件中

<?php

error_reporting(E_ALL); 
ini_set( 'display_errors','1');

/* Create a new imagick object */
$im = new Imagick();

/* Create new image. This will be used as fill pattern */
$im->newPseudoImage(50, 50, "gradient:red-black");

/* Create imagickdraw object */
$draw = new ImagickDraw();

/* Start a new pattern called "gradient" */
$draw->pushPattern('gradient', 0, 0, 50, 50);

/* Composite the gradient on the pattern */
$draw->composite(Imagick::COMPOSITE_OVER, 0, 0, 50, 50, $im);

/* Close the pattern */
$draw->popPattern();

/* Use the pattern called "gradient" as the fill */
$draw->setFillPatternURL('#gradient');

/* Set font size to 52 */
$draw->setFontSize(52);

/* Annotate some text */
$draw->annotation(20, 50, "Hello World!");

/* Create a new canvas object and a white image */
$canvas = new Imagick();
$canvas->newImage(350, 70, "white");

/* Draw the ImagickDraw on to the canvas */
$canvas->drawImage($draw);

/* 1px black border around the image */
$canvas->borderImage('black', 1, 1);

/* Set the format to PNG */
$canvas->setImageFormat('png');

/* Output the image */
header("Content-Type: image/png");
echo $canvas;
?>

You should see a hello world graphic:

您应该会看到一个 hello world 图形:

enter image description here

在此处输入图片说明

回答by Ashraf Slamang

In bash:

在 bash 中:

$ convert -version

or

或者

$ /usr/local/bin/convert -version

No need to write any PHP file just to check.

无需编写任何 PHP 文件来检查。

回答by Spencer Hakim

You can easily check for the Imagick class in PHP.

您可以轻松检查 PHP 中的 Imagick 类。

if( class_exists("Imagick") )
{
    //Imagick is installed
}

回答by convert a pdf

Try this one-shot solution that should figure out where ImageMagick is, if you have access to it...

试试这个一次性解决方案,它应该找出 ImageMagick 在哪里,如果你可以访问它......

This found all versions on my Godaddy hosting.

这在我的 Godaddy 主机上找到了所有版本。

Upload this file to your server and call it ImageMagick.phpor something then run it. You will get all the info you need... hopefully...

将此文件上传到您的服务器并调用它ImageMagick.php或其他东西然后运行它。你会得到你需要的所有信息......希望......

Good luck.

祝你好运。

<?
/*
// This file will run a test on your server to determine the location and versions of ImageMagick. 
//It will look in the most commonly found locations. The last two are where most popular hosts (including "Godaddy") install ImageMagick.
//
// Upload this script to your server and run it for a breakdown of where ImageMagick is.
//
*/
echo '<h2>Test for versions and locations of ImageMagick</h2>';
echo '<b>Path: </b> convert<br>';

function alist ($array) {  //This function prints a text array as an html list.
    $alist = "<ul>";
    for ($i = 0; $i < sizeof($array); $i++) {
        $alist .= "<li>$array[$i]";
    }
    $alist .= "</ul>";
    return $alist;
}

exec("convert -version", $out, $rcode); //Try to get ImageMagick "convert" program version number.
echo "Version return code is $rcode <br>"; //Print the return code: 0 if OK, nonzero if error.
echo alist($out); //Print the output of "convert -version"
echo '<br>';
echo '<b>This should test for ImageMagick version 5.x</b><br>';
echo '<b>Path: </b> /usr/bin/convert<br>';

exec("/usr/bin/convert -version", $out, $rcode); //Try to get ImageMagick "convert" program version number.
echo "Version return code is $rcode <br>"; //Print the return code: 0 if OK, nonzero if error.
echo alist($out); //Print the output of "convert -version"

echo '<br>';
echo '<b>This should test for ImageMagick version 6.x</b><br>';
echo '<b>Path: </b> /usr/local/bin/convert<br>';

exec("/usr/local/bin/convert -version", $out, $rcode); //Try to get ImageMagick "convert" program version number.
echo "Version return code is $rcode <br>"; //Print the return code: 0 if OK, nonzero if error.
echo alist($out); //Print the output of "convert -version";

?>

回答by Mr.Yellow

In Bash you can check if Imagick is an installed module:

在 Bash 中,您可以检查 Imagick 是否已安装模块:

$ php -m | grep imagick

If the response is blank it is not installed.

如果响应为空白,则表示未安装。

回答by fmw42

If your ISP/hosting service has installed ImageMagick and put its location in the PATH environment variable, you can find what versions are installed and where using:

如果您的 ISP/托管服务已安装 ImageMagick 并将其位置放在 PATH 环境变量中,您可以找到安装了哪些版本以及在何处使用:

<?php
echo "<pre>";
system("type -a convert");  
echo "</pre>";
?> 

回答by atyachin

To test only the IMagick PHP extension (not the full ImageMagick suite), save the following as a PHP file (testImagick.php) and then run it from console: php testImagick.php

要仅测试 IMagick PHP 扩展(不是完整的 ImageMagick 套件),请将以下内容另存为 PHP 文件 (testImagick.php),然后从控制台运行它: php testImagick.php

<?php
$image = new Imagick();
$image->newImage(1, 1, new ImagickPixel('#ffffff'));
$image->setImageFormat('png');
$pngData = $image->getImagesBlob();
echo strpos($pngData, "\x89PNG\r\n\x1a\n") === 0 ? 'Ok' : 'Failed';
echo "\n";

credit: https://mlocati.github.io/articles/php-windows-imagick.html

信用:https: //mlocati.github.io/articles/php-windows-imagick.html