-1

在我当前的项目中,我正在使用带有 PHP 和 ImageMagick 的 plupload 插件,并且它工作正常 - 现在我的下一个任务是将上传的文件重命名为小写。

要重命名这些文件,我首先将上传的图像存储在扩展名为“.temp”的临时文件夹中,然后将文件移动到主文件夹并尝试重命名。该函数第一次执行时,如果没有匹配的图像,它将完美地存储它,但第二次执行它就不行了。该函数应检查现有文件名,并应存储具有递增索引的重复,即 image_1.jpg、image_2.jpg 等,但它不会这样做,它只是覆盖图像,即 image.jpg。

这是我的代码片段:

  $filename = $this->input->post('filename');
  $root  = $_SERVER["DOCUMENT_ROOT"]."/uploads/";
  $source_image  = $root.'temp/'.$filename;

  $image_name = explode(".",$filename);
  rename($source_image, $source_image. '.temp');
  copy($source_image.'.temp', $root.$filename.'.temp');
  unlink($source_image.'.temp');

  $exp_temp_img_slash = explode(".",$filename);
  $rename_img_a = strtolower($exp_temp_img_slash[0]);
  $rename_img_b = $exp_temp_img_slash[1];

  if (file_exists($root.$rename_img_a .".".$rename_img_b)) {
      $count=1;

      while (file_exists($root. $rename_img_a ."_".$count. ".".$rename_img_b)) {
          $count++;
           $rename_img = strtolower($rename_img_a . '_' . $count . ".".$rename_img_b);
      }

      $renamed_image = $_SERVER["DOCUMENT_ROOT"]."/uploads/".$rename_img;
      rename($root.$filename.".temp",$renamed_image);
   } else {
       $rename_img = strtolower($rename_img_a .".".$rename_img_b);
       $renamed_image = $_SERVER["DOCUMENT_ROOT"]."/uploads/".$rename_img;
       rename($root.$filename.".temp",$renamed_image);
   }

任何帮助将不胜感激,谢谢。

4

1 回答 1

0
shell_exec($imagemagick_folder."convert ".$from . $fileName." -resize ".$width."x".$height." -quality 95 ".$to.$fileName);

$imagemagick_folder 是安装 imagemagick 的文件夹(例如 C:\imagemagick)

$from 和 $to 是开始和目标文件夹

如果要调整图像大小,$width 和 $height 是可选的

于 2012-04-27T08:10:09.890 回答