Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Resizing image files

This PHP function is to resize image in proportion between old size and new specified size without loosing much of image quality. 

It determines if image is landscape oriented, portrait or square and resizes image accordingly.

Function takes path to image, new width and new height as parameters.

<?php
function resize($path, $width = 1000, $height = 460)
{
    //getting image sizes
    $img_sizes = getimagesize($path);
    //olde sizes for calculation
    $oldwidth = $img_sizes[0];
    $oldheight = $img_sizes[1];
    //old sizes for resizing
    $bigw = $oldwidth;
    $bigh = $oldheight;
    //checking if it is image and have size
    if($oldwidth > 0 && $oldheight > 0){
        //if image needs to be rszed
        if($oldwidth > $width || $oldheight > $height){
            //if its landscape
            if($width > $height){
                //reising accordingly
                if($oldwidth > $width && $oldheight <= $height)
                {
                    $oldheight = round(($oldheight * $width)/$oldwidth);
                    $oldwidth = $width;
                }
                else
                {
                    $oldwidth = round(($oldwidth * $height)/$oldheight);
                    $oldheight = $height;
                }
            }
            //portrait or square
            else
            {
                //reising accordingly
                if($oldheight > $height && $oldwidth <= $width)
                {
                    $oldwidth = round(($oldwidth * $height)/$oldheight);
                    $oldheight = $height;
                }
                else
                {
                    $oldheight = round(($oldheight * $width)/$oldwidth);
                    $oldwidth = $width;
                }
            }
        }
        //if image is gif
        if(strtolower($ext[sizeof($ext)-1]) == "gif"){
            $img = imagecreatefromgif($path);
            $tmp_img = imagecreatetruecolor( $oldwidth, $oldheight );
            imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $oldwidth, 
                                $oldheight, $bigw, $bigh );
            //deleting original
            if (file_exists($path)) {unlink($path);}
            imagegif( $tmp_img, $path );
            //if png
        }else if(strtolower($ext[sizeof($ext)-1]) == "png"){
            $img = imagecreatefrompng($path);
            $tmp_img = imagecreatetruecolor( $oldwidth, $oldheight );
            imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $oldwidth, 
                                $oldheight, $bigw, $bigh );
            //deleting original
            if (file_exists($path)) {unlink($path);}
            imagepng( $tmp_img, $path );
            //if jpg
        }else if(strtolower($ext[sizeof($ext)-1]) == "jpg" || 
                 strtolower($ext[sizeof($ext)-1]) == "jpeg")
        {
            $img = imagecreatefromjpeg($path);
            $tmp_img = imagecreatetruecolor( $oldwidth, $oldheight );
            imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $oldwidth, 
                                $oldheight, $bigw, $bigh );
            //deleting original
            if (file_exists($path)) {unlink($path);}
            imagejpeg( $tmp_img, $path );
        }
    }
}
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus