PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/data/class/SC_Image.php

https://gitlab.com/raku.takayama/eccube-2_13
PHP | 242 lines | 158 code | 34 blank | 50 comment | 41 complexity | 438039baa70645c117e25a1223c8077b MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of EC-CUBE
  4. *
  5. * Copyright(c) 2000-2014 LOCKON CO.,LTD. All Rights Reserved.
  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.
  22. */
  23. //---- アップロードファイル加工クラス(thumb.phpとセットで使用する)
  24. class SC_Image
  25. {
  26. public $tmp_dir;
  27. public function __construct($tmp_dir)
  28. {
  29. // ヘッダファイル読込
  30. $this->tmp_dir = rtrim($tmp_dir, '/') . '/';
  31. }
  32. //--- 一時ファイル生成(サムネイル画像生成用)
  33. public function makeTempImage($keyname, $max_width, $max_height)
  34. {
  35. // 一意なIDを取得する。
  36. $mainname = uniqid('').'.';
  37. // 拡張子以外を置き換える。
  38. $newFileName = preg_replace("/^.*\./", $mainname, $_FILES[$keyname]['name']);
  39. $result = $this->MakeThumb($_FILES[$keyname]['tmp_name'], $this->tmp_dir, $max_width, $max_height, $newFileName);
  40. GC_Utils_Ex::gfDebugLog($result);
  41. return $newFileName;
  42. }
  43. //--- ファイルを指定保存DIRへ移動
  44. public function moveTempImage($filename, $save_dir)
  45. {
  46. // コピー元ファイル、コピー先ディレクトリが存在する場合にのみ実行する
  47. $from_path = $this->tmp_dir.$filename;
  48. $to_path = $save_dir.'/'.$filename;
  49. if (file_exists($from_path) && file_exists($save_dir)) {
  50. if (copy($from_path, $to_path)) {
  51. unlink($from_path);
  52. }
  53. } else {
  54. GC_Utils_Ex::gfDebugLog($from_path.'->'.$to_path.'のcopyに失敗しました。');
  55. }
  56. }
  57. //---- 指定ファイルを削除
  58. public function deleteImage($filename, $dir)
  59. {
  60. if (file_exists($dir.'/'.$filename)) {
  61. unlink($dir.'/'.$filename);
  62. }
  63. }
  64. /**
  65. * 指定サイズで画像を出力する.
  66. *
  67. * @param string $FromImgPath ファイル名までのパス
  68. * @param string $ToImgPath 出力先パス
  69. * @param integer $tmpMW 最大横幅
  70. * @param integer $tmpMH 最大縦幅
  71. * @param integer $newFileName 新ファイル名
  72. * @param array 新ファイル名を格納した配列
  73. */
  74. public function MakeThumb($FromImgPath, $ToImgPath, $tmpMW, $tmpMH, $newFileName = '')
  75. {
  76. // 画像の最大横幅(単位:ピクセル)
  77. $ThmMaxWidth = LARGE_IMAGE_WIDTH;
  78. // 画像の最大縦幅(単位:ピクセル)
  79. $ThmMaxHeight = LARGE_IMAGE_HEIGHT;
  80. //拡張子取得
  81. $array_ext = explode('.', $FromImgPath);
  82. $ext = $array_ext[count($array_ext) - 1];
  83. $MW = $ThmMaxWidth;
  84. if ($tmpMW) $MW = $tmpMW; // $MWに最大横幅セット
  85. $MH = $ThmMaxHeight;
  86. if ($tmpMH) $MH = $tmpMH; // $MHに最大縦幅セット
  87. if (empty($FromImgPath) || empty($ToImgPath)) {
  88. return array(0, '出力元画像パス、または出力先フォルダが指定されていません。');
  89. }
  90. if (!file_exists($FromImgPath)) {
  91. return array(0, '出力元画像が見つかりません。');
  92. }
  93. $size = @GetImageSize($FromImgPath);
  94. $re_size = $size;
  95. // 画像の種類が不明 or swf
  96. if (!$size[2] || $size[2] > 3) {
  97. return array(0, '画像形式がサポートされていません。');
  98. }
  99. //アスペクト比固定処理
  100. $tmp_w = $size[0] / $MW;
  101. if ($MH != 0) {
  102. $tmp_h = $size[1] / $MH;
  103. }
  104. if ($tmp_w > 1 || $tmp_h > 1) {
  105. if ($MH == 0) {
  106. if ($tmp_w > 1) {
  107. $re_size[0] = $MW;
  108. $re_size[1] = $size[1] * $MW / $size[0];
  109. }
  110. } else {
  111. if ($tmp_w > $tmp_h) {
  112. $re_size[0] = $MW;
  113. $re_size[1] = $size[1] * $MW / $size[0];
  114. } else {
  115. $re_size[1] = $MH;
  116. $re_size[0] = $size[0] * $MH / $size[1];
  117. }
  118. }
  119. }
  120. // サムネイル画像ファイル名作成処理
  121. $tmp = array_pop(explode('/', $FromImgPath)); // /の一番最後を切り出し
  122. $FromFileName = array_shift(explode('.', $tmp)); // .で区切られた部分を切り出し
  123. $ToFile = $FromFileName; // 拡張子以外の部分までを作成
  124. $ImgNew = imagecreatetruecolor($re_size[0], $re_size[1]);
  125. switch ($size[2]) {
  126. case '1': //gif形式
  127. if ($tmp_w <= 1 && $tmp_h <= 1) {
  128. if ($newFileName) {
  129. $ToFile = $newFileName;
  130. } elseif ($ext) {
  131. $ToFile .= '.' . $ext;
  132. } else {
  133. $ToFile .= '.gif';
  134. }
  135. if (!@copy($FromImgPath, $ToImgPath.$ToFile)) { // エラー処理
  136. return array(0, 'ファイルのコピーに失敗しました。');
  137. }
  138. ImageDestroy($ImgNew);
  139. return array(1, $ToFile);
  140. }
  141. ImageColorAllocate($ImgNew, 255, 235, 214); //背景色
  142. $black = ImageColorAllocate($ImgNew, 0, 0, 0);
  143. $red = ImageColorAllocate($ImgNew, 255, 0, 0);
  144. Imagestring($ImgNew, 4, 5, 5, "GIF $size[0]x$size[1]", $red);
  145. ImageRectangle($ImgNew, 0, 0, ($re_size[0]-1), ($re_size[1]-1), $black);
  146. if ($newFileName) {
  147. $ToFile = $newFileName;
  148. } elseif ($ext) {
  149. $ToFile .= '.' . $ext;
  150. } else {
  151. $ToFile .= '.png';
  152. }
  153. $TmpPath = $ToImgPath.$ToFile;
  154. @Imagepng($ImgNew, $TmpPath);
  155. // 画像が作成されていない場合
  156. if (!@file_exists($TmpPath)) {
  157. return array(0, '画像の出力に失敗しました。');
  158. }
  159. ImageDestroy($ImgNew);
  160. return array(1, $ToFile);
  161. case '2': //jpg形式
  162. $ImgDefault = ImageCreateFromJpeg($FromImgPath);
  163. //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
  164. if ($re_size[0] != $size[0] || $re_size[0] != $size[0]) {
  165. ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
  166. }
  167. GC_Utils_Ex::gfDebugLog($size);
  168. GC_Utils_Ex::gfDebugLog($re_size);
  169. if ($newFileName) {
  170. $ToFile = $newFileName;
  171. } elseif ($ext) {
  172. $ToFile .= '.' . $ext;
  173. } else {
  174. $ToFile .= '.jpg';
  175. }
  176. $TmpPath = $ToImgPath.$ToFile;
  177. @ImageJpeg($ImgNew, $TmpPath);
  178. // 画像が作成されていない場合
  179. if (!@file_exists($TmpPath)) {
  180. return array(0, "画像の出力に失敗しました。<br>${ImgNew}<br>${TmpPath}");
  181. }
  182. $RetVal = $ToFile;
  183. break;
  184. case '3': //png形式
  185. $ImgDefault = ImageCreateFromPNG($FromImgPath);
  186. //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
  187. ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);
  188. if ($newFileName) {
  189. $ToFile = $newFileName;
  190. } elseif ($ext) {
  191. $ToFile .= '.' . $ext;
  192. } else {
  193. $ToFile .= '.png';
  194. }
  195. $TmpPath = $ToImgPath.$ToFile;
  196. @ImagePNG($ImgNew, $TmpPath);
  197. // 画像が作成されていない場合
  198. if (!@file_exists($TmpPath)) {
  199. return array(0, '画像の出力に失敗しました。');
  200. }
  201. $RetVal = $ToFile;
  202. break;
  203. }
  204. ImageDestroy($ImgDefault);
  205. ImageDestroy($ImgNew);
  206. return array(1, $RetVal);
  207. }
  208. }