All pastes #719171 Raw Edit

Miscellany

public text v1 · immutable
#719171 ·published 2007-09-29 00:09 UTC
rendered paste body
//RETURN RESIZED IMAGE FOR GIVEN WIDTH AND HEIGHT
function resize_crop($name,$filename,$new_w,$new_h)
{
    $system= substr(strrchr($name, "."), 1 );
   if (preg_match('/jpg|jpeg/',$system))
      {
         $src_img=imagecreatefromjpeg($name);
      }
   else if (preg_match('/png/',$system))
      {
         $src_img=imagecreatefrompng($name);
      }
   else if (preg_match('/gif/',$system))
      {
         $src_img=imagecreatefromgif($name);
      }
   else $src_img=imagecreatefromjpeg($name);   

   $old_x=imagesx($src_img);
   $old_y=imagesy($src_img);
   if ($old_x > $old_y)
   {
      $thumb_w=$old_x*($new_h/$old_y);
      $thumb_h=$new_h;
      if($thumb_w<$new_w)
         {
            $diff=($new_w-$thumb_w);
            $thumb_w=$new_w;
            $thumb_h=($new_h+$diff);
          }
      }
   if ($old_x < $old_y)
   {
      $thumb_w=$new_w;
      $thumb_h=$old_y*($new_w/$old_x);
      if($thumb_h<$new_h)
         {
            $diff=($new_h-$thumb_h);
            $thumb_h=$new_h;
            $thumb_w=($new_w+$diff);
         }
   }

   if ($old_x == $old_y)
   {
      if($new_w>$new_h)
      {
         $thumb_w=$new_w;
         $thumb_h=$new_w;
      }
      else if($new_w<$new_h)
      {
         $thumb_w=$new_h;
         $thumb_h=$new_h;
      }
      else
      {
      $thumb_w=$new_w;
      $thumb_h=$new_h;
      }
   }

   $dst_img= imagecreatetruecolor($thumb_w,$thumb_h);
     if($system=="gif")
     {
       $transparent = imagecolorallocate($dst_img, "255", "255", "255");
       imagefill($dst_img, 0, 0, $transparent);
     }
   imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
   if (preg_match("/png/",$system))
      {
         imagepng($dst_img,$filename);
      }
   else if (preg_match('/gif/',$system))
      {
         imagegif($dst_img,$filename);
      }
   else
      {
         imagejpeg($dst_img,$filename);
      }
   imagedestroy($dst_img);
   imagedestroy($src_img);
}