From 7de4c79febccde0f1855f5ab9c3c32700549db53 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Tue, 21 Jun 2011 15:08:25 -0700 Subject: Automatically resize images --- htdocs/src/Post.inc.php | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'htdocs/src') diff --git a/htdocs/src/Post.inc.php b/htdocs/src/Post.inc.php index 4386546..2eef773 100644 --- a/htdocs/src/Post.inc.php +++ b/htdocs/src/Post.inc.php @@ -11,12 +11,13 @@ require_once "base.inc.php"; class Post { + const MAX_IMAGE_DIM = 600; + private $info; private $indatabase = false; private $images; private $category; - public function __construct($info=null) { $this->info = is_null($info) ? array() : $info; @@ -267,28 +268,49 @@ class Post { $info = @getimagesize($file); if (!$info) { + // Quietly ignore that the file isn't an image. return false; } - // TODO Verify image dimensions? - if ($info[0] > self::MAX_IMAGE_WIDTH - or $info[1] > self::MAX_IMAGE_HEIGHT) { + // Resize Images if needed + if ($info[0] > self::MAX_IMAGE_DIM + or $info[1] > self::MAX_IMAGE_DIM) { $ratio = $info[0] / $info[1]; if ($ratio > 1) { // Width limited - $width = min($info[0], self::MAX_IMAGE_WIDTH); - $height = $info[1] / $ratio; + $width = min($info[0], self::MAX_IMAGE_DIM); + $height = $info[1] * $width / $info[0]; } else { // Height limited - + $height = min($info[1], self::MAX_IMAGE_DIM); + $width = $info[0] * $height / $info[1]; } - $width = min($info[0], self::MAX_IMAGE_WIDTH); - $height = min($info[1], self::MAX_IMAGE_HEIGHT); + switch ($info[2]) { + case IMAGETYPE_JPG: + $image = imagecreatefromjpeg($file); + break; + + case IMAGETYPE_PNG: + $image = imagecreatefrompng($file); + break; + + default: + // We quietly ignore if the image is not a valid type. + return false; + } + + $newimage = imagecreatetruecolor($width, $height); + + imagecopyresampled($newimage, $image, 0, 0, 0, 0, $width, $height, $info[0], $info[1]); + + imagejpeg($newimage, $file); + imagedestroy($newimage); + imagedestroy($image); } // Get image id -- cgit v1.2.3