image.php 1.94 KB
<?php
        function upload_ImageResize($upload,$option){

   $type = substr(strrchr($upload['name'],"."),1);
   $newWidth = $option["width"];
   $newHeight = $option["height"];
   $nameFile = mktime() . "-" .rand(1,10000). "-" .rand(1,10000) . "." . $type;
   $save_image = $option["upload_path"] . $nameFile;
   $img = $upload["tmp_name"];

switch($type){
case "jpg":
   $function_image_create = "ImageCreateFromJpeg";
   $function_image_new = "ImageJpeg";
case "jpeg":
   $function_image_create = "ImageCreateFromJpeg";
   $function_image_new = "ImageJpeg";
break;
case "png":
   $function_image_create = "ImageCreateFromPng";
   $function_image_new = "ImagePNG";
break;
case "gif":
   $function_image_create = "ImageCreateFromGif";
   $function_image_new = "ImageGif";
break;
default:
   $function_image_create = "ImageCreateFromJpeg";
   $function_image_new = "ImageJpeg";
break;
}


   $srcImage = @$function_image_create($img);

   $srcWidth  = ImageSX($srcImage);
   $srcHeight = ImageSY($srcImage);


  if ( ($newWidth < $srcWidth) || ($newHeight < $srcHeight) ) {
   if( $srcWidth < $srcHeight ){
       $destWidth  = $newWidth * $srcWidth/$srcHeight;
       $destHeight = $newHeight;
   }else{
       $destWidth  = $newWidth;
       $destHeight = $newHeight * $srcHeight/$srcWidth;
   }
  }else{ $destWidth = $srcWidth;$destHeight = $srcHeight;}



   $destImage = imagecreatetruecolor($destWidth, $destHeight);

   ImageCopyResampled( $destImage, $srcImage, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight );
           /*
   if(isset($option['str']) && strlen($option['str'])>0){
    $colorGrey=imagecolorallocate($destImage, 192, 192, 192);
    $height_str = $destHeight-7;
    imagettftext($destImage,15,0,7,$height_str,$colorGrey,"{$_SERVER['DOCUMENT_ROOT']}/libs/fonts/ds_zombi.ttf",$option['str']);
   }   */
  @$function_image_new($destImage,$save_image,100);
  

   ImageDestroy( $srcImage  );
   ImageDestroy( $destImage );

return $nameFile;
}
?>