Completed
Push — master ( 2825b9...504b57 )
by satoru
07:25 queued 05:25
created

ImageContentsFileBehaviorTrait::imageResizeMake()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 57
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 7.6759
c 0
b 0
f 0
cc 7
eloc 30
nc 7
nop 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 (empty($baseSize['width']) || !empty($baseSize['height']) && $sizeX * $baseSize['height'] < $sizeY * $baseSize['width']) {
93
            // 縦基準
94
            $mag = $baseSize['height'] / $sizeY;
95
            $reSizeY = $baseSize['height'];
96
            $reSizeX = $sizeX * $mag;
97
        } else {
98
            // 横基準
99
            $mag = $baseSize['width'] / $sizeX;
100
            $reSizeX = $baseSize['width'];
101
            $reSizeY = $sizeY * $mag;
102
        }
103
        return [
104
            'sizeX' => $sizeX,
105
            'sizeY' => $sizeY,
106
            'reSizeX' => $reSizeX,
107
            'reSizeY' => $reSizeY,
108
        ];
109
    }
110
111
    /**
112
     * imageResizeMake
113
     * 画像リサイズ情報の取得
114
     * @author hagiwara
115
     * @param resource $image
116
     * @param integer $imagetype
117
     * @param string $imagePath
118
     * @param array $baseSize
119
     * @param array $imageSizeInfo
120
     */
121
    private function imageResizeMake($image, $imagetype, $imagePath, $baseSize, $imageSizeInfo)
122
    {
123
        // サイズ変更後の画像データを生成
124
        $outImage = ImageCreateTrueColor($imageSizeInfo['reSizeX'], $imageSizeInfo['reSizeY']);
125
        if (!$outImage) {
126
            // リサイズ後の画像作成失敗
127
            return false;
128
        }
129
130
        //透過GIF.PNG対策
131
        $this->setTPinfo($image, $imageSizeInfo['sizeX'], $imageSizeInfo['sizeY']);
132
133
        // 画像で使用する色を透過度を指定して作成
134
        $bgcolor = imagecolorallocatealpha($outImage, @$this->tp["red"], @$this->tp["green"], @$this->tp["blue"], @$this->tp["alpha"]);
135
136
        // 塗り潰す
137
        imagefill($outImage, 0, 0, $bgcolor);
138
        // 透明色を定義
139
        imagecolortransparent($outImage, $bgcolor);
140
        //!透過GIF.PNG対策
141
        // 画像リサイズ
142
        $ret = imagecopyresampled($outImage, $image, 0, 0, 0, 0, $imageSizeInfo['reSizeX'], $imageSizeInfo['reSizeY'], $imageSizeInfo['sizeX'], $imageSizeInfo['sizeY']);
143
144
        if ($ret === false) {
145
            // リサイズ失敗
146
            return false;
147
        }
148
149
        ImageDestroy($image);
150
151
        // 画像保存
152
        $imagepathinfo = $this->getPathInfo($imagePath, $baseSize);
153
        //resizeファイルを格納するディレクトリを作成
154
        if (
155
            !$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...
156
        ) {
157
            return false;
158
        }
159
160
        switch ($imagetype) {
161
            case IMAGETYPE_GIF:
162
                ImageGIF($outImage, $imagepathinfo['resize_filepath']);
163
                break;
164
            case IMAGETYPE_JPEG:
165
                ImageJPEG($outImage, $imagepathinfo['resize_filepath'], 100);
166
                break;
167
            case IMAGETYPE_PNG:
168
                ImagePNG($outImage, $imagepathinfo['resize_filepath']);
169
                break;
170
            default:
171
                return false;
172
        }
173
174
        ImageDestroy($outImage);
175
176
        return true;
177
    }
178
179
    /**
180
     * setTPinfo
181
     *  透過GIFか否か?および透過GIF情報セット
182
     *
183
     *   透過GIFである場合
184
     *   プロパティ$tpに透過GIF情報がセットされる
185
     *
186
     *     $tp["red"]   = 赤コンポーネントの値
187
     *     $tp["green"] = 緑コンポーネントの値
188
     *     $tp["blue"]  = 青コンポーネントの値
189
     *     $tp["alpha"] = 透過度(0から127/0は完全に不透明な状態/127は完全に透明な状態)
190
     *
191
     * @access private
192
     * @param  resource $src 画像リソース
193
     * @param  int   $w  対象画像幅(px)
194
     * @param  int   $h  対象画像高(px)
195
     * @return boolean
196
     */
197
    private function setTPinfo($src, $w, $h) {
198
199
        for ($sx = 0; $sx < $w; $sx++) {
200
            for ($sy = 0; $sy < $h; $sy++) {
201
                $rgb = imagecolorat($src, $sx, $sy);
202
                $idx = imagecolorsforindex($src, $rgb);
203
                if ($idx["alpha"] !== 0) {
204
                    $tp = $idx;
205
                    break;
206
                }
207
            }
208
            if (!isset($tp) || $tp !== null) {
209
                break;
210
            }
211
        }
212
        // 透過GIF
213
        if (isset($tp) && is_array($tp)) {
214
            $this->tp = $tp;
215
            return true;
216
        }
217
        // 透過GIFではない
218
        return false;
219
    }
220
221
    /**
222
     * getPathInfo
223
     * 通常のpathinfoに加えてContentsFile独自のpathも一緒に設定する
224
     * @author hagiwara
225
     * @param string $imagePath
226
     * @param array $resize
227
     */
228
    public function getPathInfo($imagePath, $resize = []) {
229
        $pathinfo = pathinfo($imagePath);
230
        $pathinfo['resize_dir'] = $pathinfo['dirname'] . '/contents_file_resize_' . $pathinfo['filename'];
231
        //一旦ベースのパスを通しておく
232
        $pathinfo['resize_filepath'] = $imagePath;
233
        if (!empty($resize)) {
234
            if (!isset($resize['width'])) {
235
                $resize['width'] = 0;
236
            }
237
            if (!isset($resize['height'])) {
238
                $resize['height'] = 0;
239
            }
240
            $pathinfo['resize_filepath'] = $pathinfo['resize_dir'] . '/' . $resize['width'] . '_' . $resize['height'];
241
        }
242
        return $pathinfo;
243
    }
244
}
245