2011-01-24 56 views
0

当我裁剪图像时,它会变成完全黑色?为什么??Codeigniter - 裁剪图像变黑

$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'; $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg'; 

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


    if(!$this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('submit', $error); } else { $data['upload_data'] = array('upload_data' => $this->upload->data()); $file_name 
    = $this->upload->file_name; 

     list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name); 

     // create small size $config['image_library'] = 'GD2'; $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name; $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name; $config['maintain_ratio'] = TRUE; $config['width'] = 181; $config['height'] = 115; $config['master_dim'] = 'width'; 

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

     if($image_width >= $config['width'] AND $image_height >= $config['height']) { 
      if (!$this->image_lib->resize()) 
      { 
      echo $this->image_lib->display_errors(); 
      } else { 
      if(file_exists($_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name)) 

      { 
       list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name); 
       if($image_height > '115') 
       { 
       $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name; 
       $y_axis = $image_height - 115; 
       $config['y_axis'] = $y_axis; 
       $config['x_axis'] = 181; 
       $this->image_lib->initialize($config); 
       if (!$this->image_lib->crop()) 
        { 
         echo $this->image_lib->display_errors(); 
        } else { 
         echo "cropped";  
       } 
      } 
      } } } 

回答

1

我相信CI使用标准的PHP GD模块。以前我发现如果你使用的图像稍微损坏,它仍然会正常显示,但是一旦你调整大小,你只需要一个黑盒子。

您是否尝试过使用任何其他图像?不同类型的图像(PNG/JPG /等)?

+0

是的,我有,但仍然是相同的结果:(我打算使用图像魔术师,但从我读过它需要安装什么? – wonderwoman

+0

是的,图像magick库需要安装,但它是很简单(有很多教程可以帮助),并且它更易于使用,我建议试试看吧。 –

+0

呃,实际上它和GD2一起工作 - 只是想知道是否可以从顶部和底部进行裁剪不仅仅是顶部? – wonderwoman