可以在PHP中使用ImageMagick(转换)命令

发布于 2025-01-20 05:55:22 字数 694 浏览 2 评论 0原文


I want to edit images using ImageMagick and PHP on my website.
I'm on Linux (Raspbian OS) and have PHP and ImageMagick installed.
I'm trying to do something similar to: `convert test.png test.webp` using PHP. All work in the Terminal.

What I've tried:

putenv("PATH=/usr/local/bin:/usr/bin:/bin");
完整命令路径“/usr/bin/convert test.png test.webp”(在终端中工作)
所有 PHP CMD 命令包括:​​ exec() shell_exec() system() passthru()
(所有这些输出都是空的,但是像 echo $PWD 这样的简单命令有效)
图像完整路径“convert /home/pi/www/test.png /home/pi/www/test.webp”(以及所有上述变体)

非常感谢任何帮助!

I want to edit images using ImageMagick and PHP on my website.
I'm on Linux (Raspbian OS) and have PHP and ImageMagick installed.
I'm trying to do something similar to: `convert test.png test.webp` using PHP. All work in the Terminal.

What I've tried:

putenv("PATH=/usr/local/bin:/usr/bin:/bin");
Full command path "/usr/bin/convert test.png test.webp" (Work in Terminal)
All of the PHP CMD commands including: exec() shell_exec() system() passthru()
(All of the output of those were empty, but simple commands like echo $PWD worked)
Images Full Path "convert /home/pi/www/test.png /home/pi/www/test.webp" (And all of the above variations)

Any help in hugely appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耶耶耶 2025-01-27 05:55:22

在 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 版本,某些参数可能因版本而异,但它可能会给您的代码带来一些想法。

Use 2>&1 in php shell_exec command and try this code;

$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;

I don't know your version of Imagick, some arguments may vary by version, but it might give some idea for your code.

橪书 2025-01-27 05:55:22

您还有 PHP 中的 imagick 扩展: https://www.php.net /manual/fr/book.imagick.php

You have also an imagick extension in PHP: https://www.php.net/manual/fr/book.imagick.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文