Resize Images Using PHP


Below one is the simple PHP Script which proportionally resize an image to a max height and width then display it.

<?php

// Max height and width

$max_width = 400; //  Enter your Desire Width

$max_height = 400; //  Enter your Desire Height



// Path to your jpeg



$upfile = 'test.jpeg'; // Give Image path here

    Header("Content-type: image/jpeg");

  

    $size = GetImageSize($upfile); // Read the size

          $width = $size[0];

          $height = $size[1];

        

          // Proportionally resize the image to the

          // max sizes specified above

        

          $x_ratio = $max_width / $width;

          $y_ratio = $max_height / $height;



          if( ($width <= $max_width) && ($height <= $max_height) )

          {

               $tn_width = $width;

               $tn_height = $height;

          }

          elseif (($x_ratio * $height) < $max_height)

          {

               $tn_height = ceil($x_ratio * $height);

               $tn_width = $max_width;

          }

          else

          {

               $tn_width = ceil($y_ratio * $width);

               $tn_height = $max_height;

          }

     // Increase memory limit to support larger files

    

     ini_set('memory_limit', '32M');

    

     // Create the new image!

     $src = ImageCreateFromJpeg($upfile);

     $dst = ImageCreateTrueColor($tn_width, $tn_height);

     ImageCopyResized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);

     ImageJpeg($dst);

// Destroy the images

ImageDestroy($src);

ImageDestroy($dst);

?>

Above function will work only for jpg and jpeg images only. For other image types (.png or .gifs), you have to change some lines from above code.

While the below function works well for all the general type of images (.jpg, .gif, .png).

<?php

// Resize image.

// Writeen By: Vijay M Rami

// Mails vijaymrami@gmail.com

// This code is written to only execute on  jpg,gif,png     

// $picname = resizepics('pics', 'new widthmax', 'new heightmax');

// Demo  $picname = resizepics('stihche.jpg', '180', '140');

$picname = resizepics('test.jpg', '400', '400'); // Give your large image file path.

echo $picname;

//Error

die( "<font color=\"#FF0066\"><center><b>File not exists :(<b></center></FONT>");

//Funcion resizepics

function resizepics($pics, $newwidth, $newheight){

     if(preg_match("/.jpg/i", "$pics")){

           header('Content-type: image/jpeg');

     }

     if (preg_match("/.gif/i", "$pics")){

           header('Content-type: image/gif');

     }

     list($width, $height) = getimagesize($pics);

     if($width > $height && $newheight < $height){

       $newheight = $height / ($width / $newwidth);

     } else if ($width < $height && $newwidth < $width) {

       $newwidth = $width / ($height / $newheight);   

     } else {

       $newwidth = $width;

       $newheight = $height;

    }

    if(preg_match("/.jpg/i", "$pics")){

    $source = imagecreatefromjpeg($pics);

    }

    if(preg_match("/.jpeg/i", "$pics")){

    $source = imagecreatefromjpeg($pics);

    }

    if(preg_match("/.jpeg/i", "$pics")){

    $source = Imagecreatefromjpeg($pics);

    }

    if(preg_match("/.png/i", "$pics")){

    $source = imagecreatefrompng($pics);

    }

    if(preg_match("/.gif/i", "$pics")){

    $source = imagecreatefromgif($pics);

    }

    $thumb = imagecreatetruecolor($newwidth, $newheight);

    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    return imagejpeg($thumb);

    if(preg_match("/.jpg/i", "$pics")){

    return imagejpeg($thumb);

    }

    if(preg_match("/.jpeg/i", "$pics")){

    return imagejpeg($thumb);

    }

    if(preg_match("/.jpeg/i", "$pics")){

    return imagejpeg($thumb);

    }

    if(preg_match("/.png/i", "$pics")){

    return imagepng($thumb);

    }

    if(preg_match("/.gif/i", "$pics")){

    return imagegif($thumb);

    }

}

?>

Post a Comment

0 Comments