2016-11-11 227 views
0

我想編寫一個php代碼,用於調整從數據庫中檢索的圖像的大小並將其上傳到網站中。但我無法調整圖像大小。這被直接上傳到網站,而調整大小。我的代碼如下提前..thanks給...調整圖像大小並上傳

public function editimg()`enter code here` 
    { 
    $config['image_library'] = 'gd2'; 
     $id=$this->input->post('hiddenimgid');  
     $config['upload_path'] = './assets/img/movies'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '150'; 
     $config['max_width'] = '199'; 
     $config['max_height'] = '199'; 
    //echo "hii";die; 
    // print_r($config);die; 
     $this->load->library('upload', $config); 
     if (! $this->upload->do_upload()) 
     { 
      //echo "not uploaded";die; 
      $error = array('error' => $this->upload->display_errors()); 
$data['message'] = "Image Cannot be Uploaded,Try Again !!"; 
      $data['error']=$error; 

     $this->index_one($data); 

     } //if 

     else 
     { 
      //echo "hii";die; 
      $config['image_library'] = 'gd2'; 
      $image_data   = $this->upload->data(); 
      $filename   = $image_data['file_name']; 
      $source_path  = 'assets/img/movies/'.$filename ; 
      $new_image_path  = 'assets/img/movies/'.$image_data['raw_name'].'_thumb'.$image_data['file_ext']; //name of resized image 
      $target_path  = 'assets/img/movies/'; 
      $thumb['image_library'] = 'gd2'; 
      $thumb['source_image'] = $source_path; 
      $thumb['new_image']  = $target_path; 
      $thumb['create_thumb'] = TRUE; 
      $thumb['maintain_ratio'] = TRUE; 
      $thumb['width'] = 140; 
      $thumb['height'] = 200; 
      //print_r($thumb);die; 
      //$this->load->library('image_lib', $config); 
      $this->load->library('image_lib', $thumb); 



        if (!$this->image_lib->resize()) 
        { 
         //echo "hii";die; 
         //$this->session->set_flashdata('errors', $this->image_lib->display_errors('', '')); 
         $this->session->set_flashdata('errors', $error= $this->image_lib->display_errors('', '')); 
         print_r($error);die; 
        } 
        else 
        { 
         //echo "hii";die; 
         //print_r($thumb);die; 
         $data=array(

         'image'=>$new_image_path 
         ); 
         //print_r($new_image_path);die; 
         if($this->movies_model->updateMovies($data,$id))   
         { 
          $data['message'] = "Movies added successfully !!"; 
          //$this->index_one($data); 
          redirect("movies/index", 'refresh'); 
          //print_r($thumb);die; 
         } 
         else 
         { 
         $data['message'] = "not_uploaded !!"; 
         $this->index_one($data); 
         } 
        } 
     } 
} 

`

+0

你可以使用簡單的圖像大小調整代碼。你可以從https://github.com/claviska/SimpleImage –

+0

得到代碼抱歉,但沒有找到任何代碼調整大小... – shireen

回答

0

它使用簡單,簡單的圖像

<?php 

include('src/abeautifulsite/SimpleImage.php'); 

    try { 
    $img = new abeautifulsite\SimpleImage('/*image name*/'); 
    $img->resize(320, 200)->save('/*name of resize image*/');resize image and save resized image 

} catch(Exception $e) { 
    echo 'Error: ' . $e->getMessage(); 
} 
0

奧萊是正確的話,如果你正在努力調整圖像大小,最好使用庫來處理所有的邊緣情況和陷阱。

下面是如何與SimpleImage 2.X做到這一點:

$image = new \abeautifulsite\SimpleImage('image.jpg'); 
$image->resize(320, 200)->save('result.jpg'); 

而且SimpleImage 3.X:

$image = new \claviska\SimpleImage('image.jpg'); 
$image->resize(320, 200)->toFile('output.jpg'); 

的SimpleImage庫可以與作曲安裝:

composer require claviska/simpleimage 

或下載from GitHub並手動包含。

+0

嗨科裏,已經DM你在Twitter上,但我也會在這裏嘗試! toFile()返回一個SimpleImage對象,該如何將其轉換爲常規的$ _FILE變量? 我需要的僅僅是在操作後上傳到我的AWS S3存儲桶,但AWS Sdk使用$ _FILE作爲輸入 –

+1

不要試圖模擬$ _FILE的行爲。 AWS SDK允許您使用putObject()和許多選項。如果要將輸出寫入文件,請使用帶有字符串的putObject作爲「Body」參數。例如$ client-> putObject(['Body'=> file_get_contents('file.ext'),...) – claviska

+0

謝謝! @ claviska工作得很好! :) –