[18233] | 1 | <?php |
---|
[16582] | 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
[22206] | 5 | * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. |
---|
[16582] | 6 | * |
---|
| 7 | * http://www.lockon.co.jp/ |
---|
| 8 | * |
---|
| 9 | * This program is free software; you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License |
---|
| 11 | * as published by the Free Software Foundation; either version 2 |
---|
| 12 | * of the License, or (at your option) any later version. |
---|
| 13 | * |
---|
| 14 | * This program is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this program; if not, write to the Free Software |
---|
| 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
[18233] | 22 | * |
---|
| 23 | * 端末の画面解像度にあわせて画像を変換する |
---|
| 24 | */ |
---|
| 25 | |
---|
[21514] | 26 | define('MOBILE_IMAGE_INC_REALDIR', realpath(dirname( __FILE__)) . '/../include/'); |
---|
[20534] | 27 | require_once MOBILE_IMAGE_INC_REALDIR . 'image_converter.inc'; |
---|
[18233] | 28 | |
---|
| 29 | /** |
---|
| 30 | * 画像変換クラス |
---|
| 31 | */ |
---|
[22856] | 32 | class SC_MobileImage |
---|
[22567] | 33 | { |
---|
[18233] | 34 | /** |
---|
| 35 | * 画像を端末の解像度に合わせて変換する |
---|
| 36 | * output buffering 用コールバック関数 |
---|
| 37 | * |
---|
| 38 | * @param string 入力 |
---|
| 39 | * @return string 出力 |
---|
| 40 | */ |
---|
[23124] | 41 | public static function handler($buffer) |
---|
[22567] | 42 | { |
---|
[18233] | 43 | // 端末情報を取得する |
---|
[20495] | 44 | $carrier = SC_MobileUserAgent_Ex::getCarrier(); |
---|
| 45 | $model = SC_MobileUserAgent_Ex::getModel(); |
---|
[18233] | 46 | |
---|
| 47 | // 携帯電話の場合のみ処理を行う |
---|
| 48 | if ($carrier !== FALSE) { |
---|
| 49 | // HTML中のIMGタグを取得する |
---|
[21926] | 50 | $images = array(); |
---|
[21209] | 51 | $pattern = '/<img\s+[^<>]*src=[\'"]?([^>"\'\s]+)[\'"]?[^>]*>/i'; |
---|
[18233] | 52 | $result = preg_match_all($pattern, $buffer, $images); |
---|
| 53 | |
---|
| 54 | // 端末の情報を取得する |
---|
[20538] | 55 | $fp = fopen(MOBILE_IMAGE_INC_REALDIR . "mobile_image_map_$carrier.csv", 'r'); |
---|
[20920] | 56 | // 取得できない場合は, 入力内容をそのまま返す |
---|
| 57 | if ($fp === false) { |
---|
| 58 | return $buffer; |
---|
| 59 | } |
---|
[21514] | 60 | while (($data = fgetcsv($fp, 1000, ',')) !== FALSE) { |
---|
[18233] | 61 | if ($data[1] == $model || $data[1] == '*') { |
---|
| 62 | $cacheSize = $data[2]; |
---|
| 63 | $imageFileSize = $data[7]; |
---|
| 64 | $imageType = $data[6]; |
---|
| 65 | $imageWidth = $data[5]; |
---|
| 66 | $imageHeight = $data[4]; |
---|
| 67 | break; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | fclose($fp); |
---|
| 71 | |
---|
| 72 | // docomoとsoftbankの場合は画像ファイル一つに利用可能なサイズの上限を計算する |
---|
| 73 | // auはHTMLのbyte数上限に画像ファイルサイズが含まれないのでimageFileSizeのまま。 |
---|
[20538] | 74 | if ($carrier == 'docomo' or $carrier == 'softbank') { |
---|
[21441] | 75 | if ($result != false && $result > 0) { |
---|
[21444] | 76 | // 計算式:(利用端末で表示可能なcacheサイズ - HTMLのバイト数 - 変換後の画像名のバイト数(目安値)) / HTML中の画像数 |
---|
| 77 | $temp_imagefilesize = ($cacheSize - strlen($buffer) - (140 * $result)) / $result; |
---|
[20562] | 78 | } else { |
---|
[21444] | 79 | // 計算式:(利用端末で表示可能なcacheサイズ - HTMLのバイト数) |
---|
| 80 | $temp_imagefilesize = ($cacheSize - strlen($buffer)); |
---|
[20562] | 81 | } |
---|
[18233] | 82 | // 計算結果が端末の表示可能ファイルサイズ上限より小さい場合は計算結果の値を有効にする |
---|
| 83 | if ($temp_imagefilesize < $imageFileSize) { |
---|
| 84 | $imageFileSize = $temp_imagefilesize; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // 画像変換の情報をセットする |
---|
| 89 | $imageConverter = New ImageConverter(); |
---|
[19807] | 90 | $imageConverter->setOutputDir(MOBILE_IMAGE_REALDIR); |
---|
[18233] | 91 | $imageConverter->setImageType($imageType); |
---|
| 92 | $imageConverter->setImageWidth($imageWidth); |
---|
| 93 | $imageConverter->setImageHeight($imageHeight); |
---|
| 94 | $imageConverter->setFileSize($imageFileSize); |
---|
| 95 | |
---|
| 96 | // HTML中のIMGタグを変換後のファイルパスに置換する |
---|
[18375] | 97 | foreach ($images[1] as $key => $path) { |
---|
[21776] | 98 | // resize_image.phpは除外 |
---|
| 99 | if (stripos($path, ROOT_URLPATH . 'resize_image.php') !== FALSE) { |
---|
[23025] | 100 | continue; |
---|
[21776] | 101 | } |
---|
| 102 | |
---|
[18375] | 103 | $realpath = html_entity_decode($path, ENT_QUOTES); |
---|
[23025] | 104 | $realpath = substr_replace($realpath, HTML_REALDIR, 0, strlen(ROOT_URLPATH)); |
---|
[18375] | 105 | $converted = $imageConverter->execute($realpath); |
---|
[18233] | 106 | if (isset($converted['outputImageName'])) { |
---|
[21447] | 107 | $buffer = str_replace($path, MOBILE_IMAGE_URLPATH . $converted['outputImageName'], $buffer); |
---|
[18233] | 108 | } else { |
---|
| 109 | $buffer = str_replace($images[0][$key], '<!--No image-->', $buffer); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
[22856] | 113 | |
---|
[18233] | 114 | return $buffer; |
---|
| 115 | } |
---|
| 116 | } |
---|