[英]Can't Use ImageMagick (convert) command in PHP
putenv("PATH=/usr/local/bin:/usr/bin:/bin");
完整命令路徑“ /usr/bin/convert test.png test.webp
”(在終端中工作)
所有 PHP CMD 命令包括: exec()
shell_exec()
system()
passthru()
(所有這些 output 都是空的,但是像echo $PWD
這樣的簡單命令有效)
圖片完整路徑“ convert /home/pi/www/test.png /home/pi/www/test.webp
”(以及上述所有變體)
非常感謝任何幫助!
在 php shell_exec 命令中使用 2>&1 並嘗試此代碼;
$file = "/home/pi/www/test.png";
$dest = "/home/pi/www/test.webp";
$result = '';
$file_extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if(extension_loaded("cwebp") || extension_loaded('gif2webp')) {
if($file_extension=="png" or $file_extension=="jpg" or $file_extension=="jpeg"){
$result = shell_exec("cwebp -q 80 ".escapeshellarg($file)." -o ".escapeshellarg($dest) . " 2>&1");
}
if($file_extension=="gif"){
$result = shell_exec("gif2webp -q 80 ".escapeshellarg($file)." -o ".escapeshellarg($dest) . " 2>&1");
}
} else if(extension_loaded("gmagick") || extension_loaded('imagick')) {
if($file_extension=="png" or $file_extension=="jpg" or $file_extension=="jpeg" or $file_extension=="gif"){
$result = shell_exec("convert ".escapeshellarg($file)." -quality 80 -define webp:lossless=true ".escapeshellarg($dest) . " 2>&1");
}
} else {
die("Does not have any webp library!");
}
echo $result;
我不知道你的 Imagick 版本,一些 arguments 可能因版本而異,但它可能會給你的代碼一些想法。
您在 PHP 中還有一個 imagick 擴展: https://www.php.net/manual/fr/book.imagick.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.