From 9b0b9a0e6082b3375ebdadc651c971671c922ebc Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 29 Sep 2014 00:41:35 +0200 Subject: Move thumbnail code to dedicated class This also moves the code from mfile->image_dimension() to the only place where it was called. Signed-off-by: Florian Pritz --- application/controllers/file.php | 10 ++- application/libraries/Imglib.php | 169 ++++++++++++++++++++++++++++++++++++++ application/models/mfile.php | 172 --------------------------------------- 3 files changed, 175 insertions(+), 176 deletions(-) create mode 100644 application/libraries/Imglib.php diff --git a/application/controllers/file.php b/application/controllers/file.php index e1b43d314..1bdab2c9f 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -316,13 +316,14 @@ class File extends MY_Controller { private function _tooltip_for_image($filedata) { + $this->load->library("imglib"); $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
"; $tooltip .= "$upload_date
"; - $tooltip .= "$dimensions - ${filedata["mimetype"]}
"; + $tooltip .= "${width}x${height} - ${filedata["mimetype"]}
"; return $tooltip; } @@ -514,9 +515,10 @@ 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); + $this->load->library("imglib"); + $thumb = $CI->imglib->makeThumb($this->mfile->file($filedata["hash"]), $thumb_size, IMAGETYPE_JPEG); if ($thumb === false) { show_error("Failed to generate thumbnail"); diff --git a/application/libraries/Imglib.php b/application/libraries/Imglib.php new file mode 100644 index 000000000..4d6de21d3 --- /dev/null +++ b/application/libraries/Imglib.php @@ -0,0 +1,169 @@ + + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +class Imglib { + /* + * 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($file, $size = 150, $target_type = null) + { + $source_gdim = imagecreatefromstring(file_get_contents($file)); + 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($file); + + 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 + ); + + /* + * Fix orientation according to exif tag + */ + try { + $exif = exif_read_data($file); + } catch (ErrorException $e) { + } + + if (isset($exif['Orientation'])) { + $mirror = false; + $deg = 0; + + switch ($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) $thumb = imagerotate($thumb, $deg, 0); + if ($mirror) $thumb = $this->_mirrorImage($thumb); + } + + 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; + } + + private function _mirrorImage($imgsrc) + { + $width = imagesx($imgsrc); + $height = imagesy($imgsrc); + + $src_x = $width -1; + $src_y = 0; + $src_width = -$width; + $src_height = $height; + + $imgdest = imagecreatetruecolor($width, $height); + imagealphablending($imgdest,false); + imagesavealpha($imgdest,true); + + if (imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) { + return $imgdest; + } + + return $imgsrc; + } + +} diff --git a/application/models/mfile.php b/application/models/mfile.php index c4d2c6e68..f23855bfd 100644 --- a/application/models/mfile.php +++ b/application/models/mfile.php @@ -102,178 +102,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 - ); - - /* - * Fix orientation according to exif tag - */ - try { - $exif = exif_read_data($source_path); - } catch (ErrorException $e) { - } - - if (isset($exif['Orientation'])) { - $mirror = false; - $deg = 0; - - switch ($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) $thumb = imagerotate($thumb, $deg, 0); - if ($mirror) $thumb = $this->_mirrorImage($thumb); - } - - 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; - } - - function _mirrorImage($imgsrc) - { - $width = imagesx($imgsrc); - $height = imagesy($imgsrc); - - $src_x = $width -1; - $src_y = 0; - $src_width = -$width; - $src_height = $height; - - $imgdest = imagecreatetruecolor($width, $height); - imagealphablending($imgdest,false); - imagesavealpha($imgdest,true); - - if (imagecopyresampled($imgdest, $imgsrc, 0, 0, $src_x, $src_y, $width, $height, $src_width, $src_height)) { - return $imgdest; - } - - return $imgsrc; - } - // Add a hash to the DB function add_file($hash, $id, $filename) { -- cgit v1.2.3-24-g4f1b