2016-05-27 10 views
1

誰もが、私はエラーコードを取得していますので0 に画像の最大サイズを設定するために私を助けることができる場合は、ファイルの種類を確認しますこのコードを作る。私はまた、ファイルの種類がjpgまたはCodeIgniterでは、0に画像の最大サイズを設定し、JPGまたはPNG

pngであるかどうかを検証したい

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'); 
    } 
} 

してください:私は、単にコードのこの問題に対する解決策があるかどうかを知りたいphp.iniに制限を変更する必要はありません助けて!

おかげで、

ジョー

答えて

3

トライデフォルトのCodeIgniterのライブラリ:

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

が、私は画像の幅と高さのサイズを変更したいです。 – Joehamir

+0

参照リンク: http://stackoverflow.com/questions/19218247/codeigniter-image-resize –

関連する問題