diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2011-06-21 15:08:25 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net ; true> | 2011-06-21 15:08:25 -0700 |
commit | 7de4c79febccde0f1855f5ab9c3c32700549db53 (patch) | |
tree | 2bb8077386df8f299e96f2fea0ca10b3af56160d | |
parent | 84a9200647ea9e8f4d65e8d76bd263f4e82a922d (diff) |
Automatically resize images
-rw-r--r-- | htdocs/new-post.php | 3 | ||||
-rw-r--r-- | htdocs/src/Post.inc.php | 40 |
2 files changed, 33 insertions, 10 deletions
diff --git a/htdocs/new-post.php b/htdocs/new-post.php index 1bcdc89..9082102 100644 --- a/htdocs/new-post.php +++ b/htdocs/new-post.php @@ -190,7 +190,8 @@ function handle_images() { $post = $_SESSION['newpost']; // Display image form - echo "<p>You may upload up to four images with your post.</p>"; + echo "<p>You may upload up to four images with your post." + . " Valid image types are JPEG and PNG.</p>"; form_start('source'); 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 |