OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / manager / imageManager.php
1 <?php
2 /**
3  * 画像処理マネージャー
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2013 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: imageManager.php 6148 2013-07-01 01:11:14Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once(M3_SYSTEM_INCLUDE_PATH . '/common/core.php');
17
18 class ImageManager extends Core
19 {
20         const CONTENT_DIR = '/etc/';
21         const THUMBNAIL_DIR = '/thumb';
22         const SITE_LOGO_DIR = '/etc/site/thumb/';               // サイトロゴ格納ディレクトリ
23         const AVATAR_DIR = '/etc/avatar/';              // アバター格納ディレクトリ
24         const DEFAULT_THUMB_DIR = '/etc/default/thumb/';                // デフォルトサムネールディレクトリ
25         const DEFAULT_AVATAR_BASE = 'default_';         // デフォルトのアバターファイル名ヘッド部
26         const DEFAULT_THUMBNAIL_EXT = 'png';
27         const DEFAULT_THUMBNAIL_SIZE = 72;
28         const DEFAULT_THUMBNAIL_TYPE = '72c';
29         const CF_SITE_LOGO_FILENAME = 'site_logo_filename';             // サイトロゴファイル
30         const CF_THUMB_FORMAT = 'thumb_format';         // サムネールフォーマット
31         const CF_AVATAR_FORMAT = 'avatar_format';       // アバターフォーマット
32         const CF_OGP_THUMB_FORMAT = 'ogp_thumb_format';         // OGPサムネールフォーマット
33         const NOT_AVAILABLE_HEAD = 'notavailable_';             // 設定画像がない場合の表示画像のファイル名ヘッダ部
34         
35         /**
36          * コンストラクタ
37          */
38         function __construct()
39         {
40                 // 親クラスを呼び出す
41                 parent::__construct();
42         }
43         /**
44          * システム共通のデフォルトのサムネールを作成
45          *
46          * @param string $contentType   コンテンツタイプ
47          * @param int    $deviceType    デバイスタイプ(0=PC、1=携帯、2=スマートフォン)
48          * @param string $contentId             コンテンツID
49          * @param string $path                  元の画像ファイル
50          * @param array  $destFilename  作成した画像のファイル名
51          * @param string $destDir               作成した画像のディレクトリ
52          * @param string $destDirUrl    作成した画像のディレクトリURL
53          * @return bool                                 true=作成,false=作成失敗
54          */
55         function createSystemDefaultThumb($contentType, $deviceType, $contentId, $path, &$destFilename, &$destDir = NULL, &$destDirUrl = NULL)
56         {
57                 global $gSystemManager;
58
59                 // パラメータエラーチェック
60                 if (strlen($contentId) == 0) return false;
61                 
62                 $destFilename = array();                // 画像ファイル名
63                 
64                 // 画像格納用のディレクトリ作成
65                 $thumbDir = $this->getSystemThumbPath($contentType, $deviceType);
66                 if (!file_exists($thumbDir)) mkdir($thumbDir, M3_SYSTEM_DIR_PERMISSION, true/*再帰的*/);
67                 if (!is_null($destDir)) $destDir = $thumbDir;
68                 
69                 // 画像ディレクトリのURL
70                 if (!is_null($destDirUrl)) $destDirUrl = $this->getSystemThumbUrl($contentType, $deviceType);
71
72                 // 画像のフォーマットを取得
73                 $formatArray = explode(';', $gSystemManager->getSystemConfig(self::CF_THUMB_FORMAT));
74                 
75                 for ($i = 0; $i < count($formatArray); $i++){
76                         $format = trim($formatArray[$i]);
77                         $ret = preg_match('/(\d+)(.*)\.(gif|png|jpg|jpeg|bmp)$/i', $format, $matches);
78                         if ($ret){
79                                 $thumbSize = $matches[1];
80                                 $thumbAttr = strtolower($matches[2]);
81                                 $ext = strtolower($matches[3]);
82
83                                 $imageType = IMAGETYPE_JPEG;
84                                 switch ($ext){
85                                         case 'jpg':
86                                         case 'jpeg':
87                                                 $imageType = IMAGETYPE_JPEG;
88                                                 break;
89                                         case 'gif':
90                                                 $imageType = IMAGETYPE_GIF;
91                                                 break;
92                                         case 'png':
93                                                 $imageType = IMAGETYPE_PNG;
94                                                 break;
95                                         case 'bmp':
96                                                 $imageType = IMAGETYPE_BMP;
97                                                 break;
98                                 }
99                                 $thumbFilename = $contentId . '_' . $format;
100                                 $thumbPath = $thumbDir . DIRECTORY_SEPARATOR . $thumbFilename;
101
102                                 // サムネールの作成
103                                 if ($thumbAttr == 'c'){         // 切り取りサムネールの場合
104                                         //$ret = $this->gInstance->getImageManager()->createThumb($path, $thumbPath, $thumbSize, $imageType, true);
105                                         $ret = $this->createThumb($path, $thumbPath, $thumbSize, $imageType, true);
106                                 } else {
107                                         //$ret = $this->gInstance->getImageManager()->createThumb($path, $thumbPath, $thumbSize, $imageType, false);
108                                         $ret = $this->createThumb($path, $thumbPath, $thumbSize, $imageType, false);
109                                 }
110                                 if ($ret){
111                                         $destFilename[] = $thumbFilename;
112                                 } else {
113                                         return false;
114                                 }
115                         }
116                 }
117                 return true;
118         }
119         /**
120          * システム共通のデフォルトのサムネールを削除
121          *
122          * @param string $contentType   コンテンツタイプ
123          * @param int    $deviceType    デバイスタイプ(0=PC、1=携帯、2=スマートフォン)
124          * @param array  $filename              ファイル名
125          * @return bool                                 true=成功,false=失敗
126          */
127         function delSystemDefaultThumb($contentType, $deviceType, $filename)
128         {
129                 global $gSystemManager;
130                 
131                 // 画像格納用のディレクトリ取得
132                 $thumbDir = $this->getSystemThumbPath($contentType, $deviceType);
133                 
134                 $ret = true;
135                 for ($i = 0; $i < count($filename); $i++){
136                         $thumbnailPath = $thumbDir . DIRECTORY_SEPARATOR . $filename[$i];
137                         if (!@unlink($thumbnailPath)) $ret = false;
138                 }
139                 return $ret;
140         }
141         /**
142          * システム共通のサムネール画像のURLを取得
143          *
144          * @param string     $contentType       コンテンツタイプ
145          * @param int        $deviceType        デバイスタイプ(0=PC、1=携帯、2=スマートフォン)
146          * @param string     $filename          ファイル名(空のときは格納ディレクトリを返す)
147          * @param string     $notAvailableFileType      ファイルが見つからない場合の代替ファイルのタイプ(ogp=OGP用サムネール)
148          * @return string                                       画像URL
149          */
150         function getSystemThumbUrl($contentType, $deviceType, $filename = '', $notAvailableFileType = '')
151         {
152                 global $gEnvManager;
153                                 
154                 switch ($deviceType){
155                         case 0:         // PC
156                         default:
157                                 $deviceDir = '';
158                                 break;
159                         case 1:         // 携帯
160                                 $deviceDir = '/m';
161                                 break;
162                         case 2:         // スマートフォン
163                                 $deviceDir = '/s';
164                                 break;
165                 }
166                 
167                 $destUrl = $gEnvManager->getResourceUrl() . self::CONTENT_DIR . $contentType . $deviceDir . self::THUMBNAIL_DIR;                // 画像格納用のディレクトリ
168                 
169                 if (empty($notAvailableFileType)){
170                         if (empty($filename)){
171                                 return $destUrl;
172                         } else {
173                                 return $destUrl . '/' . $filename;
174                         }
175                 } else {                // 画像が見つからない場合の画像タイプが指定されているとき
176                         // ファイルとして扱う
177                         $destDir = $gEnvManager->getResourcePath() . self::CONTENT_DIR . $contentType . $deviceDir . self::THUMBNAIL_DIR;               // 画像格納用のディレクトリ
178                         $path = $destDir . DIRECTORY_SEPARATOR . $filename;
179                         if (!is_dir($path) && is_readable($path)){
180                                 return $destUrl . '/' . $filename;
181                         } else {
182                                 return $this->getNotAvailableImageUrl($notAvailableFileType);
183                         }
184                 }
185         }
186         /**
187          * システム共通のサムネール画像のパスを取得
188          *
189          * @param string     $contentType       コンテンツタイプ
190          * @param int        $deviceType        デバイスタイプ(0=PC、1=携帯、2=スマートフォン)
191          * @param string     $filename          ファイル名(空のときは格納ディレクトリを返す)
192          * @param string     $notAvailableFileType      ファイルが見つからない場合の代替ファイルのタイプ(ogp=OGP用サムネール)
193          * @return string                                       画像パス
194          */
195         function getSystemThumbPath($contentType, $deviceType, $filename = '', $notAvailableFileType = '')
196         {
197                 global $gEnvManager;
198                                 
199                 switch ($deviceType){
200                         case 0:         // PC
201                         default:
202                                 $deviceDir = '';
203                                 break;
204                         case 1:         // 携帯
205                                 $deviceDir = '/m';
206                                 break;
207                         case 2:         // スマートフォン
208                                 $deviceDir = '/s';
209                                 break;
210                 }
211                 
212                 $destDir = $gEnvManager->getResourcePath() . self::CONTENT_DIR . $contentType . $deviceDir . self::THUMBNAIL_DIR;               // 画像格納用のディレクトリ
213                 
214                 if (empty($notAvailableFileType)){
215                         if (empty($filename)){
216                                 return $destDir;
217                         } else {
218                                 return $destDir . DIRECTORY_SEPARATOR . $filename;
219                         }
220                 } else {                // 画像が見つからない場合の画像タイプが指定されているとき
221                         // ファイルとして扱う
222                         $path = $destDir . DIRECTORY_SEPARATOR . $filename;
223                         if (!is_dir($path) && is_readable($path)){
224                                 return $destDir . DIRECTORY_SEPARATOR . $filename;
225                         } else {
226                                 return $this->getNotAvailableImagePath($notAvailableFileType);
227                         }
228                 }
229         }
230         /**
231          * デフォルトのサムネールを作成
232          *
233          * @param string $contentType   コンテンツタイプ
234          * @param string $contentId             コンテンツID
235          * @param string $path                  元の画像ファイル
236          * @return bool                         true=作成,false=作成失敗
237          */
238         function createDefaultThumb($contentType, $contentId, $path)
239         {
240                 global $gEnvManager;
241                 
242                 // サムネール画像のパス
243                 $destPath = $gEnvManager->getResourcePath() . self::CONTENT_DIR . $contentType . self::THUMBNAIL_DIR . DIRECTORY_SEPARATOR . $contentId . '_' . self::DEFAULT_THUMBNAIL_TYPE . '.' . self::DEFAULT_THUMBNAIL_EXT;
244
245                 // 画像格納用のディレクトリ作成
246                 $destDir = dirname($destPath);
247                 if (!file_exists($destDir)) mkdir($destDir, M3_SYSTEM_DIR_PERMISSION, true/*再帰的*/);
248                                         
249                 $ret = $this->createThumb($path, $destPath, self::DEFAULT_THUMBNAIL_SIZE, IMAGETYPE_PNG);
250                 return $ret;
251         }
252         /**
253          * サムネール画像のURLを取得
254          *
255          * @param string $contentType   コンテンツタイプ
256          * @param string $contentId             コンテンツID
257          * @return string                               画像URL
258          */
259         function getDefaultThumbUrl($contentType, $contentId)
260         {
261                 global $gEnvManager;
262                 
263                 return $gEnvManager->getResourceUrl() . self::CONTENT_DIR . $contentType . self::THUMBNAIL_DIR . '/' . $contentId . '_' . self::DEFAULT_THUMBNAIL_TYPE . '.' . self::DEFAULT_THUMBNAIL_EXT;
264         }
265         /**
266          * サムネール画像のファイルパスを取得
267          *
268          * @param string $contentType   コンテンツタイプ
269          * @param string $contentId             コンテンツID
270          * @return string                               画像パス
271          */
272         function getDefaultThumbPath($contentType, $contentId)
273         {
274                 global $gEnvManager;
275                 
276                 return $gEnvManager->getResourcePath() . self::CONTENT_DIR . $contentType . self::THUMBNAIL_DIR . DIRECTORY_SEPARATOR . $contentId . '_' . self::DEFAULT_THUMBNAIL_TYPE . '.' . self::DEFAULT_THUMBNAIL_EXT;
277         }
278         /**
279          * サムネールを作成
280          *
281          * @param string $path          元の画像ファイル
282          * @param string $destPath      サムネールファイルパス
283          * @param int,array $size       サムネールの縦横サイズ(縦横異なる場合は連想配列(width,height))
284          * @param int $type                     サムネールの画像タイプ
285          * @param bool $useCrop         画像切り取りありかどうか
286          * @param string $bgColor       背景色。設定しない場合は空文字列
287          * @param int $imageQuality     画像品質
288          * @return bool                         true=作成,false=作成失敗
289          */
290         function createThumb($path, $destPath, $size, $type, $useCrop = true, $bgColor = '#FFFFFF', $imageQuality = 100)
291         {
292                 $imageSize = @getimagesize($path);
293                 if ($imageSize){
294                         $imageType = $imageSize[2];
295                 } else {
296                         return false;
297                 }
298                 
299                 // 画像作成
300                 switch ($imageType){
301                         case IMAGETYPE_GIF:
302                                 $imageObj = @imagecreatefromgif($path);
303                                 break;
304                         case IMAGETYPE_JPEG:
305                                 $imageObj = @imagecreatefromjpeg($path);
306                                 break;
307                         case IMAGETYPE_PNG:
308                                 $imageObj = @imagecreatefrompng($path);
309                                 break;
310                         default:
311                                 return false;
312                 }
313
314                 // 画像サイズ取得
315                 $x = 0;
316                 $y = 0;
317                 $newX = 0;
318                 $newY = 0;
319                 $width = imagesx($imageObj);
320                 $height = imagesy($imageObj);
321                 if (is_array($size)){
322                         $destWidth = $size['width'];            // サムネールの幅
323                         $destHeight = $size['height'];          // サムネールの高さ
324                 } else {
325                         $destWidth = $size;             // サムネールの幅
326                         $destHeight = $size;            // サムネールの高さ
327                 }
328                 if ($useCrop){          // 画像切り取りありのとき
329                         if (is_array($size)){
330                                 if ($height / $destHeight < $width / $destWidth){
331                                         $newHeight      = $destHeight;
332                                         $newWidth       = round($destHeight * $width / $height);
333                                         $x = ceil(($newWidth - $destWidth) / 2);
334                                         $y = 0;
335                                 } else {
336                                         $newWidth       = $destWidth;
337                                         $newHeight      = round($destWidth * $height / $width);
338                                         $x = 0;
339                                         $y = ceil(($newHeight - $destHeight) / 2);
340                                 }
341                         } else {
342                                 if ($width > $height){
343                                         $newHeight      = $size;
344                                         $newWidth       = round($size * $width / $height);
345
346                                         $x = ceil(($width - $height) / 2);
347                                         $y = 0;
348                                 } else {
349                                         $newWidth       = $size;
350                                         $newHeight      = round($size * $height / $width);
351
352                                         $x = 0;
353                                         $y = ceil(($height - $width) / 2);
354                                 }
355                         }
356                 } else {
357                         if ($width > $height){
358                                 $newHeight = $height * ($size / $width);
359                                 $newWidth = $size;
360                         } else {
361                                 $newWidth = $width * ($size / $height);
362                                 $newHeight = $size;
363                         }
364                 
365                         $newX = 0;
366                         $newY = 0;
367                         if ($newWidth < $size) $newX = round(($size - $newWidth) / 2);
368                         if ($newHeight < $size) $newY = round(($size - $newHeight) / 2);
369                 }
370
371                 // TrueColorイメージを作成
372                 //$thumbObj = imagecreatetruecolor($size, $size);
373                 $thumbObj = imagecreatetruecolor($destWidth, $destHeight);
374                 
375                 // 背景色の設定           
376                 if (!$useCrop){                 // 切り取りなしのとき
377                         // サムネールの背景色を取得
378                         $bgColorR = intval(substr($bgColor, 1, 2), 16);
379                         $bgColorG = intval(substr($bgColor, 3, 2), 16);
380                         $bgColorB = intval(substr($bgColor, 5, 2), 16);
381
382                         // TrueColorイメージを作成
383                 //      $thumbObj = imagecreatetruecolor($size, $size);
384                         $bgcolor = imagecolorallocate($thumbObj, $bgColorR, $bgColorG, $bgColorB);              // 背景色設定
385                         imagefill($thumbObj, 0, 0, $bgcolor);
386                 }
387                 
388                 // 画像リサイズ
389                 // imagecopyresampledの方がimagecopyresizedよりも画質が良いのでこちらを使用
390                 if (function_exists("imagecopyresampled")){
391                         if (!imagecopyresampled($thumbObj, $imageObj, $newX, $newY, $x, $y, $newWidth, $newHeight, $width, $height)){
392                                 if (!imagecopyresized($thumbObj, $imageObj, $newX, $newY, $x, $y, $newWidth, $newHeight, $width, $height)) return false;
393                         }
394                 } else {
395                         if (!imagecopyresized($thumbObj, $imageObj, $newX, $newY, $x, $y, $newWidth, $newHeight, $width, $height)) return false;
396                 }
397                 
398                 // 画像出力
399                 switch ($type){
400                         case IMAGETYPE_GIF:
401                                 $ret = @imagegif($thumbObj, $destPath, $imageQuality);
402                                 break;
403                         case IMAGETYPE_JPEG:
404                                 $ret = @imagejpeg($thumbObj, $destPath, $imageQuality);
405                                 break;
406                         case IMAGETYPE_PNG:
407                                 //$ret = @imagepng($thumbObj, $destPath, $imageQuality);                // PNGでは$imageQualityを使用すると画像が0サイズで作成される
408                                 $ret = @imagepng($thumbObj, $destPath);
409                                 break;
410                 }
411                 // イメージを破棄
412                 $ret = imagedestroy($imageObj);
413                 $ret = imagedestroy($thumbObj);
414                 return $ret;
415         }
416         
417         /**
418          * リサイズ画像を作成
419          *
420          * @param string $path          元の画像ファイル
421          * @param string $destPath      出力画像ファイルパス
422          * @param int,array $size       画像縦横最大サイズ(intの場合は縦横が収まる最大サイズ、arrayの場合は縦横の最大サイズ(width,height))
423          * @param int $type                     出力画像タイプ
424          * @param array destSize        作成画像の縦横
425          * @param int $imageQuality     画像品質
426          * @return bool                         true=作成,false=作成失敗
427          */
428         function createImage($path, $destPath, $size, $type, &$destSize, $imageQuality = 100)
429         {
430                 $imageSize = @getimagesize($path);
431                 if ($imageSize){
432                         $imageType = $imageSize[2];
433                         $srcWidth = $imageSize[0];
434                         $srcHeight = $imageSize[1];
435                 } else {
436                         return false;
437                 }
438
439                 // 画像オブジェクト作成
440                 switch ($imageType){
441                         case IMAGETYPE_GIF:
442                                 $imageObj = @imagecreatefromgif($path);
443                                 break;
444                         case IMAGETYPE_JPEG:
445                                 $imageObj = @imagecreatefromjpeg($path);
446                                 break;
447                         case IMAGETYPE_PNG:
448                                 $imageObj = @imagecreatefrompng($path);
449                                 break;
450                         default:                // 処理不可の画像タイプの場合は終了
451                                 return false;
452                 }
453                 
454                 // 画像のサイズを求める
455                 if (is_array($size)){           // 配列の場合は縦横サイズ指定
456                         $destWidth = $size['width'];
457                         $destHeight = $size['height'];
458                 } else {
459                         $destWidth = $srcWidth;
460                         $destHeight = $srcHeight;
461                         if ($srcWidth > $srcHeight){
462                                 if ($srcWidth > $size){
463                                         $destWidth = $size;
464                                         $destHeight = round($srcHeight * ($size / $srcWidth));
465                                 }
466                         } else {
467                                 if ($srcHeight > $size){
468                                         $destWidth = round($srcWidth * ($size / $srcHeight));
469                                         $destHeight = $size;
470                                 }
471                         }
472                 }
473                 
474                 // 出力用の画像オブジェクト作成
475                 $destImageObj = imagecreatetruecolor($destWidth, $destHeight);
476                 
477                 // 画像作成
478                 if (function_exists("imagecopyresampled")){
479                         if (!imagecopyresampled($destImageObj, $imageObj, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight)){
480                                 if (!imagecopyresized($destImageObj, $imageObj, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight)) return false;
481                         }
482                 } else {
483                         if (!imagecopyresized($destImageObj, $imageObj, 0, 0, 0, 0, $destWidth, $destHeight, $srcWidth, $srcHeight)) return false;
484                 }
485                 
486                 // 画像ファイル出力
487                 switch ($type){
488                         case IMAGETYPE_GIF:
489                                 $ret = @imagegif($destImageObj, $destPath, $imageQuality);
490                                 break;
491                         case IMAGETYPE_JPEG:
492                                 $ret = @imagejpeg($destImageObj, $destPath, $imageQuality);
493                                 break;
494                         case IMAGETYPE_PNG:
495                                 //$ret = @imagepng($destImageObj, $destPath, $imageQuality);            // PNGでは$imageQualityを使用すると画像が0サイズで作成される
496                                 $ret = @imagepng($destImageObj, $destPath);
497                                 break;
498                 }
499                 // イメージを破棄
500                 $ret = imagedestroy($imageObj);
501                 $ret = imagedestroy($destImageObj);
502                 
503                 // 画像サイズの再設定
504                 $destSize = array('width' => $destWidth, 'height' => $destHeight);
505                 return $ret;
506         }
507         /**
508          * コンテンツから先頭の画像のパスを取得
509          *
510          * @param string $html          検索コンテンツ
511          * @return string                       画像パス(取得できない場合は空文字列)
512          */
513         function getFirstImagePath($html)
514         {
515                 global $gEnvManager;
516                 
517                 $exp = '/<img[^<]*?src\s*=\s*[\'"]+(.+?)[\'"]+[^>]*?>/si';
518                 $ret = preg_match($exp, $html, $matches);
519                 if ($ret){
520                         $path = $gEnvManager->getMacroPath($matches[1]);                // 画像URL
521                         $path = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $gEnvManager->getSystemRootPath(), $path);// マクロ変換
522                         if (is_readable($path)) return $path;
523                 }
524                 return '';
525         }
526         /**
527          * サイトロゴ画像のURLを取得
528          *
529          * @return string                               画像URL
530          */
531         function getSiteLogoUrl()
532         {
533                 global $gEnvManager;
534                 global $gSystemManager;
535                 
536                 $siteLogoFiles = explode(';', $gSystemManager->getSystemConfig(self::CF_SITE_LOGO_FILENAME));           // サイトロゴファイル名
537                 $path = $gEnvManager->getResourcePath() . self::SITE_LOGO_DIR . $siteLogoFiles[count($siteLogoFiles) -1];
538                 $url = '';
539                 if (is_readable($path)) $url = $gEnvManager->getResourceUrl() . self::SITE_LOGO_DIR . $siteLogoFiles[count($siteLogoFiles) -1];
540                 return $url;
541         }
542         /**
543          * アバター画像のURLを取得
544          *
545          * @param string $filename              画像ファイル名。空の場合はデフォルトアバター画像。
546          * @return string                               画像URL。存在しない場合はデフォルト画像URLを取得
547          */
548         function getAvatarUrl($filename = '')
549         {
550                 global $gEnvManager;
551                 
552                 if (empty($filename)){
553                         $filename = self::DEFAULT_AVATAR_BASE . $this->getDefaultAvatarFormat();
554                 } else {
555                         $path = $gEnvManager->getResourcePath() . self::AVATAR_DIR . $filename;
556                         if (!is_readable($path)) $filename = self::DEFAULT_AVATAR_BASE . $this->getDefaultAvatarFormat();
557                 }
558                 $url = $gEnvManager->getResourceUrl() . self::AVATAR_DIR . $filename;
559                 return $url;
560         }
561         /**
562          * アバター画像のパスを取得
563          *
564          * @param string $filename              画像ファイル名。空の場合はデフォルトアバター画像。
565          * @return string                               画像パス
566          */
567         function getAvatarPath($filename = '')
568         {
569                 global $gEnvManager;
570                 
571                 if (empty($filename)) $filename = self::DEFAULT_AVATAR_BASE . $this->getDefaultAvatarFormat();
572                 $path = $gEnvManager->getResourcePath() . self::AVATAR_DIR . $filename;
573                 return $path;
574         }
575         /**
576          * アバターデフォルトフォーマットを取得
577          *
578          * @return string                               フォーマット
579          */
580         function getDefaultAvatarFormat()
581         {
582                 global $gSystemManager;
583                 
584                 $formats = explode(';', $gSystemManager->getSystemConfig(self::CF_AVATAR_FORMAT));              // アバターフォーマット
585                 $format = $formats[count($formats) -1];         // 最後(最大)の画像
586                 return $format;
587         }
588         /**
589          * OGP用サムネールデフォルトフォーマットを取得
590          *
591          * @return string                               フォーマット
592          */
593         function getDefaultOGPThumbFormat()
594         {
595                 global $gSystemManager;
596                 
597                 $format = $gSystemManager->getSystemConfig(self::CF_OGP_THUMB_FORMAT);          // OGPサムネールフォーマット
598                 return $format;
599         }
600         /**
601          * 指定フォーマットの画像を作成
602          *
603          * @param string $path                          元の画像ファイル
604          * @param string $formats                       作成する画像フォーマット(複数の場合は「;」区切り)
605          * @param string $destDir                       作成する画像のディレクトリ
606          * @param string $destFilenameBase      作成する画像ファイル名基本部。ファイル名は「基本部_フォーマット」で作成。
607          * @param array  $destFilename          作成した画像のファイル名
608          * @return bool                                         true=作成,false=作成失敗
609          */
610         function createImageByFormat($path, $formats, $destDir, $destFilenameBase, &$destFilename)
611         {
612                 // 引数エラーチェック
613                 if (empty($formats)) return false;
614                 
615                 $destFilename = array();                // 画像ファイル名
616                 
617                 // 画像のフォーマットを取得
618                 $formatArray = explode(';', $formats);
619                 
620                 for ($i = 0; $i < count($formatArray); $i++){
621                         $format = $formatArray[$i];
622                         $ret = $this->parseImageFormat($format, $imageType, $imageAttr, $imageSize);
623                         if ($ret){
624                                 $newFilename = $destFilenameBase . '_' . $format;
625                                 $newPath = $destDir . DIRECTORY_SEPARATOR . $newFilename;
626
627                                 // サムネールの作成
628                                 if ($imageAttr == 'c'){         // 切り取りサムネールの場合
629                                         //$ret = $this->gInstance->getImageManager()->createThumb($path, $newPath, $imageSize, $imageType, true);
630                                         $ret = $this->createThumb($path, $newPath, $imageSize, $imageType, true);
631                                 } else {
632                                         //$ret = $this->gInstance->getImageManager()->createThumb($path, $newPath, $imageSize, $imageType, false);
633                                         $ret = $this->createThumb($path, $newPath, $imageSize, $imageType, false);
634                                 }
635                                 if ($ret){
636                                         $destFilename[] = $newFilename;
637                                 } else {
638                                         return false;
639                                 }
640                         } else {
641                                 break;
642                         }
643                 }
644                 return $ret;
645         }
646         /**
647          * フォーマットから画像サイズ、画像タイプを取得
648          *
649          * @param string $format                画像フォーマット
650          * @param string $imageType             画像タイプ
651          * @param string $imageAttr             画像属性(c=切り取りあり)
652          * @param string,array $imageSize               画像サイズ(縦横異なる場合は連想配列(width,height))
653          * @return bool                                 true=取得成功、false=取得失敗
654          */
655         function parseImageFormat($format, &$imageType, &$imageAttr, &$imageSize)
656         {
657                 $format = trim($format);
658                 //$ret = preg_match('/(\d+)(.*)\.(gif|png|jpg|jpeg|bmp)$/i', $format, $matches);
659                 $ret = preg_match('/(\d+)([xX]?)(\d*)(.*)\.(gif|png|jpg|jpeg|bmp)$/i', $format, $matches);      // 「MMxNNc.jpg」タイプのフォーマットを解析
660                 if ($ret){
661                         $imageSize = $matches[1];
662                         $height = intval($matches[3]);
663                         if ($height > 0){
664                                 $imageSize = array('width' => $imageSize, 'height' => $height);
665                         }
666                         //$imageAttr = strtolower($matches[2]);
667                         //$ext = strtolower($matches[3]);
668                         $imageAttr = strtolower($matches[4]);
669                         $ext = strtolower($matches[5]);
670                         
671                         $imageType = IMAGETYPE_JPEG;
672                         switch ($ext){
673                                 case 'jpg':
674                                 case 'jpeg':
675                                         $imageType = IMAGETYPE_JPEG;
676                                         break;
677                                 case 'gif':
678                                         $imageType = IMAGETYPE_GIF;
679                                         break;
680                                 case 'png':
681                                         $imageType = IMAGETYPE_PNG;
682                                         break;
683                                 case 'bmp':
684                                         $imageType = IMAGETYPE_BMP;
685                                         break;
686                         }
687                 }
688                 return $ret;
689         }
690         /**
691          * 画像未設定時に表示する画像のURLを取得
692          *
693          * @param string $type          画像のタイプ(ogp=OGP用サムネール)
694          * @return string                       画像URL
695          */
696         function getNotAvailableImageUrl($type)
697         {
698                 global $gEnvManager;
699                         
700                 switch ($type){
701                         case 'ogp':
702                                 $url = $gEnvManager->getResourceUrl() . self::DEFAULT_THUMB_DIR . self::NOT_AVAILABLE_HEAD . $this->getDefaultOGPThumbFormat();
703                                 break;
704                 }
705                 return $url;
706         }
707         /**
708          * 画像未設定時に表示する画像のパスを取得
709          *
710          * @param string $type          画像のタイプ(ogp=OGP用サムネール)
711          * @return string                       画像URL
712          */
713         function getNotAvailableImagePath($type)
714         {
715                 global $gEnvManager;
716                         
717                 switch ($type){
718                         case 'ogp':
719                                 $path = $gEnvManager->getResourcePath() . self::DEFAULT_THUMB_DIR . self::NOT_AVAILABLE_HEAD . $this->getDefaultOGPThumbFormat();
720                                 break;
721                 }
722                 return $path;
723         }
724         /**
725          * 画像の情報を取得
726          *
727          * @param string $path          画像ファイル
728          * @param int $width            画像幅
729          * @param int $height           画像高さ
730          * @return bool                         true=取得,false=取得失敗
731          */
732         function getImageInfo($path, &$width, &$height)
733         {
734                 $imageSize = @getimagesize($path);
735                 if ($imageSize){
736                         $imageType = $imageSize[2];
737                         $width = $imageSize[0];
738                         $height = $imageSize[1];
739                         return true;
740                 } else {
741                         return false;
742                 }
743         }
744 }
745 ?>