2

我在上传简单的图片时遇到问题。我已经尝试过,只有在添加图像调整大小功能后,上传部分才能完美地工作,但会出现未知错误。

public function post_add_image()
  {
    \Upload::process(array(
        'path'        => '../images/',
        'normalize'   => true,
        'change_case' => 'lower',
        'randomize'   => true,
        'ext_whitelist' => array('jpeg', 'jpg', 'gif', 'png'),
        'max_size'    => 1024 * 1024,

    ));

    if (!\Upload::is_valid()) {
      return $this->response(\Upload::get_errors());
    } else {
      foreach (\Upload::get_files() as $file) {

        // Here comes ERROR
        $image = Image::load($file['file'], false, $file['extension']);
        $image->crop_resize(800, 600);
        $image->save('test');

      }
    }

    //\Upload::save();

    return $this->response(\Upload::get_files());
  }

和错误:

RuntimeException [错误]:库不支持 /tmp/phpOnZReS 的这种文件类型。

COREPATH/classes/image/driver.php@line

我制作了上传文件的 var_dump:

array(10) {
  ["name"]=>
  string(27) "crunchbang-logo-minimal.png"
  ["type"]=>
  string(9) "image/png"
  ["error"]=>
  bool(false)
  ["size"]=>
  int(278)
  ["field"]=>
  string(10) "pictures:0"
  ["file"]=>
  string(14) "/tmp/phpDdTLwJ"
  ["errors"]=>
  array(0) {
  }
  ["extension"]=>
  string(3) "png"
  ["filename"]=>
  string(23) "crunchbang-logo-minimal"
  ["mimetype"]=>
  string(9) "image/png"
}
4

2 回答 2

1

这会有所帮助(在调整文件大小之前,您必须将图像文件保存在公共资产或任何您想要的位置)

public function post_add_image()
{
    \Upload::process(array(
    'path'        => '../images/',
    'normalize'   => true,
    'change_case' => 'lower',
    'randomize'   => true,
    'ext_whitelist' => array('jpeg', 'jpg', 'gif', 'png'),
    'max_size'    => 1024 * 1024,

    ));

   if (!\Upload::is_valid()) {
       return $this->response(\Upload::get_errors());
   } else {

       // Here comes the fix

       Upload::save();
       $saved_image = Upload::get_files();
       $original_name = $saved_image[0]['name'];
       $file_name = $saved_image[0]['saved_as'];  

       $public_asset_path_img = "assets".DS."img";

       $image = Image::load(DOCROOT.$public_asset_path_img.DS.$file_name, false, $file['extension']);
       $image->resize('800');
       $image->save(DOCROOT.$public_asset_path_img.DS.$file_name);


   }

   return $this->response(\Upload::get_files());
}
于 2015-01-30T04:33:18.367 回答
0

我认为您的问题可能是Upload::save()在您尝试调整图像大小后调用。上传的文件将被放入 PHP tmp 文件夹,因此您的/tmp/phpOnZReS.

尝试将您Upload::save()移到循环之前foreach (\Upload::get_files() as $file),看看是否有帮助。

于 2013-04-25T14:44:31.137 回答