在Codeigniter中调整大小和裁剪

时间:2011-01-23 19:25:51

标签: codeigniter

您好我想知道您是否可以帮助我,基本上我正在使用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 个答案:

答案 0 :(得分:2)

我不确定你是否无法让实际的图像大小调整库工作,或者你是否只想知道如何保存到不同大小的三个不同的地方......假设你想要做后者,您可能只想创建一个函数来为您调整图像大小,然后传入目录/ etc的不同高度/宽度/名称。你想要的......没有测试过,但它看起来像这样:

function your_function() { 

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

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);

    $this->image_resize('115', '181', 'small', $file_name, $image_width, $image_height);    
    $this->image_resize('300', '400', 'medium', $file_name, $image_width, $image_height);
    $this->image_resize('600', '500', 'large', $file_name, $image_width, $image_height);        
}
}

private function image_resize($height, $width, $path, $file_name, $image_width, $image_height) 
{
    // Resize image settings
    $config['image_library'] = 'GD2';
    $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name;
    $config['new_image'] = $_SERVER['DOCUMENT_ROOT']."/website/uploads/$path/$file_name";
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $width;
    $config['height'] = $height;
    $config['master_dim'] = 'width';

    $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/$path/$file_name")) 
            {
                list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT']."/website/uploads/$path$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";    
                    }
                }
            }       
        }
    }
}