如何使用 Codeigniter 调整图像大小

2023-12-08

我正在尝试上传图像并调整图像大小。我想要图像原始图像以及缩略图。

但问题是图像调整大小代码不起作用。

图像上传代码工作正常,它将图像存储在文件夹中,但图像调整大小代码不起作用。

我怎样才能做到这一点 ?

这是我的代码

public function add_images(){

    $this->form_validation->set_rules('description','Description','required');
    $this->form_validation->set_rules('status','Status','required');

    if($this->form_validation->run() == TRUE) {
         // print_r($this->input->post());
        $config['upload_path'] = 'public/img/inner_images/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';   // upload only valid images            
        // update library setting of upload
        $this->load->library('upload', $config);
        //upload image 
        $this->upload->do_upload('image');
        $fInfo = $this->upload->data(); // get all info of uploaded file

        //for image resize
        $img_array = array();
        $img_array['image_library'] = 'gd2';
        $img_array['maintain_ratio'] = TRUE;
        $img_array['create_thumb'] = TRUE;
        //you need this setting to tell the image lib which image to process
        $img_array['source_image'] = $fInfo['full_path'];
        $img_array['width'] = 113;
        $img_array['height'] = 75;

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

        if (!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors(); exit;
        }

        if($fInfo['file_ext'] ==='.gif'||$fInfo['file_ext'] ==='.jpg'|| $fInfo['file_ext'] ==='.jpeg' || $fInfo['file_ext'] ==='.png'|| $fInfo['file_ext'] ===''){
            $insert = array(
                'inner_image' =>$fInfo['file_name'],
                'description' => $this->input->post('description'),
                'status' => $this->input->post('status')
            );
            $check = $this->mdl_inner->add_image($insert);
            if($check){
                $this->session->set_flashdata('success',"Image Added Sucessfully");
                redirect('admin/inner_gallery/add_images/', 'refresh');  
            }
        }else{
            $this->session->set_flashdata('error',"Upload Proper Image Format");
            redirect('admin/inner_gallery/add_images/', 'refresh');
        }
    }
    $arrData['middle'] = 'admin/inner/add_image';
    $this->load->view('admin/template',$arrData);
}

谢谢大家,但我解决了这个问题。 通过在构造函数中加载库

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

之后添加两行代码

 $this->image_lib->clear();
 $this->image_lib->initialize($img_array);

并删除这一行

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

我的最终代码是

public function add_images(){

    $this->form_validation->set_rules('description','Description','required');
    $this->form_validation->set_rules('status','Status','required');

    if($this->form_validation->run() == TRUE) {
         // print_r($this->input->post());
        $config['upload_path'] = 'public/img/inner_images/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';   // upload only valid images            
        // update library setting of upload
        $this->load->library('upload', $config);
        //upload image 
        $this->upload->do_upload('image');
        $fInfo = $this->upload->data(); // get all info of uploaded file

        //for image resize
        $img_array = array();
        $img_array['image_library'] = 'gd2';
        $img_array['maintain_ratio'] = TRUE;
        $img_array['create_thumb'] = TRUE;
        //you need this setting to tell the image lib which image to process
        $img_array['source_image'] = $fInfo['full_path'];
        $img_array['width'] = 113;
        $img_array['height'] = 75;

        $this->image_lib->clear(); // added this line
        $this->image_lib->initialize($img_array); // added this line
        if (!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors(); exit;
        }
        if($fInfo['file_ext'] ==='.gif'||$fInfo['file_ext'] ==='.jpg'|| $fInfo['file_ext'] ==='.jpeg' || $fInfo['file_ext'] ==='.png'|| $fInfo['file_ext'] ===''){
            $insert = array(
                'inner_image' =>$fInfo['file_name'],
                'description' => $this->input->post('description'),
                'status' => $this->input->post('status')
            );
            $check = $this->mdl_inner->add_image($insert);
            if($check){
                $this->session->set_flashdata('success',"Image Added Sucessfully");
                redirect('admin/inner_gallery/add_images/', 'refresh');  
            }
        }else{
            $this->session->set_flashdata('error',"Upload Proper Image Format");
            redirect('admin/inner_gallery/add_images/', 'refresh');
        }
    }
    $arrData['middle'] = 'admin/inner/add_image';
    $this->load->view('admin/template',$arrData);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Codeigniter 调整图像大小 的相关文章

  • 使用 php-ews(Exchange Web 服务)在特定日期后获取电子邮件

    在我的 PHP 脚本中 我需要弄清楚如何检索指定消息 ID 之后或特定日期之后的所有电子邮件 两者都可以 我只需要检索自上次抓取收件箱以来的新电子邮件 这个收件箱每天收到数千封电子邮件 而且我在 30 天内无法删除任何电子邮件 对于初始导入
  • PHP 通过 SSL 连接到 MS SQL

    我想要实现的目标非常简单 我想通过安全连接从 PHP 脚本连接到外部 MS SQL 数据库 然而 这已被证明是有问题的 到目前为止 经过三个小时的研究 我不知所措 客户端的平台是Ubuntu 这意味着我无法使用SQLSRV 安全连接已经在不
  • 按文件名对 $_FILES 进行排序 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 他俩 如您所知 在新的 HTML5 中 您可以非常轻松地上传多个文件 但我这里的问题是如何按列 名称 对 FILES 数组进行排序 这是
  • Yii2 - 错误请求 (#400) |前端和后端cookie

    仅当我打开时才会出现此问题frontend and backend在相同的browser 设想 与后端交互 gt 切换选项卡 gt 与前端交互 gt 切换选项卡返回 gt 与后端交互 gt 错误请求 400 Cookie 后端 identi
  • flutter应用程序中有图像编辑器的api吗?我需要在图像中添加文本

    是否可以编辑图像 例如旋转以及在图像上添加文本 有没有什么插件可以做到这一点 我需要一个图像编辑器来添加具有各种字体和颜色的文本 谢谢 你应该使用重画边界 https docs flutter io flutter widgets Repa
  • Java 旋转图像

    Override public void paintComponent Graphics g super paintComponent g Graphics2D g2 Graphics2D g create rotation of play
  • PHP WCF 集成

    是不是如果我想支持php客户端访问我的服务 我必须有一个基本的http端点 这是因为php仍然只支持soap 1 1吗 据我所知 自从我使用 PHP 以来已经两年了 情况仍然如此 如果客户端应用程序将使用 PHP 的内置 SoapClien
  • PHP:读取所有传入 HTTP 请求的类 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • CakePHP 视图包括其他视图

    我有一个 CakePHP 应用程序 在某些时候会显示带有产品媒体 图片或视频 的视图 我想知道是否有某种方式可以包含另一个威胁视频或威胁图片的视图 具体取决于标志 我想将这些 小视图 用于其他几个目的 所以它应该 像 蛋糕组件一样 以便重用
  • PDO PHP 连接,致命错误

    我的连接类 firstcode php class DB functions public db function construct try db new PDO mysql localhost dbname xxx charset ut
  • 使用 PHP glob 列出 FTP 服务器上的文件不起作用

    我使用此代码来访问目录 location files pictures glob location png 我想使用 FTP 访问远程路径 location opendir ftp user password host name files
  • Facebook PHP-SDK 页面刷新后似乎丢失了 userID

    我似乎登录工作正常 我可以登录 接受应用程序 第一次 然后显示用户信息 例如姓名 图片 等 然而 当我刷新页面时 userid 又回到 0 我必须再次登录 我不确定问题是什么 我必须在每次页面加载时重新启动它还是什么 我不知道 我会发布一些
  • Nginx 502 网关错误。通过增加buffer来解决。为什么?

    我正在设置 LEMP 堆栈来运行 Drupal 我安装了 Nginx 和 PHP FastCGI Nginx 工作正常 但任何运行 PHP 的尝试都会出现错误 502 Bad Gateway 谷歌很快发现 nginx 502 错误网关 ht
  • PHP 编码风格回归;在开关/外壳中

    我们正在尝试为我们的团队实施新的编码风格指南 当未找到 break 时 php codeniffer 会在 switch case 语句上打印警告 如下所示 switch foo case 1 return 1 case 2 return
  • CodeIgniter:My_Lang 中的 get_instance

    我发现这个有用的国际化代码 http pastebin com SyKmPYTX http pastebin com SyKmPYTX 一切正常 除了我无法在此类中使用 CI 函数 我想从 DB 设置 languages 和 special
  • php - 解析html页面

    div divbox div p para1 p p para2 p p para3 p table class table tr td td tr table p para4 p p para5 p 有人可以告诉我如何解析这个 html
  • Jquery一键提交多个同名表单

    我有动态创建的循环表单 我需要一键提交所有表单 我正在遵循下面的代码 你能建议我怎么做吗 谢谢
  • 使用会话 php 创建 cookie?

    我使用会话来登录我网站中的用户 问题是 我想让用户remember密码 因此关闭 打开浏览器后他们不需要再次登录 我需要使用 cookie 和 session 来实现它吗 my code user POST user pass POST p
  • 在本地 SDK 服务器上工作时,实时 Google App Engine 上出现 404

    我已经在GAE标准环境上部署了几个PHP应用程序 一切正常 现在我正在部署一个新应用程序 该应用程序位于由gcloudSDK按预期工作 终端命令 dev appserver py log level warning app yaml 问题是
  • PayPal 网关已拒绝请求。安全标头无效(#10002:安全错误 Magento

    在 magento 中增加 PayPal 预付款 我已填写 magento admin 中的所有凭据 但是当我进入前端并单击 pay pal 按钮时 它给出了 PayPal 网关已拒绝请求 安全标头无效 10002 安全错误 我用谷歌搜索了

随机推荐