Completed
Pull Request — master (#32)
by satoru
08:52
created

ImageContentsFileBehaviorTrait::setTPinfo()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 6.1403
cc 8
eloc 14
nc 14
nop 3
1
<?php
2
3
namespace ContentsFile\Model\Behavior\Traits;
4
5
/**
6
 * ImageContentsFileBehaviorTrait
7
 * 画像関係の処理
8
 */
9
trait ImageContentsFileBehaviorTrait
10
{
11
    private $tp;
12
13
    /**
14
     * imageResize
15
     * 画像のリサイズ処理(外からでもたたけるようにpublicにする
16
     * @author hagiwara
17
     * @param string $imagePath
18
     * @param array $baseSize
19
     */
20
    public function imageResize($imagePath, $baseSize) {
21
22
        $imageInfo = $this->getImageInfo($imagePath);
23
        $image = $imageInfo['image'];
24
        $imagetype = $imageInfo['imagetype'];
25
        if (!$image) {
26
            // 画像の読み込み失敗
27
            return false;
28
        }
29
        // // 画像の縦横サイズを取得
30
        $imageSizeInfo = $this->imageSizeInfo($image, $baseSize);
31
32
        return $this->imageResizeMake($image, $imagetype, $imagePath, $baseSize, $imageSizeInfo);
33
    }
34
35
    /**
36
     * getImageInfo
37
     * 画像情報の取得
38
     * @author hagiwara
39
     * @param string $imagePath
40
     */
41
    private function getImageInfo($imagePath)
42
    {
43
        if (file_exists($imagePath) === false) {
44
            return false;
45
        }
46
47
        $imagetype = exif_imagetype($imagePath);
48
        if ($imagetype === false) {
49
            return false;
50
        }
51
52
        // 画像読み込み
53
        switch ($imagetype) {
54
            case IMAGETYPE_GIF:
55
                $image = ImageCreateFromGIF($imagePath);
56
                break;
57
            case IMAGETYPE_JPEG:
58
                $image = ImageCreateFromJPEG($imagePath);
59
                break;
60
            case IMAGETYPE_PNG:
61
                $image = ImageCreateFromPNG($imagePath);
62
                break;
63
            default:
64
                $image = false;
65
        }
66
        return [
67
            'image' => $image,
68
            'imagetype' => $imagetype,
69
        ];
70
    }
71
72
    /**
73
     * imageSizeInfo
74
     * 画像リサイズ情報の取得
75
     * @author hagiwara
76
     * @param resource $image
77
     * @param array $baseSize
78
     */
79
    private function imageSizeInfo($image, $baseSize)
80
    {
81
        // 画像の縦横サイズを取得
82
        $sizeX = ImageSX($image);
83
        $sizeY = ImageSY($image);
84
        // リサイズ後のサイズ
85
        if (!array_key_exists('height', $baseSize)) {
86
            $baseSize['height'] = 0;
87
        }
88
        if (!array_key_exists('width', $baseSize)) {
89
            $baseSize['width'] = 0;
90
        }
91
        // リサイズ種別
92
        if (!array_key_exists('type', $baseSize)) {
93
            $baseSize['type'] = 'normal';
94
        }
95
96
        if ($baseSize['type'] == 'normal_s' || $baseSize['type'] == 'scoop') {
97
            // 短い方基準もしくは、くりぬき
98 View Code Duplication
            if (empty($baseSize['width']) || !empty($baseSize['height']) && $sizeX * $baseSize['height'] < $sizeY * $baseSize['width']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
                // 縦基準
100
                $mag = $baseSize['width'] / $sizeX;
101
                $reSizeX = $baseSize['width'];
102
                $reSizeY = $sizeY * $mag;
103
            } else {
104
                // 横基準
105
                $mag = $baseSize['height'] / $sizeY;
106
                $reSizeY = $baseSize['height'];
107
                $reSizeX = $sizeX * $mag;
108
            }
109 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
            // 長い方基準
111
            if (empty($baseSize['width']) || !empty($baseSize['height']) && $sizeX * $baseSize['height'] < $sizeY * $baseSize['width']) {
112
                // 縦基準
113
                $mag = $baseSize['height'] / $sizeY;
114
                $reSizeY = $baseSize['height'];
115
                $reSizeX = $sizeX * $mag;
116
            } else {
117
                // 横基準
118
                $mag = $baseSize['width'] / $sizeX;
119
                $reSizeX = $baseSize['width'];
120
                $reSizeY = $sizeY * $mag;
121
            }
122
        }
123
        return [
124
            'sizeX' => $sizeX,
125
            'sizeY' => $sizeY,
126
            'reSizeX' => $reSizeX,
127
            'reSizeY' => $reSizeY,
128
            'type' => $baseSize['type'],
129
        ];
130
    }
131
132
    /**
133
     * imageResizeMake
134
     * 画像リサイズ情報の取得
135
     * @author hagiwara
136
     * @param resource $image
137
     * @param integer $imagetype
138
     * @param string $imagePath
139
     * @param array $baseSize
140
     * @param array $imageSizeInfo
141
     */
142
    private function imageResizeMake($image, $imagetype, $imagePath, $baseSize, $imageSizeInfo)
143
    {
144
        // サイズ変更後の画像データを生成
145
        $campusX = $imageSizeInfo['reSizeX'];
146
        $campusY = $imageSizeInfo['reSizeY'];
147
        // くりぬきの場合(幅と高さが両方必要)
148
        if ($imageSizeInfo['type'] == 'scoop' && !empty($baseSize['width']) && !empty($baseSize['height'])) {
149
            $campusX = $baseSize['width'];
150
            $campusY = $baseSize['height'];
151
        }
152
        $outImage = ImageCreateTrueColor($campusX, $campusY);
153
        if (!$outImage) {
154
            // リサイズ後の画像作成失敗
155
            return false;
156
        }
157
158
        //透過GIF.PNG対策
159
        //ブレンドモードを無効にする
160
        imagealphablending($outImage, false);
161
        //完全なアルファチャネル情報を保存するフラグをonにする
162
        imagesavealpha($outImage, true);
163
        //!透過GIF.PNG対策
164
        // 画像リサイズ
165
        
166
        $diffX = 0;
167
        $diffY = 0;
168
        // くりぬきの場合(幅と高さが両方必要)
169
        if ($imageSizeInfo['type'] == 'scoop' && !empty($baseSize['width']) && !empty($baseSize['height'])) {
170
            $diffX = ($imageSizeInfo['sizeX'] - ($baseSize['width'] * $imageSizeInfo['sizeX'] / $imageSizeInfo['reSizeX'])) / 2;
171
            $diffY = ($imageSizeInfo['sizeY'] - ($baseSize['height'] * $imageSizeInfo['sizeY'] / $imageSizeInfo['reSizeY'])) / 2;
172
        }
173
        $ret = imagecopyresampled($outImage, $image, 0, 0, $diffX, $diffY, $imageSizeInfo['reSizeX'], $imageSizeInfo['reSizeY'], $imageSizeInfo['sizeX'], $imageSizeInfo['sizeY']);
174
        if ($ret === false) {
175
            // リサイズ失敗
176
            return false;
177
        }
178
179
        ImageDestroy($image);
180
181
        // 画像保存
182
        $imagepathinfo = $this->getPathInfo($imagePath, $baseSize);
183
        //resizeファイルを格納するディレクトリを作成
184
        if (
185
            !$this->mkdir($imagepathinfo['resize_dir'], 0777, true)
0 ignored issues
show
Bug introduced by
It seems like mkdir() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
186
        ) {
187
            return false;
188
        }
189
190
        switch ($imagetype) {
191
            case IMAGETYPE_GIF:
192
                ImageGIF($outImage, $imagepathinfo['resize_filepath']);
193
                break;
194
            case IMAGETYPE_JPEG:
195
                ImageJPEG($outImage, $imagepathinfo['resize_filepath'], 100);
196
                break;
197
            case IMAGETYPE_PNG:
198
                ImagePNG($outImage, $imagepathinfo['resize_filepath']);
199
                break;
200
            default:
201
                return false;
202
        }
203
204
        ImageDestroy($outImage);
205
206
        return true;
207
    }
208
209
    /**
210
     * getPathInfo
211
     * 通常のpathinfoに加えてContentsFile独自のpathも一緒に設定する
212
     * @author hagiwara
213
     * @param string $imagePath
214
     * @param array $resize
215
     */
216
    public function getPathInfo($imagePath, $resize = []) {
217
        $pathinfo = pathinfo($imagePath);
218
        $pathinfo['resize_dir'] = $pathinfo['dirname'] . '/contents_file_resize_' . $pathinfo['filename'];
219
        //一旦ベースのパスを通しておく
220
        $pathinfo['resize_filepath'] = $imagePath;
221
        if (!empty($resize)) {
222
            if (!isset($resize['width'])) {
223
                $resize['width'] = 0;
224
            }
225
            if (!isset($resize['height'])) {
226
                $resize['height'] = 0;
227
            }
228
            if (!isset($resize['type'])) {
229
                $resize['type'] = 'normal';
230
            }
231
            $pathinfo['resize_filepath'] = $pathinfo['resize_dir'] . '/' . $resize['width'] . '_' . $resize['height'] . '_' . $resize['type'];
232
        }
233
        return $pathinfo;
234
    }
235
}
236