| 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * 画像ファイルの変換を行う
|
---|
| 4 | */
|
---|
| 5 | class ImageConverter {
|
---|
| 6 | var $outputImageDir; // 変換後の画像の保存先
|
---|
| 7 | var $outputImageType; // 変換後の画像の形式
|
---|
| 8 | var $outputImageWidth; // 変換後の画像の横幅
|
---|
| 9 | var $outputImageHeight; // 変換後の画像の高さ
|
---|
| 10 | var $outputImageFileSize; // 変換後の画像のファイルサイズ
|
---|
| 11 |
|
---|
| 12 | // コンストラクタ
|
---|
| 13 | function ImageConverter() {
|
---|
| 14 | $this->outputImageDir = realpath(realpath(dirname(__FILE__)));
|
---|
| 15 | $this->outputImageType = 'jpeg';
|
---|
| 16 | $this->outputImageWidth = 320;
|
---|
| 17 | $this->outputImageHeight = NULL;
|
---|
| 18 | $this->outputFileSize = 20000;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | // 変換実行
|
---|
| 22 | function execute($inputImagePath) {
|
---|
| 23 | // 前処理
|
---|
| 24 | $filestat = @stat($inputImagePath);
|
---|
| 25 | $imagesize = getimagesize($inputImagePath);
|
---|
| 26 | $inputImageWidth = $imagesize[0];
|
---|
| 27 | $inputImageHeight = $imagesize[1];
|
---|
| 28 | $inputImageType = $imagesize[2];
|
---|
| 29 | $outputImageName = sha1($inputImagePath . '_' . $this->outputImageWidth . '_' . $this->outputFileSize . '_' . $filestat['mtime']) . '.' . $this->outputImageType;
|
---|
| 30 | $outputImagePath = $this->outputImageDir . '/' . $outputImageName;
|
---|
| 31 |
|
---|
| 32 | // 変換後の画像の高さが指定されていない場合、変換後の画像の横幅から求める
|
---|
| 33 | if (is_null($this->outputImageHeight)) {
|
---|
| 34 | $this->outputImageHeight = $inputImageHeight * ($this->outputImageWidth / $inputImageWidth);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | // ファイルが存在するか確認し、存在しない場合のみ作成する
|
---|
| 38 | if (file_exists($outputImagePath)) {
|
---|
| 39 | $info['convert'] = FALSE;
|
---|
| 40 | } else {
|
---|
| 41 | // 元ファイル作成
|
---|
| 42 | switch($inputImageType) {
|
---|
| 43 | case 1:
|
---|
| 44 | // gif
|
---|
| 45 | $tempImage = imagecreatefromgif($inputImagePath);
|
---|
| 46 | break;
|
---|
| 47 | case 2:
|
---|
| 48 | // jpeg
|
---|
| 49 | $tempImage = imagecreatefromjpeg($inputImagePath);
|
---|
| 50 | break;
|
---|
| 51 | case 3:
|
---|
| 52 | // png
|
---|
| 53 | $tempImage = imagecreatefrompng($inputImagePath);
|
---|
| 54 | break;
|
---|
| 55 | case 6:
|
---|
| 56 | // bmp
|
---|
| 57 | $tempImage = imagecreatefromwbmp($inputImagePath);
|
---|
| 58 | break;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | if (!$tempImage) {
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | $scale = 1.0;
|
---|
| 66 | $outputImagePathTemp = $outputImagePath . '.tmp-' . rand();
|
---|
| 67 | do {
|
---|
| 68 | // 空ファイル作成
|
---|
| 69 | $outputImage = ImageCreateTruecolor($this->outputImageWidth * $scale, $this->outputImageHeight * $scale);
|
---|
| 70 | ImageCopyResampled($outputImage, $tempImage, 0, 0, 0, 0, $this->outputImageWidth * $scale, $this->outputImageHeight * $scale, $inputImageWidth, $inputImageHeight);
|
---|
| 71 |
|
---|
| 72 | // ファイル出力
|
---|
| 73 |
|
---|
| 74 | @unlink($outputImagePathTemp);
|
---|
| 75 |
|
---|
| 76 | switch ($this->outputImageType) {
|
---|
| 77 | case 1:
|
---|
| 78 | case 'gif':
|
---|
| 79 | imagegif($outputImage, $outputImagePathTemp);
|
---|
| 80 | break;
|
---|
| 81 | default:
|
---|
| 82 | case 2:
|
---|
| 83 | case 'jpg':
|
---|
| 84 | case 'jpeg':
|
---|
| 85 | $quality = 75;
|
---|
| 86 | // 表示可能なファイルサイズ以下になるまで、10%ずつクオリティを調整する
|
---|
| 87 | do {
|
---|
| 88 | @unlink($outputImagePathTemp);
|
---|
| 89 | imagejpeg($outputImage, $outputImagePathTemp, $quality);
|
---|
| 90 | $quality -= 10;
|
---|
| 91 | clearstatcache();
|
---|
| 92 | } while (filesize($outputImagePathTemp) > $this->outputFileSize && $quality > 0);
|
---|
| 93 | break;
|
---|
| 94 | case 3:
|
---|
| 95 | case 'png':
|
---|
| 96 | imagepng($outputImage, $outputImagePathTemp);
|
---|
| 97 | break;
|
---|
| 98 | case 6:
|
---|
| 99 | case 'bmp':
|
---|
| 100 | imagewbmp($outputImage, $outputImagePathTemp);
|
---|
| 101 | break;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | // メモリ開放
|
---|
| 105 | imagedestroy($outputImage);
|
---|
| 106 |
|
---|
| 107 | $scale -= 0.1;
|
---|
| 108 | clearstatcache();
|
---|
| 109 | } while (filesize($outputImagePathTemp) > $this->outputFileSize && $scale >= 0.5);
|
---|
| 110 |
|
---|
| 111 | rename($outputImagePathTemp, $outputImagePath);
|
---|
| 112 |
|
---|
| 113 | // メモリ開放
|
---|
| 114 | imagedestroy($tempImage);
|
---|
| 115 |
|
---|
| 116 | $info['convert'] = TRUE;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | $info['outputImagePath'] = $outputImagePath;
|
---|
| 120 | $info['outputImageName'] = $outputImageName;
|
---|
| 121 | return $info;
|
---|
| 122 |
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | // Setter
|
---|
| 126 | function setOutputDir($outputDir) { $this->outputImageDir = $outputDir; }
|
---|
| 127 | function setImageType($imageType) { $this->outputImageType = $imageType; }
|
---|
| 128 | function setImageWidth($imageWidth) { $this->outputImageWidth = $imageWidth; }
|
---|
| 129 | function setFileSize($fileSize) { $this->outputFileSize = $fileSize; }
|
---|
| 130 | // function setImageHeight($imageHeight) { $this->outputImageHeight = $imageHeight; }
|
---|
| 131 |
|
---|
| 132 | // Getter
|
---|
| 133 | function getOutputDir() { return $this->outputDir; }
|
---|
| 134 | function getImageType() { return $this->outputImageType; }
|
---|
| 135 | function getImageWidth() { return $this->outputImageWidth; }
|
---|
| 136 | function getImageHeight() { return $this->outputImageHeight; }
|
---|
| 137 |
|
---|
| 138 | /*
|
---|
| 139 | * PrivateMethod
|
---|
| 140 | */
|
---|
| 141 | function beforeExecute() {
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 | ?>
|
---|