GORAGOD.com

freelance, web developer, web designer, hosting, domain name

อาจารย์ช่วยดูโค๊ด Resize รูปภาพให้หน่อยคับ

   


    $photo=$_FILES['img_prd']['tmp_name'];
     $photo_name=$_FILES['img_prd']['name'];
     $photo_size=$_FILES['img_prd']['size'];
     $photo_type=$_FILES['img_prd']['type'];
    
 if (( ereg("^image/pjpeg",$photo_type))or(ereg("^image/gif",$photo_type))){
      }else{
       if($photo<>''){
        unlink($photo);
       }
       echo "<br><br><br><div align=\"center\"><H3 class=\"txtred\">ERROR : *.jpg or *.gif only !!! </H3><br><br><br>";
die();
      }
     $passw = random_password(7);
      $filenewcon = strstr($photo_name,'.');
      $now = date("Dgis");
      $img_prd = "$passw$now$filenewcon";
      $original_photo = $photo;
      $uploaddir = $dirprd;
      $w_want = $w_prd;
      $h_want = $h_prd;
      $thumbdir = $dirprdr;
      $w_want_re = $w_prdr;
      $h_want_re = $h_prdr;
      $uploadfile = $uploaddir. basename($img_prd);
      $thumbfile = $thumbdir. basename($img_prd);
      list($w_original,$h_original) = getimagesize($original_photo);
if(($w_original<$w_want)and($h_original<$h_want)){  
        $w_edit=$w_original;
        $h_edit=$h_original;
       }else{
        if($w_original>=$w_want){
         $w_edit=$w_want;
         $percent_edit =$w_edit/$w_original;
         $h_edit=$h_original*$percent_edit;
         if($h_edit>$h_want){
          $h_edit=$h_want;
          $percent_edit =$h_edit/$h_original;
          $w_edit=$w_original*$percent_edit;
         }
        }else if($h_original>=$h_want){
         $h_edit=$h_want;
         $percent_edit =$h_edit/$h_original;
         $w_edit=$w_original*$percent_edit;
         if($w_edit>$w_want){
          $w_edit=$w_want;
          $percent_edit =$w_edit/$w_original;
          $h_edit=$h_original*$percent_edit;
         }
        }
       }
       $img_e1=imagecreatetruecolor($w_edit,$h_edit);
       if (ereg ("^image/pjpeg",$photo_type)) {
        $img_e2=imagecreatefromjpeg($original_photo);
       }else if (ereg ("^image/gif",$photo_type)) {
        $img_e2=imagecreatefromgif($original_photo);
       }
       imagecopyresampled($img_e1,$img_e2,0,0,0,0,$w_edit,$h_edit,$w_original,$h_original);
       imagejpeg($img_e1, $uploadfile, $quality);
       imagedestroy($img_e1);
       imagedestroy($img_e2);
      //resize normal photo
      //resize thumb photo
       if(($w_original<$w_want_re)and($h_original<$h_want_re)){
        $w_edit=$w_original;
        $h_edit=$h_original;
       }else{
        if($w_original>=$w_want_re){  
         $w_edit=$w_want_re;
         $percent_edit =$w_edit/$w_original;
         $h_edit=$h_original*$percent_edit;
         if($h_edit>$h_want_re){
          $h_edit=$h_want_re;
          $percent_edit =$h_edit/$h_original;
          $w_edit=$w_original*$percent_edit;
         }
        }else if($h_original>=$h_want_re){
         $h_edit=$h_want_re;
         $percent_edit =$h_edit/$h_original;
         $w_edit=$w_original*$percent_edit;
         if($w_edit>$w_want_re){
          $w_edit=$w_want_re;
          $percent_edit =$w_edit/$w_original;
          $h_edit=$h_original*$percent_edit;
         }
        }
       }
       $img_e1=imagecreatetruecolor($w_edit,$h_edit);
       if (ereg ("^image/pjpeg",$photo_type)) {
        $img_e2=imagecreatefromjpeg($original_photo);
       }else if (ereg ("^image/gif",$photo_type)) {
        $img_e2=imagecreatefromgif($original_photo);
       }
       imagecopyresampled($img_e1,$img_e2,0,0,0,0,$w_edit,$h_edit,$w_original,$h_original);
       imagejpeg($img_e1, $thumbfile, $quality);
       imagedestroy($img_e1);
       imagedestroy($img_e2);
11 ส.ค. 2552 1 2,836

       if (ereg ("^image/pjpeg",$photo_type)) {
        $img_e2=imagecreatefromjpeg($original_photo);
       }else if (ereg ("^image/gif",$photo_type)) {
        $img_e2=imagecreatefromgif($original_photo);
       }

2 คำสั่งนี้อาจผิดพลาด

type อาจ ไม่ได้อยู่ใน 2 ตัวนี้ ทำให้ $img_e2 ไม่ถูกต้อง

โค้ด resize image มีบนเว็บ ลองหาดู หรือไม่ก็ดูใน GCMS
#1
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 2M
^