Completed
Branch s3_upload (4473df)
by satoru
02:12
created

ImageContentsFileBehaviorTrait::imageResize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php
2
3
namespace ContentsFile\Model\Behavior\Traits;
4
5
use ContentsFile\Aws\S3;
6
use Cake\Core\Configure;
7
use Cake\Filesystem\Folder;
8
use Cake\I18n\Time;
9
use Cake\Network\Exception\InternalErrorException;
10
use Cake\Utility\Security;
11
12
/**
13
 * ImageContentsFileBehaviorTrait
14
 * 画像関係の処理
15
 */
16
trait ImageContentsFileBehaviorTrait
17
{
18
    /**
19
     * imageResize
20
     * 画像のリサイズ処理(外からでもたたけるようにpublicにする
21
     * @author hagiwara
22
     * @param string $imagePath
23
     * @param array $baseSize
24
     */
25
    public function imageResize($imagePath, $baseSize) {
26
27
        $imageInfo = $this->getImageInfo($imagePath);
28
        $image = $imageInfo['image'];
29
        $imagetype = $imageInfo['imagetype'];
30
        if (!$image) {
31
            // 画像の読み込み失敗
32
            return false;
33
        }
34
        // // 画像の縦横サイズを取得
35
        $imageSizeInfo = $this->imageSizeInfo($image, $baseSize);
36
37
        return $this->imageResizeMake($image, $imagetype, $imagePath, $baseSize, $imageSizeInfo);
38
    }
39
40
    /**
41
     * getImageInfo
42
     * 画像情報の取得
43
     * @author hagiwara
44
     * @param string $imagePath
45
     */
46
    private function getImageInfo($imagePath)
47
    {
48
        if (file_exists($imagePath) === false) {
49
            return false;
50
        }
51
52
        $imagetype = exif_imagetype($imagePath);
53
        if ($imagetype === false) {
54
            return false;
55
        }
56
57
        // 画像読み込み
58
        switch ($imagetype) {
59
            case IMAGETYPE_GIF:
60
                $image = ImageCreateFromGIF($imagePath);
61
                break;
62
            case IMAGETYPE_JPEG:
63
                $image = ImageCreateFromJPEG($imagePath);
64
                break;
65
            case IMAGETYPE_PNG:
66
                $image = ImageCreateFromPNG($imagePath);
67
                break;
68
            default:
69
                $image = false;
70
        }
71
        return [
72
            'image' => $image,
73
            'imagetype' => $imagetype,
74
        ];
75
    }
76
77
    /**
78
     * imageSizeInfo
79
     * 画像リサイズ情報の取得
80
     * @author hagiwara
81
     * @param string $imagePath
0 ignored issues
show
Bug introduced by
There is no parameter named $imagePath. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
     * @param array $baseSize
83
     */
84
    private function imageSizeInfo($image, $baseSize)
85
    {
86
        // 画像の縦横サイズを取得
87
        $sizeX = ImageSX($image);
88
        $sizeY = ImageSY($image);
89
        // リサイズ後のサイズ
90
        if (!array_key_exists('height', $baseSize)) {
91
            $baseSize['height'] = 0;
92
        }
93
        if (!array_key_exists('width', $baseSize)) {
94
            $baseSize['width'] = 0;
95
        }
96
97
        if (empty($baseSize['width']) || !empty($baseSize['height']) && $sizeX * $baseSize['height'] < $sizeY * $baseSize['width']) {
98
            // 縦基準
99
            $mag = $baseSize['height'] / $sizeY;
100
            $reSizeY = $baseSize['height'];
101
            $reSizeX = $sizeX * $mag;
102
        } else {
103
            // 横基準
104
            $mag = $baseSize['width'] / $sizeX;
105
            $reSizeX = $baseSize['width'];
106
            $reSizeY = $sizeY * $mag;
107
        }
108
        return [
109
            'sizeX' => $sizeX,
110
            'sizeY' => $sizeY,
111
            'reSizeX' => $reSizeX,
112
            'reSizeY' => $reSizeY,
113
        ];
114
    }
115
116
    /**
117
     * imageResizeMake
118
     * 画像リサイズ情報の取得
119
     * @author hagiwara
120
     * @param string $image
121
     * @param integer $imagetype
122
     * @param string $imagePath
123
     * @param array $baseSize
124
     * @param array $imageSizeInfo
125
     */
126
    private function imageResizeMake($image, $imagetype, $imagePath, $baseSize, $imageSizeInfo)
127
    {
128
        // サイズ変更後の画像データを生成
129
        $outImage = ImageCreateTrueColor($imageSizeInfo['reSizeX'], $imageSizeInfo['reSizeY']);
130
        if (!$outImage) {
131
            // リサイズ後の画像作成失敗
132
            return false;
133
        }
134
135
        //透過GIF.PNG対策
136
        $this->setTPinfo($image, $imageSizeInfo['sizeX'], $imageSizeInfo['sizeY']);
0 ignored issues
show
Documentation introduced by
$image is of type string, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
138
        // 画像で使用する色を透過度を指定して作成
139
        $bgcolor = imagecolorallocatealpha($outImage, @$this->tp["red"], @$this->tp["green"], @$this->tp["blue"], @$this->tp["alpha"]);
0 ignored issues
show
Bug introduced by
The property tp does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
140
141
        // 塗り潰す
142
        imagefill($outImage, 0, 0, $bgcolor);
143
        // 透明色を定義
144
        imagecolortransparent($outImage, $bgcolor);
145
        //!透過GIF.PNG対策
146
        // 画像リサイズ
147
        $ret = imagecopyresampled($outImage, $image, 0, 0, 0, 0, $imageSizeInfo['reSizeX'], $imageSizeInfo['reSizeY'], $imageSizeInfo['sizeX'], $imageSizeInfo['sizeY']);
148
149
        if ($ret === false) {
150
            // リサイズ失敗
151
            return false;
152
        }
153
154
        ImageDestroy($image);
155
156
        // 画像保存
157
        $imagepathinfo = $this->getPathInfo($imagePath, $baseSize);
0 ignored issues
show
Bug introduced by
It seems like getPathInfo() 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...
158
        //resizeファイルを格納するディレクトリを作成
159
        if (
160
            !$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...
161
        ) {
162
            return false;
163
        }
164
165
        switch ($imagetype) {
166
            case IMAGETYPE_GIF:
167
                ImageGIF($outImage, $imagepathinfo['resize_filepath']);
168
                break;
169
            case IMAGETYPE_JPEG:
170
                ImageJPEG($outImage, $imagepathinfo['resize_filepath'], 100);
171
                break;
172
            case IMAGETYPE_PNG:
173
                ImagePNG($outImage, $imagepathinfo['resize_filepath']);
174
                break;
175
            default:
176
                return false;
177
        }
178
179
        ImageDestroy($outImage);
180
181
        return true;
182
    }
183
184
    /**
185
     * setTPinfo
186
     *  透過GIFか否か?および透過GIF情報セット
187
     *
188
     *   透過GIFである場合
189
     *   プロパティ$tpに透過GIF情報がセットされる
190
     *
191
     *     $tp["red"]   = 赤コンポーネントの値
192
     *     $tp["green"] = 緑コンポーネントの値
193
     *     $tp["blue"]  = 青コンポーネントの値
194
     *     $tp["alpha"] = 透過度(0から127/0は完全に不透明な状態/127は完全に透明な状態)
195
     *
196
     * @access private
197
     * @param  resource $src 画像リソース
198
     * @param  int   $w  対象画像幅(px)
199
     * @param  int   $h  対象画像高(px)
200
     * @return boolean
201
     */
202
    private function setTPinfo($src, $w, $h) {
203
204
        for ($sx = 0; $sx < $w; $sx++) {
205
            for ($sy = 0; $sy < $h; $sy++) {
206
                $rgb = imagecolorat($src, $sx, $sy);
207
                $idx = imagecolorsforindex($src, $rgb);
208
                if ($idx["alpha"] !== 0) {
209
                    $tp = $idx;
210
                    break;
211
                }
212
            }
213
            if (!isset($tp) || $tp !== null) {
214
                break;
215
            }
216
        }
217
        // 透過GIF
218
        if (isset($tp) && is_array($tp)) {
219
            $this->tp = $tp;
220
            return true;
221
        }
222
        // 透過GIFではない
223
        return false;
224
    }
225
}
226