summaryrefslogtreecommitdiffstats
path: root/application
diff options
context:
space:
mode:
Diffstat (limited to 'application')
-rw-r--r--application/controllers/file.php12
-rw-r--r--application/core/MY_Controller.php1
-rw-r--r--application/errors/error_general.php5
-rw-r--r--application/libraries/Customautoloader.php26
-rw-r--r--application/libraries/Image.php267
-rw-r--r--application/models/mfile.php109
-rw-r--r--application/views/file/fragments/thumbnail.php3
-rw-r--r--application/views/header.php1
8 files changed, 309 insertions, 115 deletions
diff --git a/application/controllers/file.php b/application/controllers/file.php
index 329a0bdf7..b81900af2 100644
--- a/application/controllers/file.php
+++ b/application/controllers/file.php
@@ -168,6 +168,7 @@ class File extends MY_Controller {
case "image/png":
case "image/gif":
$filedata["tooltip"] = $this->_tooltip_for_image($filedata);
+ $filedata["orientation"] = libraries\Image::get_exif_orientation($file);
$this->output_cache->add_merge(
array("items" => array($filedata)),
'file/fragments/thumbnail'
@@ -317,12 +318,12 @@ class File extends MY_Controller {
private function _tooltip_for_image($filedata)
{
$filesize = format_bytes($filedata["filesize"]);
- $dimensions = $this->mfile->image_dimension($this->mfile->file($filedata["hash"]));
+ list($width, $height) = getimagesize($this->mfile->file($filedata["hash"]));
$upload_date = date("r", $filedata["date"]);
$tooltip = "${filedata["id"]} - $filesize<br>";
$tooltip .= "$upload_date<br>";
- $tooltip .= "$dimensions - ${filedata["mimetype"]}<br>";
+ $tooltip .= "${width}x${height} - ${filedata["mimetype"]}<br>";
return $tooltip;
}
@@ -514,9 +515,11 @@ class File extends MY_Controller {
$cache_key = $filedata['hash'].'_thumb_'.$thumb_size;
- $thumb = cache_function($cache_key, 100, function() use ($id, $thumb_size){
+ $thumb = cache_function($cache_key, 100, function() use ($filedata, $thumb_size){
$CI =& get_instance();
- $thumb = $CI->mfile->makeThumb($id, $thumb_size, IMAGETYPE_JPEG);
+ $img = new libraries\Image($this->mfile->file($filedata["hash"]));
+ $img->makeThumb($thumb_size, $thumb_size);
+ $thumb = $img->get(IMAGETYPE_JPEG);
if ($thumb === false) {
show_error("Failed to generate thumbnail");
@@ -551,6 +554,7 @@ class File extends MY_Controller {
continue;
}
$query[$key]["tooltip"] = $this->_tooltip_for_image($item);
+ $query[$key]["orientation"] = libraries\Image::get_exif_orientation($this->mfile->file($item["hash"]));
}
$this->data["items"] = $query;
diff --git a/application/core/MY_Controller.php b/application/core/MY_Controller.php
index bba8725a9..53aad9145 100644
--- a/application/core/MY_Controller.php
+++ b/application/core/MY_Controller.php
@@ -44,6 +44,7 @@ class MY_Controller extends CI_Controller {
mb_internal_encoding('UTF-8');
$this->load->helper(array('form', 'filebin'));
+ $this->load->library('customautoloader');
// TODO: proper accept header handling or is this enough?
if (isset($_SERVER["HTTP_ACCEPT"])) {
diff --git a/application/errors/error_general.php b/application/errors/error_general.php
index be495e4f6..844dfb74d 100644
--- a/application/errors/error_general.php
+++ b/application/errors/error_general.php
@@ -43,6 +43,9 @@ if (class_exists("CI_Controller") && !isset($GLOBALS["is_error_page"])) {
<?php
include 'application/views/footer.php';
+} elseif (php_sapi_name() === 'cli' OR defined('STDIN')) {
+ echo "$heading\n";
+ echo str_replace("<br>", "\n", $message);
} else {
// default CI error page
?>
@@ -96,7 +99,7 @@ code {
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
-p {
+p, div {
margin: 12px 15px 12px 15px;
}
</style>
diff --git a/application/libraries/Customautoloader.php b/application/libraries/Customautoloader.php
new file mode 100644
index 000000000..1904977d7
--- /dev/null
+++ b/application/libraries/Customautoloader.php
@@ -0,0 +1,26 @@
+<?php
+/*
+ * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * Licensed under AGPLv3
+ * (see COPYING for full license text)
+ *
+ */
+
+// Original source: http://stackoverflow.com/a/9526005/953022
+class CustomAutoloader{
+ public function __construct()
+ {
+ spl_autoload_register(array($this, 'loader'));
+ }
+
+ public function loader($className)
+ {
+ $path = APPPATH.str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php';
+ if (file_exists($path)) {
+ require $path;
+ return;
+ }
+ }
+}
+?>
diff --git a/application/libraries/Image.php b/application/libraries/Image.php
new file mode 100644
index 000000000..6d6c9e563
--- /dev/null
+++ b/application/libraries/Image.php
@@ -0,0 +1,267 @@
+<?php
+/*
+ * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * Licensed under AGPLv3
+ * (see COPYING for full license text)
+ *
+ */
+
+namespace libraries;
+
+/**
+ * This class deals with a single image and provides useful operations that
+ * operate on this image.
+ */
+class Image {
+ private $img;
+ private $source_type;
+ private $exif;
+
+ /**
+ * Create a new object and load the contents of file.
+ * @param file file to read
+ */
+ public function __construct($file)
+ {
+ $this->read($file);
+ }
+
+ /**
+ * Replace the current image by reading in a file
+ * @param file file to read
+ */
+ public function read($file)
+ {
+ $img = imagecreatefromstring(file_get_contents($file));
+ if ($img === false) {
+ show_error("Unsupported image type");
+ }
+ $this->set_img_object($img);
+ $this->fix_alpha();
+
+ $this->source_type = getimagesize($file)[2];
+
+ switch ($this->source_type) {
+ case IMAGETYPE_JPEG:
+ $this->exif = exif_read_data($file);
+ break;
+ default:
+ }
+ }
+
+ /**
+ * Return the current image rendered to a specific format. Passing null as
+ * the target_type returns the image in the format of the source image
+ * (loaded with read()).
+ *
+ * @param target_type one of IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG or null
+ * @return binary image data
+ */
+ public function get($target_type = null)
+ {
+ if ($target_type === null) {
+ $target_type = $this->source_type;
+ }
+
+ ob_start();
+ switch ($target_type) {
+ case IMAGETYPE_GIF:
+ $ret = imagegif($this->img);
+ break;
+ case IMAGETYPE_JPEG:
+ $ret = imagejpeg($this->img);
+ break;
+ case IMAGETYPE_PNG:
+ $ret = imagepng($this->img);
+ break;
+ default:
+ assert(0);
+ }
+ $result = ob_get_clean();
+
+ if (!$ret) {
+ show_error("Failed to create thumbnail");
+ }
+
+ return $result;
+ }
+
+ /**
+ * Resize the image.
+ * @param width
+ * @param height
+ */
+ public function resize($width, $height)
+ {
+ $temp_gdim = imagecreatetruecolor($width, $height);
+ $this->fix_alpha();
+ imagecopyresampled(
+ $temp_gdim,
+ $this->img,
+ 0, 0,
+ 0, 0,
+ $width, $height,
+ imagesx($this->img), imagesy($this->img)
+ );
+
+ $this->set_img_object($temp_gdim);
+ }
+
+ /**
+ * Crop the image to the area defined by x, y, width and height.
+ *
+ * @param x starting coordinate
+ * @param y starting coordinate
+ * @param width width of the area
+ * @param height height of the area
+ */
+ public function crop($x, $y, $width, $height)
+ {
+ $thumb = imagecreatetruecolor($width, $height);
+ $this->fix_alpha();
+ imagecopy(
+ $thumb,
+ $this->img,
+ 0, 0,
+ $x, $y,
+ $width, $height
+ );
+
+ $this->set_img_object($thumb);
+ }
+
+ /**
+ * Crop/resize the image to fit into the desired size. This also rotates
+ * the image if the source image had an EXIF orientation tag.
+ *
+ * @param width width of the resulting image
+ * @param height height of the resulting image
+ */
+ // Source: http://salman-w.blogspot.co.at/2009/04/crop-to-fit-image-using-aspphp.html
+ public function makeThumb($target_width, $target_height)
+ {
+ $source_aspect_ratio = imagesx($this->img) / imagesy($this->img);
+ $desired_aspect_ratio = $target_width / $target_height;
+
+ if ($source_aspect_ratio > $desired_aspect_ratio) {
+ // Triggered when source image is wider
+ $temp_height = $target_height;
+ $temp_width = round(($target_height * $source_aspect_ratio));
+ } else {
+ // Triggered otherwise (i.e. source image is similar or taller)
+ $temp_width = $target_width;
+ $temp_height = round(($target_width / $source_aspect_ratio));
+ }
+
+ $this->resize($temp_width, $temp_height);
+
+ $x0 = ($temp_width - $target_width) / 2;
+ $y0 = ($temp_height - $target_height) / 2;
+ $this->crop($x0, $y0, $target_width, $target_height);
+
+ $this->apply_exif_orientation();
+ }
+
+ static public function get_exif_orientation($file)
+ {
+ $type = getimagesize($file)[2];
+ switch ($type) {
+ case IMAGETYPE_JPEG:
+ $exif = exif_read_data($file);
+ if (isset($exif["Orientation"])) {
+ return $exif["Orientation"];
+ }
+ return 0;
+ break;
+ default:
+ return 0;
+ }
+ }
+
+ /**
+ * Rotate the image according to the sources EXIF orientation tag if any.
+ */
+ public function apply_exif_orientation()
+ {
+ if (isset($this->exif['Orientation'])) {
+ $mirror = false;
+ $deg = 0;
+
+ switch ($this->exif['Orientation']) {
+ case 2:
+ $mirror = true;
+ break;
+ case 3:
+ $deg = 180;
+ break;
+ case 4:
+ $deg = 180;
+ $mirror = true;
+ break;
+ case 5:
+ $deg = 270;
+ $mirror = true;
+ break;
+ case 6:
+ $deg = 270;
+ break;
+ case 7:
+ $deg = 90;
+ $mirror = true;
+ break;
+ case 8:
+ $deg = 90;
+ break;
+ }
+
+ if ($deg) {
+ $this->set_img_object(imagerotate($this->img, $deg, 0));
+ }
+
+ if ($mirror) {
+ $this->mirror();
+ }
+ }
+ }
+
+ /**
+ * Mirror the image along the x axis.
+ */
+ public function mirror()
+ {
+ $width = imagesx($this->img);
+ $height = imagesy($this->img);
+
+ $src_x = $width -1;
+ $src_y = 0;
+ $src_width = -$width;
+ $src_height = $height;
+
+ $imgdest = imagecreatetruecolor($width, $height);
+ imagealphablending($imgdest,false);
+ imagesavealpha($imgdest,true);
+
+ imagecopyresampled($imgdest, $this->img, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height);
+ $this->set_img_object($imgdest);
+ }
+
+ private function set_img_object($new)
+ {
+ assert($new !== false);
+
+ $old = $this->img;
+ $this->img = $new;
+
+ if ($old != null) {
+ imagedestroy($old);
+ }
+ }
+
+ private function fix_alpha()
+ {
+ imagealphablending($this->img,false);
+ imagesavealpha($this->img,true);
+ }
+
+}
diff --git a/application/models/mfile.php b/application/models/mfile.php
index 8dc4772d9..be315b9e6 100644
--- a/application/models/mfile.php
+++ b/application/models/mfile.php
@@ -101,115 +101,6 @@ class Mfile extends CI_Model {
return $mimetype;
}
- public function image_dimension($file)
- {
- list($width, $height) = getimagesize($file);
-
- return "${width}x${height}";
- }
-
- /*
- * This returns a square thumbnail for the input image
- * Source: http://salman-w.blogspot.co.at/2009/04/crop-to-fit-image-using-aspphp.html
- */
- public function makeThumb($id, $size = 150, $target_type = null)
- {
- $filedata = $this->get_filedata($id);
- if (!$filedata) {
- return false;
- }
-
- $source_path = $this->file($filedata["hash"]);
-
- $source_gdim = imagecreatefromstring(file_get_contents($source_path));
- if ($source_gdim === false) {
- show_error("Unsupported image type");
- }
- imagealphablending($source_gdim,false);
- imagesavealpha($source_gdim,true);
-
- list($source_width, $source_height, $source_type) = getimagesize($source_path);
-
- if ($target_type === null) {
- $target_type = $source_type;
- }
-
- $target_width = $size;
- $target_height = $size;
-
- $source_aspect_ratio = $source_width / $source_height;
- $desired_aspect_ratio = $target_width / $target_height;
-
- if ($source_aspect_ratio > $desired_aspect_ratio) {
- // Triggered when source image is wider
- $temp_height = $target_height;
- $temp_width = round(($target_height * $source_aspect_ratio));
- } else {
- // Triggered otherwise (i.e. source image is similar or taller)
- $temp_width = $target_width;
- $temp_height = round(($target_width / $source_aspect_ratio));
- }
-
- /*
- * Resize the image into a temporary GD image
- */
-
- $temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
- imagealphablending($temp_gdim,false);
- imagesavealpha($temp_gdim,true);
- imagecopyresampled(
- $temp_gdim,
- $source_gdim,
- 0, 0,
- 0, 0,
- $temp_width, $temp_height,
- $source_width, $source_height
- );
-
- /*
- * Copy cropped region from temporary image into the desired GD image
- */
-
- $x0 = ($temp_width - $target_width) / 2;
- $y0 = ($temp_height - $target_height) / 2;
- $thumb = imagecreatetruecolor($target_width, $target_height);
- imagealphablending($thumb,false);
- imagesavealpha($thumb,true);
- imagecopy(
- $thumb,
- $temp_gdim,
- 0, 0,
- $x0, $y0,
- $target_width, $target_height
- );
-
- ob_start();
- switch ($target_type) {
- case IMAGETYPE_GIF:
- $ret = imagegif($thumb);
- break;
- case IMAGETYPE_JPEG:
- $ret = imagejpeg($thumb);
- break;
- case IMAGETYPE_PNG:
- $ret = imagepng($thumb);
- break;
- default:
- assert(0);
- }
- $result = ob_get_clean();
-
- if (!$ret) {
- show_error("Failed to create thumbnail");
- }
-
- imagedestroy($thumb);
- imagedestroy($temp_gdim);
- imagedestroy($source_gdim);
-
- return $result;
- }
-
// Add a hash to the DB
function add_file($hash, $id, $filename)
{
diff --git a/application/views/file/fragments/thumbnail.php b/application/views/file/fragments/thumbnail.php
index 6bd82fcb9..61d15f325 100644
--- a/application/views/file/fragments/thumbnail.php
+++ b/application/views/file/fragments/thumbnail.php
@@ -1,8 +1,9 @@
+<?php register_js_include("/data/js/jquery.colorbox.js"); ?>
<!-- Comment markers background: http://stackoverflow.com/a/14776780/953022 -->
<div class="container container-wide">
<div class="upload_thumbnails"><!--
<?php foreach($items as $key => $item): ?>
- --><a href="<?php echo site_url("/".$item["id"])."/"; ?>" title="<?php echo htmlentities($item["filename"]); ?>" data-content="<?php echo htmlentities($item["tooltip"]); ?>" data-id="<?php echo $item["id"]; ?>"><img class="thumb" src="<?php echo site_url("file/thumbnail/".$item["id"]); ?>"></a><!--
+ --><a rel="gallery" class="colorbox" data-orientation="<?php echo $item["orientation"]; ?>" href="<?php echo site_url("/".$item["id"])."/"; ?>" title="<?php echo htmlentities($item["filename"]); ?>" data-content="<?php echo htmlentities($item["tooltip"]); ?>" data-id="<?php echo $item["id"]; ?>"><img class="thumb" src="<?php echo site_url("file/thumbnail/".$item["id"]); ?>"></a><!--
<?php endforeach; ?>
-->
</div>
diff --git a/application/views/header.php b/application/views/header.php
index 8f246aeb8..ab947bd45 100644
--- a/application/views/header.php
+++ b/application/views/header.php
@@ -16,6 +16,7 @@ if (is_cli_client() && !isset($force_full_html)) {
<link href="<?php echo link_with_mtime("/data/css/ui-lightness/jquery-ui-1.10.3.custom.min.css"); ?>" rel="stylesheet">
<link href="<?php echo link_with_mtime("/data/css/bootstrap.min.css"); ?>" rel="stylesheet">
<link href="<?php echo link_with_mtime("/data/css/style.css"); ?>" rel="stylesheet">
+ <link href="<?php echo link_with_mtime("/data/css/colorbox.css"); ?>" rel="stylesheet">
<?php
if (file_exists(FCPATH."data/local/style.css")) {
echo '<link href="'.link_with_mtime("/data/local/style.css").'" rel="stylesheet">';