<?php
function upload_file($file,$myName,$distination)
{
  $new_name="";
    if(isset($file["name"]))
          {
           $name = $file["name"];
           $size = $file["size"];
           $ext = explode('.', $file["name"]);
           $allowed_ext = array("png", "jpg", "jpeg");
           error_reporting(0);
           if(in_array($ext[1], $allowed_ext))
           {
             if($size < (3000*3000))
              {
                 $new_image = '';
                 $new_name = $myName. '.' . $ext[1];
                 $destination = $distination. $new_name;
                 list($width, $height) = getimagesize($file["tmp_name"]);
               if($ext[1] == 'png')
                 {  $new_image = imagecreatefrompng($file["tmp_name"]); }
               if($ext[1] == 'jpg' || $ext[1] == 'jpeg')
                      {   $new_image = imagecreatefromjpeg($file["tmp_name"]);}
                      $new_width=800;
                      $new_height = ($height/$width)*800;
                      $tmp_image = imagecreatetruecolor($new_width, $new_height);
                      imagecopyresampled($tmp_image, $new_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                      $quality = 90 ;
                      if($size < (512*512)){$quality = 80 ;}else if($size < (800*800)){$quality = 75 ;}
                      else if($size < (1030*1030)){$quality = 70 ;}else {$quality = 60 ;}
                      imagejpeg($tmp_image, $destination,$quality);
                      imagedestroy($new_image);
                      imagedestroy($tmp_image);
                      echo '<img src="'.$destination.'" />';

             }else { echo 'الصورة اكبر من 1.5 ميجا من فضلك اختار صورة أقل';}
           }
          //  else{ echo 'يجب إختيار صورة ..';  }
        }
        // else { echo 'من فضلك إختار صوره'; }
          return $new_name;
     }
 ?>
