2016-11-11 115 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工作得很好! :) –