在Codeigniter中调整大小和裁剪 [英] Resizing and cropping in Codeigniter

查看:96
本文介绍了在Codeigniter中调整大小和裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道如果你能帮助我,基本上我使用Codeigniter,我想要能够上传一个图像,并将其保存到三个不同的文件夹,三个不同的大小,但是,他们必须符合我指定的精确尺寸

Hi I was wondering if you could help me, basically I am using Codeigniter and I want to be able to upload an image and save it to three different folders as three different sizes, however, they must fit the exact dimensions I specify without looking stretched or distorted.

这是我的控制器 - 如果你能帮助我,我会非常感激。

This is my controller - if you could help me I would be most grateful.

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


推荐答案

我不知道你是否有麻烦获取实际的图像sizer库工作,或者你只是想知道如何保存到三个不同的地方,不同的大小...假设你想做后者,你可能想创建一个函数,为你的图像大小的东西,然后传递不同的高度/宽度/目录/名称/ etc。你想要...没有测试它,但它看起来像这样:

I'm not sure if you were having trouble getting the actual image sizer library to work, or whether you just want to know how to save to three different places with different sizes... assuming you want to do the latter, you probably want to just create a function that does the image sizing stuff for you and then pass in the different height/width/name of directory/etc. that you want... Haven't tested it but it would look something like this:

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

这篇关于在Codeigniter中调整大小和裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆