2012-07-16 114 views
0

我真的很努力与我的CodeIgniter脚本为用户上传,调整大小和裁剪配置文件图像。最后,我想用一个加密名称上传图像,调整大小并保存两次(一次作为缩略图,一次作为中等大小的图像),然后删除原始文件。但是,我想我真的错过了一些东西,因为我无法获得第一个上传调整大小的过程。我肯定在代码的某个地方有错误(跟在代码后面的错误),但我想我可能在我对这里的逻辑的理解上存在差距。谁知道?任何反馈都非常感谢。CodeIgniter的图片上传和调整大小失败

public function image() { 
    $config['upload_path'] = './temp_images/'; // This directory has 777 access 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size'] = '2048'; 
    $config['max_width'] = '0'; // For unlimited width 
    $config['max_height'] = '0'; // For unlimited height 
    $config['encrypt_name'] = TRUE; 
    $config['remove_spaces'] = TRUE; 

    $this->load->library('upload', $config); 

    if (! $this->upload->do_upload()) 
    { 
     $data['error'] = array('error' => $this->upload->display_errors()); 
     $this->load->view('upload', $data); 
    } 
    else 
    { 
     $image_data_array = array('upload_data' => $this->upload->data()); 
     foreach ($image_data_array as $image_data){ 
      $raw_name = $image_data['raw_name']; 
      $file_ext = $image_data['file_ext']; 
     } 

     $file_name = $raw_name . $file_ext; 
     $image_path = 'temp_images/' . $file_name; 
     $new_path = 'image/profile/'; // This directory has 777 access 

     $config['image_library'] = 'gd'; 
     $config['source_image'] = $image_path; 
     $config['new_image'] = $new_path; 
     $config['maintain_ratio'] = FALSE; 
     $config['width']  = 140; 
     $config['height'] = 140; 
     $config['quality'] = '80%'; 

     $new_name = $new_path . $raw_name . $file_ext; 

     $this->load->library('image_lib', $config); 

     // The following is just to test that it worked: 

     if (! $this->image_lib->resize()) { 
      echo $this->image_lib->display_errors(); 
      echo 'source: ' . $config['source_image'] . '<br />'; 
      echo 'new: ' . $config['new_image'] . '<br />'; 
     } 
     else { 
      echo "<img src='" . base_url() . $new_name . "' />"; 
     } 

     $this->image_lib->clear(); 

    } 
} 

上传过程正常。即使调整大小,我刚刚打电话$this->image_lib->resize()。当我尝试错误捕捉时,它开始对我大喊大叫。我双重验证了temp_images和image/profile都具有777权限(如前所述,上载和调整大小实际上在一个点上工作)。这里是产生的错误:

Unable to save the image. Please make sure the image and file directory are writable. 
source: temp_images/8ee1bab383cf941f34218f7535d5c078.jpg 
new: image/profile/ 
+0

当140x140建立映像$配置[“new_image” ]必须是图像的完整路径,包括文件名“image/profile/$ filename”。 $ filename就像“image.jpg” – user2844810 2015-05-07 19:25:00

回答

1

从来没有真正使用过CI,也看不到你的代码的细节。

  • 首先,检查在文件夹上设置的权限 - 您正尝试上传到该文件夹​​。也尝试使用完整的服务器路径。
  • 其次,看看你是否有PHP的图像扩展,如GD,ImageMagick启用。
0

请通过包含文件名来更新new_image值。

$config['new_image'] = 'image/profile/' . $filename; 
1

我已经试过这tutorial

它可以很好地用于图片上传和调整大小的图像文件夹中创建大拇指...