Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Codeigniter中上载时,如何将图像大小限制为特定大小?_Php_Html_Codeigniter - Fatal编程技术网

Php 在Codeigniter中上载时,如何将图像大小限制为特定大小?

Php 在Codeigniter中上载时,如何将图像大小限制为特定大小?,php,html,codeigniter,Php,Html,Codeigniter,我的控制器功能是 public function add() { $blog_title = $this->input->post("qes_blog_title"); $description = htmlentities($_POST['qes_blog_description']); $edit_status = $this->input->post("edit"); $filename = $blog_title .

我的控制器功能是

public function add() { 
    $blog_title  = $this->input->post("qes_blog_title"); 
    $description = htmlentities($_POST['qes_blog_description']); 
    $edit_status = $this->input->post("edit"); 
    $filename    = $blog_title . date('ymdhis'); 
    if ($this->session->userdata('user_type') == "SA") { 
        $dirname  = (dirname(dirname(dirname(__FILE__)))) . 
            '/uploads/blog/admin'; 
        $usertype = "1"; 
    } else { 
        $dirname  = (dirname(dirname(dirname(__FILE__)))) . '/uploads/blog/' 
            . $this->session->userdata('user_id'); 
        $usertype = "0"; 
    } 
    if (!file_exists($dirname)) { 
        mkdir($dirname, 0777, true); 
    } 
    $config['upload_path']   = $dirname; 
    $config['file_name']     = $filename; 
    $config['allowed_types'] = 'jpeg|jpg|png'; 
    $this->load->library('upload', $config); 
    $this->upload->initialize($config); 
    if (!$this->upload->do_upload("qes_blog_image")) { 
        //print_r($this->upload->display_errors()); 
        $data = array( 
            'qes_blog_title' => $blog_title, 
            'qes_blog_description' => $description, 
            'qes_vendor_id' => $this->session->userdata('user_id'), 
            'qes_user_type' => $usertype, 
            'qes_created_at' => date('Y-m-d H:i:s') 
        ); 
    } else { 
        if ($edit_status) { 
            $condition       = array( 
                'qes_vendor_blog_id' => $edit_status 
            ); 
            $categoryDetails = $this->common_model->get_row($condition, $this->tablename); 
            if ($categoryDetails->qes_category_image != NULL) { 
                if ($this->session->userdata('user_type') == "SA") { 
                    unlink((dirname(dirname(dirname(__FILE__)))) . '/uploads/blog/admin/' . $categoryDetails->qes_blog_image); 
                } else { 
                    unlink((dirname(dirname(dirname(__FILE__)))) . '/uploads/blog/' . $categoryDetails->qes_vendor_id . "/" . $categoryDetails->qes_blog_image); 
                } 
            } 
        } 
        $uploqes_data     = $this->upload->data(); 
        $uploqes_filename = $uploqes_data['file_name']; 
        $data             = array( 
            'qes_blog_title' => $blog_title, 
            'qes_blog_description' => $description, 
            'qes_vendor_id' => $this->session->userdata('user_id'), 
            'qes_user_type' => $usertype, 
            'qes_blog_image' => $uploqes_filename, 
            'qes_created_at' => date('Y-m-d H:i:s') 
        ); 
    } 
    $condition    = array( 
        'qes_blog_title' => $blog_title 
    ); 
    $blog_details = $this->common_model->get_row($condition, $this->tablename); 
    if ($edit_status) { 
        if (count($blog_details) > 0) { 
            if ($blog_details->qes_blog_title == $blog_title) { 
                $condition = array( 
                    'qes_vendor_blog_id' => $edit_status 
                ); 
                $result    = $this->common_model->update_row($data, $condition, $this->tablename); 
                $status    = 'Blog "<b>' . $blog_title . '</b>" Successfully Updated'; 
            } else { 
                $htm    = "0"; 
                $result = "0"; 
                $status = "0"; 
            } 
        } else { 
            $condition = array( 
                'qes_vendor_blog_id' => $edit_status 
            ); 
            $result    = $this->common_model->update_row($data, $condition, $this->tablename); 
            $status    = 'Blog "<b>' . $blog_title . '</b>" Successfully Updated'; 
        } 
    } else { 
        if (count($blog_details) > 0) { 
            if ($blog_details->qes_blog_title == $blog_title) { 
                $htm    = "0"; 
                $result = "0"; 
                $status = "0"; 
            } else { 
                $result = $this->common_model->insert_data($this->tablename, $data); 
                $status = 'Blog "<b>' . $blog_title . '</b>" Successfully Created'; 
            } 
        } else { 
            $result = $this->common_model->insert_data($this->tablename, $data); 
            $status = 'Blog "<b>' . $blog_title . '</b>" Successfully Created'; 
        } 
    } 
    if ($result) { 
        $htm = $result; 
        $this->session->set_flashdata('success_msg', $status); 
    } else { 
        $htm = '0'; 
        $this->session->set_flashdata('success_msg', 'Error While Creating Parent'); 
    } 
    echo $htm; 
}
公共函数add(){
$blog_title=$this->input->post(“qes_blog_title”);
$description=htmlentities($_POST['qes_blog_description']);
$edit_status=$this->input->post(“编辑”);
$filename=$blog_title.date('ymdhis');
如果($this->session->userdata('user_type')==“SA”){
$dirname=(dirname(dirname(dirname(_文件__)))。
“/uploads/blog/admin”;
$usertype=“1”;
}否则{
$dirname=(dirname(dirname(dirname(_文件__)))。/uploads/blog/'
.$this->session->userdata('user_id');
$usertype=“0”;
} 
如果(!file_存在($dirname)){
mkdir($dirname,0777,true);
} 
$config['upload_path']=$dirname;
$config['file_name']=$filename;
$config['allowed_types']='jpeg | jpg | png';
$this->load->library('upload',$config);
$this->upload->initialize($config);
如果(!$this->upload->do_upload(“qes_blog_image”){
//打印($this->upload->display_errors());
$data=数组(
'qes_blog_title'=>$blog_title,
“qes_blog_description”=>$description,
“qes\u供应商\u id”=>$this->session->userdata('user\u id'),
'qes_user_type'=>$usertype,
'qes_在'=>日期('Y-m-d H:i:s'创建)
); 
}否则{
如果($edit_status){
$condition=数组(
“qes\u供应商\u博客\u id”=>$edit\u状态
); 
$categorydeails=$this->common\u model->get\u row($condition,$this->tablename);
如果($categorydeails->qes\u category\u image!=NULL){
如果($this->session->userdata('user_type')==“SA”){
取消链接((dirname(dirname(dirname(_文件)))。/uploads/blog/admin/。$categorydeails->qes_blog_image);
}否则{
取消链接((目录名(目录名(目录名(_文件__)))。/uploads/blog/。$categorydeails->qes_供应商_id。“/”$categorydeails->qes_blog_图像);
} 
} 
} 
$UPLOQUES_data=$this->upload->data();
$UPLOQUES_filename=$UPLOQUES_数据['file_name'];
$data=数组(
'qes_blog_title'=>$blog_title,
“qes_blog_description”=>$description,
“qes\u供应商\u id”=>$this->session->userdata('user\u id'),
'qes_user_type'=>$usertype,
'qes_blog_image'=>UPLOQUES_文件名,
'qes_在'=>日期('Y-m-d H:i:s'创建)
); 
} 
$condition=数组(
'qes_blog_title'=>$blog_title
); 
$blog\u details=$this->common\u model->get\u row($condition,$this->tablename);
如果($edit_status){
如果(计数($blog_详细信息)>0{
如果($blog_details->qes_blog_title==$blog_title){
$condition=数组(
“qes\u供应商\u博客\u id”=>$edit\u状态
); 
$result=$this->common\u model->update\u row($data,$condition,$this->tablename);
$status='Blog'.$Blog_title.'Successfully Updated';
}否则{
$htm=“0”;
$result=“0”;
$status=“0”;
} 
}否则{
$condition=数组(
“qes\u供应商\u博客\u id”=>$edit\u状态
); 
$result=$this->common\u model->update\u row($data,$condition,$this->tablename);
$status='Blog'.$Blog_title.'Successfully Updated';
} 
}否则{
如果(计数($blog_详细信息)>0{
如果($blog_details->qes_blog_title==$blog_title){
$htm=“0”;
$result=“0”;
$status=“0”;
}否则{
$result=$this->common\u model->insert\u data($this->tablename,$data);
$status='Blog'.$Blog_title.'Successfully Created';
} 
}否则{
$result=$this->common\u model->insert\u data($this->tablename,$data);
$status='Blog'.$Blog_title.'Successfully Created';
} 
} 
如果($结果){
$htm=$result;
$this->session->set_flashdata('success_msg',$status);
}否则{
$htm='0';
$this->session->set_flashdata('success_msg','Error when Creating Parent');
} 
echo$htm;
}
我是Codeigniter的初学者


如何在上传时限制图像的大小。只能上载特定大小的图像。上面是添加博客的控制器函数。如何在上面的代码中指定大小。如何在Codeigniter中上载图像时保持“仅上载指定大小的图像”这样的条件?

请参考此代码,这可能会帮助您..谢谢

    $file_name = '';
            $error = '';
            if ($_FILES['image']['name'] != '') {

                $config['upload_path'] = 'uploads/blog/main/';
                $config['allowed_types'] = 'jpg|png|jpeg';
                $this->load->library('upload', $config);

                if (!$this->upload->do_upload('image')) {
                    $error = $this->upload->display_errors();
                } else {
                    $data = array('upload_data' => $this->upload->data());

                    $file_name = $data['upload_data']['file_name'];
                    $config['source_image'] = 'uploads/blog/main/' . $file_name;
                    $config['new_image'] = 'uploads/blog/thumb/' . $file_name;

                    $config['width'] = 172;
                    $config['height'] = 122;
                    $config['maintain_ratio'] = FALSE;
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    if ($data['upload_data']['image_height'] > $this->main_height || $data['upload_data']['image_width'] > $this->main_width) {
                        $config['image_library'] = 'gd2';
                        $config['source_image'] = 'uploads/blog/main/' . $file_name;
                        $config['new_image'] = 'uploads/blog/main/' . $file_name;
                        $config['width'] = $this->main_width;
                        $config['height'] = $this->main_height;
                        $config['maintain_ratio'] = FALSE;
                        $this->image_lib->initialize($config);
                        $this->image_lib->resize();
                    }
                }
            }

请参考此代码,这可能会帮助您..谢谢

    $file_name = '';
            $error = '';
            if ($_FILES['image']['name'] != '') {

                $config['upload_path'] = 'uploads/blog/main/';
                $config['allowed_types'] = 'jpg|png|jpeg';
                $this->load->library('upload', $config);

                if (!$this->upload->do_upload('image')) {
                    $error = $this->upload->display_errors();
                } else {
                    $data = array('upload_data' => $this->upload->data());

                    $file_name = $data['upload_data']['file_name'];
                    $config['source_image'] = 'uploads/blog/main/' . $file_name;
                    $config['new_image'] = 'uploads/blog/thumb/' . $file_name;

                    $config['width'] = 172;
                    $config['height'] = 122;
                    $config['maintain_ratio'] = FALSE;
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->image_lib->clear();
                    if ($data['upload_data']['image_height'] > $this->main_height || $data['upload_data']['image_width'] > $this->main_width) {
                        $config['image_library'] = 'gd2';
                        $config['source_image'] = 'uploads/blog/main/' . $file_name;
                        $config['new_image'] = 'uploads/blog/main/' . $file_name;
                        $config['width'] = $this->main_width;
                        $config['height'] = $this->main_height;
                        $config['maintain_ratio'] = FALSE;
                        $this->image_lib->initialize($config);
                        $this->image_lib->resize();
                    }
                }
            }
编辑

如果要将图像大小限制为特定的
宽度
高度
,请在配置中将
宽度
高度
最小值和最大值设置为相同的值。例如,要将图像大小限制为1024x768,可以在配置中设置首选项,如下所示

$config['min_width']       = '1024';         // restrict width to 1024px
$config['min_height']      = '768';          // restrict height to 768px
$config['max_width']       = '1024';         // restrict width to 1024px
$config['max_height']      = '768';          // restrict height to 768px

$this->load->library('upload', $config);     // pass the config when loading the library
如果使用
自动加载
,请将配置传递给
初始化
方法

$this->upload->initialize($config);
旁注: 我看到您在加载库并初始化时正在传递
config
。这是多余的<如果您正在使用自动加载功能,则使用代码>初始化方法。

编辑

如果要将图像大小限制为特定的
宽度
高度
,请在配置中将
宽度
高度
最小值和最大值设置为相同的值。例如,到r
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
// error
   die("Upload failed with error code " . $_FILES['image']['error']);
} else {
// no error
}
$info = getimagesize($_FILES['image']['tmp_name']);
if ($info === FALSE) {

//Unable to determine image type of uploaded file

}

if (($info[2] !== IMAGETYPE_GIF) && ($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {

// File received is not in valid format

}
$width = $info[0];
$height = $info[1];
if($width!=$height) {
//Image is not square
}
if($width<50) { // Image width should be atleast 50 pixel }