OSDN Git Service

MERGE: リビジョン1870。Media/MediaObjectクラスの機能拡張
authorsakamocchi <o-takashi@sakamocchi.jp>
Tue, 22 May 2012 15:03:51 +0000 (00:03 +0900)
committersakamocchi <o-takashi@sakamocchi.jp>
Tue, 22 May 2012 15:03:51 +0000 (00:03 +0900)
リサンプリングを行うためのメソッド・メンバーをいくつか追加した。
リサンプリングメソッドは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
nucleus/media.php

index 9e6128a..a7c8a37 100644 (file)
@@ -14,7 +14,7 @@
  *\r
  * @license http://nucleuscms.org/license.txt GNU General Public License\r
  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
- * @version $Id: MEDIA.php 1864 2012-05-19 11:39:44Z sakamocchi $\r
+ * @version $Id: MEDIA.php 1870 2012-05-22 14:57:15Z sakamocchi $\r
  */\r
 \r
 define('PRIVATE_COLLECTION',           'Private Collection');\r
@@ -105,7 +105,7 @@ class Media
                while ( $filename = readdir($dirhandle) )\r
                {\r
                        // only add files that match the filter\r
-                       if ( !@is_dir($filename) && self::checkFilter($filename, $filter) )\r
+                       if ( !is_dir($mediadir . $filename) && self::checkFilter($filename, $filter) )\r
                        {\r
                                array_push($filelist, new MediaObject($collection, $filename, $DIR_MEDIA));\r
                        }\r
@@ -348,14 +348,14 @@ class Media
         * Media::responseResampledImage()\r
         * send resampled image via HTTP\r
         * \r
-        * @param       Object  $medium         Medium Object\r
+        * @param       object  $medium         Medium Object\r
         * @exit\r
         */\r
        static public function responseResampledImage($medium, $maxwidth=0, $maxheight=0)\r
        {\r
                if ( get_class($medium) !== 'Medium' )\r
                {\r
-                       header("HTTP/1.1 503 Service Unavailable");\r
+                       header("HTTP/1.1 500 Internal Server Error");\r
                        exit('Nucleus CMS: Fail to generate resampled image');\r
                        return;\r
                }\r
@@ -413,7 +413,7 @@ class Media
                \r
                if ( $name === '' )\r
                {\r
-                       $name = $this->getHashedname();\r
+                       $name = $medium->getHashedname();\r
                }\r
                \r
                $resampledimage = $medium->getResampledBinary($maxwidth, $maxheight);\r
@@ -507,16 +507,18 @@ class MediaObject
         * @param       string          $filename       \r
         * @param       string          $root           fullpath to media directory\r
         */\r
-       public function __construct($collection, $filename, $root)\r
+       public function __construct($collection, $filename, $root=0)\r
        {\r
                global $CONF, $DIR_MEDIA;\r
                \r
-               $root = preg_replace('#/*$#', '', $DIR_MEDIA);\r
-               if ( $root == '' || $collection == '' )\r
+               /* for backward compatibility */\r
+               if ( is_numeric($root) )\r
                {\r
-                       return FALSE;\r
+                       $root = $DIR_MEDIA;\r
                }\r
                \r
+               $root = preg_replace('#/*$#', '', $root);\r
+               \r
                /* get and validate fullpath for the medium */\r
                if ( !file_exists($root)\r
                  || FALSE === ($fullpath = realpath("{$root}/{$collection}/{$filename}"))\r
@@ -531,18 +533,34 @@ class MediaObject
                $this->private = (integer) $collection;\r
                $this->collection = $collection;\r
                $this->filename = basename($fullpath);\r
-               $this->timestamp = @filemtime($fullpath);\r
-               $this->size = ceil(filesize($fullpath) / 1000);\r
+               $this->timestamp = filemtime($fullpath);\r
                \r
                /* store relative directory path from root directory for media */\r
-               $this->path = preg_replace(array("#{$this->root}/#", "#/{$this->name}#"), '', $fullpath);\r
+               $this->path = preg_replace(array("#{$this->root}/#", "#/{$this->filename}#"), '', $fullpath);\r
                if ( $this->path === $this->name )\r
                {\r
                        $this->path = ''; \r
                }\r
                \r
+               return;\r
+       }\r
+       \r
+       /**\r
+        * MediaObject::refine()\r
+        * refine data\r
+        * \r
+        * @param       void\r
+        * @return      void\r
+        */\r
+       public function refine()\r
+       {\r
+               global $CONF;\r
+               \r
+               /* store size (byte order) */\r
+               $this->size = filesize("{$this->root}/{$this->path}/{$this->filename}");\r
+               \r
                /* get width and height if this is image binary */\r
-               if ( FALSE === ($info = @getimagesize ($fullpath)) )\r
+               if ( FALSE === ($info = @getimagesize ("{$this->root}/{$this->path}/{$this->filename}")) )\r
                {\r
                        $this->mime = 'application/octet-stream';\r
                        $this->width = 0;\r
@@ -559,7 +577,7 @@ class MediaObject
                if ( defined('FILEINFO_MIME_TYPE') && function_exists ('finfo_open')\r
                  && (FALSE !== ($info = finfo_open(FILEINFO_MIME_TYPE))) )\r
                {\r
-                       $this->mime = finfo_file($info, $fullpath);\r
+                       $this->mime = finfo_file($info, "{$this->root}/{$this->path}/{$this->filename}");\r
                }\r
                \r
                /* store data with parsed filename */\r
index beeeb8c..0635de3 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
- * Copyright (C) 2002-2012 The Nucleus Group
+ * Copyright (C) 2002-2009 The Nucleus Group
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -20,8 +20,8 @@
  *     passed through to the add-item form (linkto, popupimg or inline img)
  *
  * @license http://nucleuscms.org/license.txt GNU General Public License
- * @copyright Copyright (C) 2002-2012 The Nucleus Group
- * @version $Id: media.php 1624 2012-01-09 11:36:20Z sakamocchi $
+ * @copyright Copyright (C) 2002-2009 The Nucleus Group
+ * @version $Id: media.php 1870 2012-05-22 14:57:15Z sakamocchi $
  *
  */
 
@@ -163,63 +163,69 @@ function media_select() {
                </tr>
 
        <?php
-
-       if (sizeof($arr)>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 "<tr>";
-                       echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";
-
+               }
+               
+               for ( $i = $idxStart; $i < $idxEnd; $i++ )
+               {
+                       $medium = $arr[$i];
+                       $medium->refine();
+                       
+                       echo "<tr>\n";
+                       echo "<td>" . date("Y-m-d", $medium->timestamp) . "</td>\n";
+                       
                        // strings for javascript
-                       $jsCurrentCollection = str_replace("'","\\'",$currentCollection);
-                       $jsFileName = str_replace("'","\\'",$obj->filename);
-
-                       if ($filetype != 0) {
-                               // image (gif/jpg/png/swf)
-                               echo "<td><a href=\"media.php\" onclick=\"chooseImage('", Entity::hsc($jsCurrentCollection), "','", Entity::hsc($jsFileName), "',"
-                                                          . "'", Entity::hsc($width), "','" , Entity::hsc($height), "'"
-                                                          . ")\" title=\"" . Entity::hsc($obj->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 "<td><a href=\"media.php\" onclick=\"chooseImage('" . Entity::hsc($jsCurrentCollection) . "','" . Entity::hsc($jsFileName) . "',"
+                                                          . "'" . Entity::hsc($medium->width) . "','" . Entity::hsc($medium->height) . "'"
+                                                          . ")\" title=\"" . Entity::hsc($medium->filename) . "\">"
+                                                          . Entity::hsc(Entity::shorten($medium->filename, 25, '...'))
                                                           ."</a>";
-                               echo ' (<a href="', Entity::hsc($CONF['MediaURL'] . $currentCollection . '/' . $obj->filename), '" onclick="window.open(this.href); return false;" title="',Entity::hsc(_MEDIA_VIEW_TT),'">',_MEDIA_VIEW,'</a>)';
-                               echo "</td>";
-                       } else {
+                               echo ' (<a href="', Entity::hsc("{$CONF['MediaURL']}/$currentCollection/$medium->filename"), '" onclick="window.open(this.href); return false;" title="'. Entity::hsc(_MEDIA_VIEW_TT) . '">' . _MEDIA_VIEW . '</a>)';
+                               echo "</td>\n";
+                               echo '<td>' . Entity::hsc($medium->width) . 'x' . Entity::hsc($medium->height) . "</td>\n";
+                       }
+                       else
+                       {
                                // no image (e.g. mpg)
                                echo "<td><a href='media.php' onclick=\"chooseOther('" , Entity::hsc($jsCurrentCollection), "','", Entity::hsc($jsFileName), "'"
-                                                          . ")\" title=\"" . Entity::hsc($obj->filename). "\">"
-                                                          . Entity::hsc(Entity::shorten($obj->filename,30,'...'))
-                                                          ."</a></td>";
-
+                                              . ")\" title=\"" . Entity::hsc($medium->filename). "\">"
+                                              . Entity::hsc(Entity::shorten($medium->filename, 30, '...'))
+                                              ."</a></td>\n";
+                               echo '<td>' . Entity::hsc($medium->size) . "KB</td>\n";
                        }
-                       echo '<td>' , Entity::hsc($width) , 'x' , Entity::hsc($height) , '</td>';
-                       echo '</tr>';
+                       echo "</tr>\n";
+                       continue;
                }
-       } // if (sizeof($arr)>0)
+       }
        ?>
 
                </table>