在codeigniter中上传时,我必须压缩图像大小而不改变高度和宽度

时间:2017-06-22 13:43:59

标签: php image codeigniter codeigniter-2 codeigniter-3

我一次上传图片数量,因此上传时间太长,因此我想在上传时压缩图片尺寸(不改变高度和宽度)

所以请建议我更改我的代码。

我的代码段:

if($_FILES['userfile']['name'] != ""){


$allowed =  array('jpg','jpeg' ,'gif','png');
$filename = $_FILES['userfile']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);


    $testimg =$_FILES["userfile"]["name"];

      $ppfilename = preg_replace("/[^A-Za-z0-9_\w]+/", "", $testimg).".".$ext;

      $ppfilename = $ppfilename;
      $ppfilename = explode(".", $ppfilename);
      $ppnew_filename = "ad_" . $ppfilename[0] . time() . "." . $ppfilename[1];


      $ppthumb = explode(".", $ppnew_filename);
      $ppthumb = $ppthumb[0] . "_thumb". "." . $ppthumb[1];

      $_FILES["userfile"]["name"] = $ppnew_filename;
      $this->load->library("upload");

      $this->upload->initialize($this->set_upload_options()); 

      if($this->upload->do_upload()){
          $ppimagedata = $this -> upload -> data();
          $ppnewimagename = $ppimagedata["file_name"];

          $this -> load -> library("image_lib");
          $config['image_library']   = 'gd2';
          $config['source_image']    = $ppimagedata["full_path"];
          $config['create_thumb']    = TRUE;
          $config['maintain_ratio']  = TRUE;
          $config['new_image']       = './uploads/Property/100X100/';
          $config['width']           = 100;
          $config['height']          = 100;

          $this -> image_lib -> initialize($config);
          $this -> image_lib -> resize();

          $config['new_image']    = './uploads/Property/300X300/';
          $config['width']       = 300;
          $config['height']       = 300;
          $this -> image_lib -> initialize($config);
          $this -> image_lib -> resize();

           }   

        }
        else{
          $ppnewimagename = "";
          $ppthumb = "";
        }
}

1 个答案:

答案 0 :(得分:0)

您必须使用图片处理库,然后使用质量配置

指向文档的链接:https://www.codeigniter.com/userguide3/libraries/image_lib.html#preferences