From 98a703b0da3172e79fbfaab2d60135c8e3810edb Mon Sep 17 00:00:00 2001 From: sakamocchi Date: Wed, 23 May 2012 00:03:51 +0900 Subject: [PATCH] =?utf8?q?MERGE:=20=E3=83=AA=E3=83=93=E3=82=B8=E3=83=A7?= =?utf8?q?=E3=83=B31870=E3=80=82Media/MediaObject=E3=82=AF=E3=83=A9?= =?utf8?q?=E3=82=B9=E3=81=AE=E6=A9=9F=E8=83=BD=E6=8B=A1=E5=BC=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit リサンプリングを行うためのメソッド・メンバーをいくつか追加した。 リサンプリングメソッドはGDライブラリのPHPバインディングを利用。 リサンプリング可能なバイナリのMIMEはimage/jpeg、image/png、image/gifの3種類 Revision 1870: ADD: resampling method to Media and MediaObject class Add: Media::$thumbdir Media::$algorism Media::$image_mime Media::responseResampledImage() Media::storeResampledImage() Media::sort_media_by_filename() Move: sort_media() to Media::sort_media_by_timestamp() MediaObject is rewritten keeping backward compatibility. Some members and functions are newly added. Resampling method is based on PHP's default build-in extension, GD library binding. http://nucleuscms.svn.sourceforge.net/viewvc/nucleuscms?view=revision&revision=1870 --- nucleus/libs/MEDIA.php | 46 +++++++++++++++------- nucleus/media.php | 102 ++++++++++++++++++++++++++----------------------- 2 files changed, 86 insertions(+), 62 deletions(-) diff --git a/nucleus/libs/MEDIA.php b/nucleus/libs/MEDIA.php index 9e6128a..a7c8a37 100644 --- a/nucleus/libs/MEDIA.php +++ b/nucleus/libs/MEDIA.php @@ -14,7 +14,7 @@ * * @license http://nucleuscms.org/license.txt GNU General Public License * @copyright Copyright (C) 2002-2012 The Nucleus Group - * @version $Id: MEDIA.php 1864 2012-05-19 11:39:44Z sakamocchi $ + * @version $Id: MEDIA.php 1870 2012-05-22 14:57:15Z sakamocchi $ */ define('PRIVATE_COLLECTION', 'Private Collection'); @@ -105,7 +105,7 @@ class Media while ( $filename = readdir($dirhandle) ) { // only add files that match the filter - if ( !@is_dir($filename) && self::checkFilter($filename, $filter) ) + if ( !is_dir($mediadir . $filename) && self::checkFilter($filename, $filter) ) { array_push($filelist, new MediaObject($collection, $filename, $DIR_MEDIA)); } @@ -348,14 +348,14 @@ class Media * Media::responseResampledImage() * send resampled image via HTTP * - * @param Object $medium Medium Object + * @param object $medium Medium Object * @exit */ static public function responseResampledImage($medium, $maxwidth=0, $maxheight=0) { if ( get_class($medium) !== 'Medium' ) { - header("HTTP/1.1 503 Service Unavailable"); + header("HTTP/1.1 500 Internal Server Error"); exit('Nucleus CMS: Fail to generate resampled image'); return; } @@ -413,7 +413,7 @@ class Media if ( $name === '' ) { - $name = $this->getHashedname(); + $name = $medium->getHashedname(); } $resampledimage = $medium->getResampledBinary($maxwidth, $maxheight); @@ -507,16 +507,18 @@ class MediaObject * @param string $filename * @param string $root fullpath to media directory */ - public function __construct($collection, $filename, $root) + public function __construct($collection, $filename, $root=0) { global $CONF, $DIR_MEDIA; - $root = preg_replace('#/*$#', '', $DIR_MEDIA); - if ( $root == '' || $collection == '' ) + /* for backward compatibility */ + if ( is_numeric($root) ) { - return FALSE; + $root = $DIR_MEDIA; } + $root = preg_replace('#/*$#', '', $root); + /* get and validate fullpath for the medium */ if ( !file_exists($root) || FALSE === ($fullpath = realpath("{$root}/{$collection}/{$filename}")) @@ -531,18 +533,34 @@ class MediaObject $this->private = (integer) $collection; $this->collection = $collection; $this->filename = basename($fullpath); - $this->timestamp = @filemtime($fullpath); - $this->size = ceil(filesize($fullpath) / 1000); + $this->timestamp = filemtime($fullpath); /* store relative directory path from root directory for media */ - $this->path = preg_replace(array("#{$this->root}/#", "#/{$this->name}#"), '', $fullpath); + $this->path = preg_replace(array("#{$this->root}/#", "#/{$this->filename}#"), '', $fullpath); if ( $this->path === $this->name ) { $this->path = ''; } + return; + } + + /** + * MediaObject::refine() + * refine data + * + * @param void + * @return void + */ + public function refine() + { + global $CONF; + + /* store size (byte order) */ + $this->size = filesize("{$this->root}/{$this->path}/{$this->filename}"); + /* get width and height if this is image binary */ - if ( FALSE === ($info = @getimagesize ($fullpath)) ) + if ( FALSE === ($info = @getimagesize ("{$this->root}/{$this->path}/{$this->filename}")) ) { $this->mime = 'application/octet-stream'; $this->width = 0; @@ -559,7 +577,7 @@ class MediaObject if ( defined('FILEINFO_MIME_TYPE') && function_exists ('finfo_open') && (FALSE !== ($info = finfo_open(FILEINFO_MIME_TYPE))) ) { - $this->mime = finfo_file($info, $fullpath); + $this->mime = finfo_file($info, "{$this->root}/{$this->path}/{$this->filename}"); } /* store data with parsed filename */ diff --git a/nucleus/media.php b/nucleus/media.php index beeeb8c..0635de3 100644 --- a/nucleus/media.php +++ b/nucleus/media.php @@ -1,7 +1,7 @@ 0) { - - if (($offset + $CONF['MediaPerPage']) >= sizeof($arr)) + if ( sizeof($arr) > 0 ) + { + if ( ($offset + $CONF['MediaPerPage']) >= sizeof($arr) ) + { $offset = sizeof($arr) - $CONF['MediaPerPage']; - - if ($offset < 0) $offset = 0; - + } + + if ( $offset < 0 ) + { + $offset = 0; + } + $idxStart = $offset; $idxEnd = $offset + $CONF['MediaPerPage']; $idxNext = $idxEnd; $idxPrev = $idxStart - $CONF['MediaPerPage']; - - if ($idxPrev < 0) $idxPrev = 0; - - if ($idxEnd > sizeof($arr)) + + if ( $idxPrev < 0 ) + { + $idxPrev = 0; + } + + if ( $idxEnd > sizeof($arr) ) + { $idxEnd = sizeof($arr); - - for($i=$idxStart;$i<$idxEnd;$i++) { - $obj = $arr[$i]; - $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename; - - $old_level = error_reporting(0); - $size = @GetImageSize($filename); - error_reporting($old_level); - $width = $size[0]; - $height = $size[1]; - $filetype = $size[2]; - - echo ""; - echo "". date("Y-m-d",$obj->timestamp) .""; - + } + + for ( $i = $idxStart; $i < $idxEnd; $i++ ) + { + $medium = $arr[$i]; + $medium->refine(); + + echo "\n"; + echo "" . date("Y-m-d", $medium->timestamp) . "\n"; + // strings for javascript - $jsCurrentCollection = str_replace("'","\\'",$currentCollection); - $jsFileName = str_replace("'","\\'",$obj->filename); - - if ($filetype != 0) { - // image (gif/jpg/png/swf) - echo "filename). "\">" - . Entity::hsc(Entity::shorten($obj->filename,25,'...')) + $jsCurrentCollection = str_replace("'", "\\'", $currentCollection); + $jsFileName = str_replace("'", "\\'", $medium->filename); + + if ( array_key_exists($medium->mime, Media::$image_mime) ) + { + echo "width) . "','" . Entity::hsc($medium->height) . "'" + . ")\" title=\"" . Entity::hsc($medium->filename) . "\">" + . Entity::hsc(Entity::shorten($medium->filename, 25, '...')) .""; - echo ' (',_MEDIA_VIEW,')'; - echo ""; - } else { + echo ' (filename"), '" onclick="window.open(this.href); return false;" title="'. Entity::hsc(_MEDIA_VIEW_TT) . '">' . _MEDIA_VIEW . ')'; + echo "\n"; + echo '' . Entity::hsc($medium->width) . 'x' . Entity::hsc($medium->height) . "\n"; + } + else + { // no image (e.g. mpg) echo "filename). "\">" - . Entity::hsc(Entity::shorten($obj->filename,30,'...')) - .""; - + . ")\" title=\"" . Entity::hsc($medium->filename). "\">" + . Entity::hsc(Entity::shorten($medium->filename, 30, '...')) + ."\n"; + echo '' . Entity::hsc($medium->size) . "KB\n"; } - echo '' , Entity::hsc($width) , 'x' , Entity::hsc($height) , ''; - echo ''; + echo "\n"; + continue; } - } // if (sizeof($arr)>0) + } ?> -- 2.11.0