JPG resizer

Discussion in 'PHP' started by Triexa, Apr 13, 2007.

  1. #1
    I made a simple resize class before I decided to just use ImageMagick for my project.

    class ImageResize {
    	function ImageResize($file, $height, $width, $output) {
    		$oldImg = imagecreatefromjpeg($file);
    		$oldWidth = imagesx($oldImg);
    		$oldHeight = imagesy($oldImg);
    		
    		if ($oldWidth > $oldHeight) { 
    			$newWidth = $width;
    			$newHeight = $oldHeight * ($height / $oldWidth); 
    		} 
    		if ($oldWidth < $oldHeight) { 
    			$newWidth = $oldWidth * ($width / $oldHeight); 
    			$newHeight = $height; 
    		} 
    		if ($oldWidth == $oldHeight) { 
    			$newWidth = $width;
    			$newHeight = $height;
    		}
    		
    		$newImg = imagecreatetruecolor($newWidth, $newHeight);
    		imagecopyresampled($newImg, $oldImg, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);
    		return imagejpeg($newImg, $output);
    	}
    }
    PHP:
    (It only resizes JPEGs... I don't think I ever got around to testing it, but if anything is wrong should be something very minor)

    You would use it like so:

    $img = new ImageResize(INPUT_FILE, NEW_WIDTH, NEW_HEIGHT, OUTPUT_FILE);
    PHP:
    It keeps the aspect ratio of the original file.

    Just thought I would put it out there if anyone had any sort of a use for this... I know things like this already exist but this is just a simple, unbloated class to resize an image (heck, it doesn't need to even be a class.. haha)
     
    Triexa, Apr 13, 2007 IP