简体   繁体   English

PHP代码将图像格式更改为JPG

[英]PHP code to change image format to JPG

I constructed this PHP code to use ImageMagick to convert gif files to jpg but it doesn't seem to work. 我构造了此PHP代码,以使用ImageMagick将gif文件转换为jpg,但似乎无法正常工作。 Maybe I am missing something: 也许我缺少了一些东西:

<?php
$cwd = getcwd();
$directory = 'temp_images/input/'; // add path to the source direcotry, reprocesses all images directly, not fancy.

$dh = opendir($directory);

while($file = readdir($dh)) {
  if (strlen($file) > 2) {
    $dirfiles[]=$file;
  }
}
closedir($dh);
reset($dirfiles);
asort($dirfiles);


set_time_limit(0);
foreach ($dirfiles as $dirfile) {
  $file = $directory.$dirfile;
  $cmd = 'mogrify -format jpg *.gif $file';     
  echo  $dirfile . " mogrified to JPG \n";
}

?>

The gif files are not being converted. gif文件未转换。

UPDATE: In my own code I replaced with $cmd = 'mogrify -format jpg *.gif $file'; 更新:在我自己的代码中,我替换为$ cmd ='mogrify -format jpg * .gif $ file'; to exec("mogrify -format jpg . $file"); 到exec(“ mogrify -format jpg $ file”);

I was under the impression when I use mogrify the original files are purged. 当我使用mogrify清除原始文件时,给我的印象是。 How do I go about it? 我该怎么办?

$cmd永远不会执行...它只是一个字符串值。

You are using the wrong type of 'quotes' 您使用了错误类型的“引号”

foreach ($dirfiles as $dirfile) {
  $file = $directory.$dirfile;
  $cmd = 'mogrify -format jpg *.gif $file';     
  echo  $dirfile . " mogrified to JPG \n";
}

You want the back-tick operator . 您要使用反勾号运算符

foreach ($dirfiles as $dirfile) {
  $file = $directory.$dirfile;
  $cmdResult = `mogrify -format jpg *.gif $file`;     
  echo  $dirfile . " mogrified to JPG \n";
}

This is identical to using shell_exec . 这与使用shell_exec相同。

foreach ($dirfiles as $dirfile) {
  $file = $directory.$dirfile;
  $cmdResult = shell_exec ( "mogrify -format jpg *.gif {$file}" );     
  echo  $dirfile . " mogrified to JPG \n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM