Resizing images with PHP

7 Apr 2010 | Posted by: Paul Whittington
Resizing images with PHP

I wrote this function a while back that rezies images using the GDlib functionality that is present in most PHP installs

function resizeImage($imageW, $imageH, $imageIn, $imageOut, $method = "fit", $cropArray=array(), $fillColour="FFFFFF") {
    //methods =
    //fit = the image fits into the size without distortion usually making it smaller than one of the dimensions
    //crop = the image is chopped off so that it fits into the box usually resulting in a loss of image in one plane or the other
    //scale = the image is distorted and fits the box exactly
    //custom is used ic conjuction with the cropper to select a specific area
    $lastSlash = strrpos($imageOut, "/");
    if ($lastSlash!==false) {
        checkDirectoryExists (substr ($imageOut, 0, $lastSlash + 1));
    }
    $fileType = strtolower (substr($imageIn, strlen($imageIn)-3));
    if (($fileType=="gif"||$fileType=="jpg"||$fileType="png")&&file_exists ($imageIn)) {
        $imageSize = @getimagesize($imageIn);
        if ($imageSize!==false) {
            list($originalImageW, $originalImageH) = $imageSize;
            if ($originalImageW!=0&&$originalImageH!=0) {
                $targetX = 0;
                $targetY = 0;
                $sourceX = 0;
                $sourceY = 0;
                $targetW = $imageW;
                $targetH = $imageH;
                $sourceW = $originalImageW;
                $sourceH = $originalImageH;            
                        
                switch ($method) {
                    case "distort": // sizes are as they came in
                        break;    
                    case "fixedWidth":
                        $imageH = (int)($imageW * ($sourcwH/$sourceW));
                        if ($imageH>1000) {$imageH = 1000;}
                        break;
                    case "custom": // used for the image manager
                        if (count($cropArray)!=8) {print "ERROR: wrong image crop parameters"; exit;}
                        list ($targetX, $targetY, $sourceX, $sourceY, $targetW, $targetH, $sourceW, $sourceH) = $cropArray;
                        break;
                    case "fitWithin":
                    case "fit":
                        if ($sourceW>$targetW||$sourceH>$targetH) {
                            if ($targetW/$targetH > $sourceW/$sourceH) {                        
                                $imageW = (int)(($targetH / $originalImageH) * $originalImageW);
                                $targetW = $imageW;
                            } else {
                                $imageH = (int)(($targetW / $originalImageW) * $originalImageH);
                                $targetH = $imageH;
                            }
                        } else {
                            $imageW=$sourceW;
                            $imageH=$sourceH;
                            $targetW=$sourceW;
                            $targetH=$sourceH;
                        }
                        break;
                    default: // crop
                        if ($imageW/$imageH > $originalImageW/$originalImageH) {                    
                            $sourceY = round ((($originalImageH - (($originalImageW / $imageW) * $imageH)) / 2), 0);
                            $sourceH = $originalImageH - ($sourceY * 2);
                        } else {                        
                            $sourceX = round ((($originalImageW - (($originalImageH / $imageH) * $imageW)) / 2), 0);
                            $sourceW = $originalImageW - ($sourceX * 2);
                        }
                        break;
                }            
                $image_out = imagecreatetruecolor($imageW, $imageH);
                
                $fillColourHandle = imagecolorallocate($image_out, hexdec('0x'.$fillColour{0}.$fillColour{1}), hexdec('0x'.$fillColour{2}.$fillColour{3}), hexdec('0x'.$fillColour{4}.$fillColour{5}));
                imagefill($image_out, 0, 0, $fillColourHandle);
                        
                switch ($fileType) {
                case "gif":
                    $image_in = imagecreatefromgif($imageIn);
                    imagecopyresampled($image_out, $image_in, $targetX, $targetY, $sourceX, $sourceY, $targetW, $targetH, $sourceW, $sourceH);
                    imagegif($image_out, $imageOut);
                    break;
                case "jpg":
                    $image_in = imagecreatefromjpeg($imageIn);
                    imagecopyresampled($image_out, $image_in, $targetX, $targetY, $sourceX, $sourceY, $targetW, $targetH, $sourceW, $sourceH);
                    imagejpeg($image_out, $imageOut, 90);
                    break;
                case "png":
                    $image_in = imagecreatefrompng($imageIn);
                    imagecopyresampled($image_out, $image_in, $targetX, $targetY, $sourceX, $sourceY, $targetW, $targetH, $sourceW, $sourceH);
                    imagepng($image_out, $imageOut, 0); // there is another setting that can be used here for filters
                    break;
                }
                return true;
            }
        }
    }
    return false;
}

All you need to do is call the function passing it the parameters in the following order

$imageW = the width in pixels ot the output file
$imageH = the height in pixels of the output file
$imageIn = the path to the source image, this is the internal path not a url
$imageOut = the path to the output image, this is the internal path not a url this will overwrite anything alrady at this location
$method = how should the image be positioned, there are currently four supported methods:

  • fit = the image is scaled so that it fits within the given sizes.if the image is smaller in both dimensions it is not scaled up.
  • crop = the image is chopped off so that it fits into the box usually resulting in a loss of image in one plane or the other
  • scale = the image is distorted and fits the box exactly
  • custom = an 8 part array with custom specs is passed as the next parameter to precisely control the output. I use this in conjustion with a javascript driven cropper script. the array is in the form: x-coordinate of destination point, y-coordinate of destination point, x-coordinate of source point, y-coordinate of source point, Destination width, Destination height, Source width, Source height, 

$cropArray = used with the custom method above
$fillColour = used with the custom method above, allowing the background to be set if the specified position of the source image doesn't fully cover the target image.

 

 


 

Please rate this article

Click the stars below to give this article a mark out of 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 5 / 10


Post your comments...

We would really appreciate any comments or additions that you have. Include a link in your comment and if we think your comment is appropriate we will publish it. If found this article in any way useful we would really appreciate you bookmarking the page with any of the social bookmarking links provided.



Name:
(optional, shown on site)
Email:
(optional, never shown on site)
Code:
(case sensitive)
captcha
Your feedback: