2016-05-27 3 views
1

Wer kann mir helfen Bild max-Größe auf 0 zu setzen, weil ich bin immer Fehlercode: 1CodeIgniter Set Bild max-Größe auf 0 und Dateityp überprüfen, ob jpg oder png

Bitte helfen Sie mir konstruiere diesen Code. Ich will nicht ändern wollen, die Grenze in php.ini nur wissen, ob es einen Code Lösung für dieses Problem ist:

function uploadPhoto() 
{ 
    $control = 'filephoto'; 
    $path = './upload/'; 
    $image = $_FILES[$control]['name']; 
    $size = $_FILES[$control]['size']; 

    if ($imageName = $this->doUpload($control, $path, $image, 'all')) { 

    } else { 

    } 
} 

function doUpload($control, $path, $imageName, $sizes) 
{ 
    if ($this->session->userdata('logged_in')) { 
     $session_data = $this->session->userdata('logged_in'); 
     $u_id   = $session_data['u_id']; 
     $alnum  = random_string('alnum', 16); 
     if (! isset($_FILES[$control]) || ! is_uploaded_file($_FILES[$control]['tmp_name'])) { 
      $this->session->set_flashdata('error_nofile', 'No file was chosen Error code: ' . $_FILES[$control]['error']); 
      header('location:' . base_url() . "indexc/upload/"); 

      return false; 
     } 
     if ($_FILES[$control]['size'] > 2048000) { 
      $this->session->set_flashdata('error_largefile', 'File is too large (' . round(($_FILES[$control]["size"]/1000)) . 'kb), please choose a file under 2,048kb'); 
      header('location:' . base_url() . "indexc/upload/"); 

      return false; 
     } 
     if ($_FILES[$control]['error'] !== UPLOAD_ERR_OK) { 
      $this->session->set_flashdata('error_failed', 'Upload failed. Error code: ' . $_FILES[$control]['error']); 
      header('location:' . base_url() . "indexc/upload/"); 

      Return false; 
     } 
     switch (strtolower($_FILES[$control]['type'])) { 
      case 'image/jpeg': 
       $image = imagecreatefromjpeg($_FILES[$control]['tmp_name']); 
       move_uploaded_file($_FILES[$control]["tmp_name"], $path . $imageName); 
       break; 
      case 'image/png': 
       $image = imagecreatefrompng($_FILES[$control]['tmp_name']); 
       move_uploaded_file($_FILES[$control]["tmp_name"], $path . $imageName); 
       break; 
      case 'image/gif': 
       $image = imagecreatefromgif($_FILES[$control]['tmp_name']); 
       move_uploaded_file($_FILES[$control]["tmp_name"], $path . $imageName); 
       break; 
      default: 
       $this->session->set_flashdata('error_notallowed', 'This file type is not allowed'); 
       header('location:' . base_url() . "indexc/upload/"); 

       return false; 
     } 
     @unlink($_FILES[$control]['tmp_name']); 
     $old_width = imagesx($image); 
     $old_height = imagesy($image); 


     //Create tn version 
     if ($sizes == 'tn' || $sizes == 'all') { 
      $max_width = 600; 
      $max_height = 600; 
      $scale  = min($max_width/$old_width, $max_height/$old_height); 
      if ($old_width > 600 || $old_height > 600) { 
       $new_width = ceil($scale * $old_width); 
       $new_height = ceil($scale * $old_height); 
      } else { 
       $new_width = $old_width; 
       $new_height = $old_height; 
      } 
      $new = imagecreatetruecolor($new_width, $new_height); 
      imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
      switch (strtolower($_FILES[$control]['type'])) { 
       case 'image/jpeg': 
        imagejpeg($new, $path . '' . $u_id . '_' . $alnum . '_' . $imageName, 90); 
        break; 
       case 'image/png': 
        imagealphablending($new, false); 
        imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
        imagesavealpha($new, true); 
        imagepng($new, $path . '' . $u_id . '_' . $alnum . '_' . $imageName, 0); 
        break; 
       case 'image/gif': 
        imagegif($new, $path . '' . $u_id . '_' . $alnum . '_' . $imageName); 
        break; 
       default: 
      } 
     } 

     imagedestroy($image); 
     imagedestroy($new); 
     /* print '<div style="font-family:arial;"><b>'.$imageName.'</b> Uploaded successfully. Size: '.round($_FILES[$control]['size']/1000).'kb</div>'; */ 

     $this->session->set_flashdata('success_upload', '<div style="font-family:arial;"><b>' . $imageName . '</b> Uploaded successfully. Size: ' . round($_FILES[$control]['size']/1000) . 'kb</div>'); 

     $postdata['p_u_id']   = $u_id; 
     $postdata['p_content']  = $this->input->post('content'); 
     $postdata['p_image']   = $u_id . '_' . $alnum . '_' . $imageName; 
     $postdata['p_image_original'] = $imageName; 
     $postdata['p_posted']   = date("Y-m-d H:i:s"); 

     $res = $this->indexm->postinsert_to_db($postdata); 

     header('location:' . base_url() . "indexc/upload/"); 
    } else { 
     $this->load->view('index'); 
    } 
} 

Ich möchte auch überprüfen, ob der Dateityp jpg oder png

ist Bitte Hilf mir!

Dank,

joe

Antwort

3

try Standard codeigniter Bibliothek:

function do_upload() 
    { 
     $config['upload_path'] = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '100'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '768'; 

     $this->load->library('upload', $config); 

     if (! $this->upload->do_upload()) 
     { 
      $error = array('error' => $this->upload->display_errors()); 

      $this->load->view('upload_form', $error); 
     } 
     else 
     { 
      $data = array('upload_data' => $this->upload->data()); 

      $this->load->view('upload_success', $data); 
     } 
    } 
+0

, aber ich möchte die Bildbreite und Höhe ändern. – Joehamir

+0

Referenz-Link: http://stackoverflow.com/questions/19218247/codeignon-image-resize –

Verwandte Themen