如何在上传前调整图像大小并在编码器中存储在数据库中

时间:2017-05-10 12:36:36

标签: php image codeigniter file upload

这是我的控制器代码

`

$ type = explode('。',$ _FILES [' picture'] [' name']);

                        $type = $type[count($type)-1];



                        $url = "uploads/products/images/".uniqid(rand()).".".$type;

                        if(in_array($type, array('jpg','jpeg','png','JPG','JPGE','PNG') ) )
                        {
                            if(is_uploaded_file($_FILES['picture']['tmp_name']))
                            {
                               move_uploaded_file($_FILES['picture']['tmp_name'],$url);
                            }
                        }
                        '

1 个答案:

答案 0 :(得分:0)

由于您正在使用CodeIgniter,因此它拥有自己的Image Manipulation类:

https://www.codeigniter.com/userguide3/libraries/image_lib.html

$config['image_library'] = 'gd2';
$config['source_image'] = '/path/to/image/mypic.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']         = 75;
$config['height']       = 50;

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

$this->image_lib->resize();

将宽度和高度变量设置为将图像大小调整为所需的值。