2010-11-17 78 views
66

我的虛擬主機表示ImageMagic已經預先安裝在服務器上。我在phpinfo()的輸出中快速搜索了「ImageMagick」,但我什麼也沒找到。我不能在服務器中SSH,所以有沒有辦法在PHP我可以驗證安裝?驗證ImageMagick安裝

回答

42

試試這個:

<?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); 
?> 
+14

此測試是否安裝了ImageMagick的應用程序,而不是PHP模塊 – stillstanding 2010-11-17 19:46:39

+0

版本返回碼是0 *版本:ImageMagick 6.3.7 08/09/09 Q16 http://www.imagemagick.org *版權:Copyright(C)1999-2008 ImageMagick Studio LLC – 2010-11-17 19:50:34

+0

這是什麼該頁面返回。似乎它返回版本有問題,但不知何故返回版權信息。 – 2010-11-17 19:51:36

15

您可以輕鬆地爲您在PHP的Imagick類。

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

**重要:**有時這會返回FALSE,但是'extension_loaded('imagick')'返回TRUE!,所以我想更好的是:'if(extension_loaded('imagick')|| class_exists(「Imagick」)){/ *做Imagick * /}' – 2014-04-17 07:43:21

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

試試這個一次性的解決方案,應該找出其中ImageMagick的是,如果你有機會獲得它...

這才發現我的Godaddy主機的所有版本。

將此文件上傳到您的服務器並將其命名ImageMagick.php或其他內容,然後運行它。你會得到你需要的所有信息...希望...

祝你好運。

<? 
/* 
// 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"; 

?> 
+1

轉換pdf:許多thx。很好的腳本。在hostgator和godaddy上運行良好......沒有云或AWS那麼酷,但在我的小企業客戶的預算之內。 – zipzit 2013-10-08 21:41:37

+1

工作時間...谷歌吃這個:MediaWiki創建縮略圖時出錯:sh:/ usr/local/bin/convert:沒有這樣的文件或目錄 – Martin 2014-01-25 14:55:13

+0

Mine是基於.NET和Sitecore的應用程序。如何檢查我的應用程序是否使用ImageMagick? – 2016-05-16 06:26:11

30

編輯:下面的信息和腳本只適用於iMagick類 - 這是不是默認添加與ImageMagick的!

如果我想知道是否安裝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; 
?> 

你應該可以看到一個Hello World圖形:

​​

5

在bash:

$ convert -version 

$ /usr/local/bin/convert -version 

不需要編寫任何PHP文件只是爲了檢查。

0

如果你的ISP /託管服務已經安裝了ImageMagick的,並把它的位置在PATH環境變量中,你可以找到所安裝的版本,並在那裏使用:

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