上传然后调整图像大小

时间:2013-03-09 10:05:00

标签: upload fuelphp

我在简单的图片上传方面遇到了麻烦。我已经尝试过,只有在添加图像调整大小功能后才能使上传部分完美地工作,从而产生未知错误。

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"
}

2 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分:0)

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

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