image resize before uploading in CodeIgniter PHP

后端 未结 2 646
忘了有多久
忘了有多久 2021-01-23 10:37

I am having the following block of code in my function:

$target_path = \"uploads/\";

        $target_path = $target_path . basename( $_FILES[\'image\'][\'name\'         


        
2条回答
  •  时光取名叫无心
    2021-01-23 11:07

    Best is to use client side tools to resize the image locally, then upload. You can resize on the server side too (using php gd) but php copies the file in memory and by default, it can only resize images upto +/- 2MB. You can restrict uploading size and resize using php or you can use Flash, Silverlight or JavaApplets.

    Here is another question about client side resizing using flash

    UPDATE:

    Example for serverside resize using CI's Image Manipulation lib

    $config['image_library'] = 'gd2';
    $config['source_image'] = $_FILES['image']['tmp_name'];
    $config['new_image'] = $target_path
    $config['maintain_ratio'] = TRUE;
    $config['width']    = 250;
    $config['height']   = 400;
    
    $this->load->library('image_lib', $config); 
    
    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
    }
    

    More info about the resizing lib

提交回复
热议问题