2016-04-29 3 views
0

Unten ist der Code, wenn ich versuche mit dem Bild weniger als 1000px Breite funktioniert gut, aber wenn ich versuchte mit mehr als 1000px funktioniert es nicht.Coderigniter Image Resize() funktioniert nicht auf mehr als 1000px Breite von Bild

$img_array = array(); 
    $img_array['image_library'] = 'gd2'; 
    $img_array['maintain_ratio'] = TRUE; 
    $img_array['create_thumb'] = TRUE; 
    $thumb_name = $img_array['new_image'] = './public/image/thumb/' . $file; 
    //you need this setting to tell the image lib which image to process 
    $img_array['source_image'] = $path; 
    $img_array['width'] = 180; 
    $img_array['height'] = 250; 


    $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; 
    } 
    return $thumb_name; 
    } 
+0

den Code versuchen, lassen Sie mich wissen, dass es für Sie arbeitet oder nicht? –

+0

Datei-Upload funktioniert gut, aber Größe ändern geben Fehler "Der Pfad zum Bild ist nicht korrekt. Ihr Server unterstützt nicht die GD-Funktion zur Verarbeitung dieses Bildtyps." –

+0

Überprüfen Sie Ihre Server-Gd-Bibliothek aktiviert ist oder nicht \ –

Antwort

0

erstellen Modellnamen als Image_model in Modelle diesen Code in Ihrem Modell setzen Folder

class Image_model extends CI_Model { 
public function __construct() { 
    parent::__construct(); 
    $this->load->helper('url'); 
    $this->load->library('upload'); 
    $this->load->library('image_lib'); 
} 
public function do_resize($filename) 
{ 

    $source_path = 'uploads/' . $filename; 
    //create a directory thumb in uploads set as target path 
    $target_path = 'uploads/thumb/thumb_'.$filename; 

    $config_manip = array(

     'image_library' => 'gd2', 
     'source_image' => $source_path, 
     'new_image' => $target_path, 
     'maintain_ratio' => TRUE, 
     'width' => 180, 
     'height' => 250 
    ); 
    $this->image_lib->initialize($config_manip); 
    $this->load->library('image_lib', $config_manip); 


    if (!$this->image_lib->resize()) { 
     echo $this->image_lib->display_errors(); 
     die(); 
    } 
    // clear // 
    //$this->image_lib->clear(); 
} 

public function img_upload() 
{ 
    $config = array(
     'upload_path' => "uploads", 
     'allowed_types' => "*", 
     'overwrite' => TRUE, 
     'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
     'max_height' => "3000", 
     'max_width' => "3000" 
    ); 
    $this->upload->initialize($config); 
    $this->load->library('upload', $config); 

    if($this->upload->do_upload()) { 
     $response = array('upload_data' => $this->upload->data()); 
     //here calling the function do_resize 
     $this->do_resize($response['upload_data']['file_name']); 
     //return $response; 
    } 
    /*else{ 
     $error    = array('error'=>$this->upload->display_errors()); 
     //print_r($error);die(); 

    }*/ 
    } 
} 

und rufen Sie die Funktionssteuerung wie dies hier ist der Controller

class your_controller extends CI_Controller { 

function __construct(){ 
    parent::__construct(); 
    $this->load->model('image_model'); 
} 
    // here the function to save name in database 
public function add() { 
     $data      = array(); 
     $config      = array();  
     if(isset($_FILES)){ 
      $config    = $this->image_model->img_upload(); 
      $file_data    = $this->upload->data(); 
      $data['image']   = $file_data['file_name']; 
     } 
      else 
      $data['image']   = "no-image"; 
     // here the function in your model to save the name of image in your database      
     $last_id    =  $this->your_model->save($data); 
     $this->load->view('your_view'); 
    } 
+0

Datei-Upload funktioniert gut, aber Größe ändern geben Fehler " Der Pfad zum Bild ist nicht korrekt. Ihr Server unterstützt die GD-Funktion nicht, die für die Verarbeitung dieses Bildtyps erforderlich ist. " –