Thumbnail::createFromFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation;
13
14
/**
15
 * Thumbnail Generator.
16
 */
17
class Thumbnail
18
{
19
    protected $metadata = [];
20
    protected $srcImage;
21
    protected $dstImage;
22
23
    /**
24
     * Create a thumbnail from a local file.
25
     *
26
     * @static
27
     *
28
     * @param string $filename
29
     *
30
     * @return Thumbnail
31
     */
32
    public static function createFromFile($filename)
33
    {
34
        $self = new static();
35
        $self->fromFile($filename);
36
37
        return $self;
38
    }
39
40
    /**
41
     * Create a thumbnail from a string.
42
     *
43
     * @static
44
     *
45
     * @param string $blob
46
     *
47
     * @return Thumbnail
48
     */
49
    public static function createFromString($blob)
50
    {
51
        $self = new static();
52
        $self->fromString($blob);
53
54
        return $self;
55
    }
56
57
    /**
58
     * Load the local image file in memory with GD.
59
     *
60
     * @param string $filename
61
     *
62
     * @return Thumbnail
63
     */
64
    public function fromFile($filename)
65
    {
66
        $this->metadata = getimagesize($filename);
67
        $this->srcImage = imagecreatefromstring(file_get_contents($filename));
68
69
        return $this;
70
    }
71
72
    /**
73
     * Load the image blob in memory with GD.
74
     *
75
     * @param string $blob
76
     *
77
     * @return Thumbnail
78
     */
79
    public function fromString($blob)
80
    {
81
        if (!function_exists('getimagesizefromstring')) {
82
            $uri = 'data://application/octet-stream;base64,'.base64_encode($blob);
83
            $this->metadata = getimagesize($uri);
84
        } else {
85
            $this->metadata = getimagesizefromstring($blob);
86
        }
87
88
        $this->srcImage = imagecreatefromstring($blob);
89
90
        return $this;
91
    }
92
93
    /**
94
     * Resize the image.
95
     *
96
     * @param int $width
97
     * @param int $height
98
     *
99
     * @return Thumbnail
100
     */
101
    public function resize($width = 250, $height = 100)
102
    {
103
        $srcWidth = $this->metadata[0];
104
        $srcHeight = $this->metadata[1];
105
        $dstX = 0;
106
        $dstY = 0;
107
108
        if ($width == 0 && $height == 0) {
109
            $width = 100;
110
            $height = 100;
111
        }
112
113
        if ($width > 0 && $height == 0) {
114
            $dstWidth = $width;
115
            $dstHeight = floor($srcHeight * ($width / $srcWidth));
116
            $this->dstImage = imagecreatetruecolor($dstWidth, $dstHeight);
117
        } elseif ($width == 0 && $height > 0) {
118
            $dstWidth = floor($srcWidth * ($height / $srcHeight));
119
            $dstHeight = $height;
120
            $this->dstImage = imagecreatetruecolor($dstWidth, $dstHeight);
121
        } else {
122
            $srcRatio = $srcWidth / $srcHeight;
123
            $resizeRatio = $width / $height;
124
125
            if ($srcRatio <= $resizeRatio) {
126
                $dstWidth = $width;
127
                $dstHeight = floor($srcHeight * ($width / $srcWidth));
128
                $dstY = ($dstHeight - $height) / 2 * (-1);
129
            } else {
130
                $dstWidth = floor($srcWidth * ($height / $srcHeight));
131
                $dstHeight = $height;
132
                $dstX = ($dstWidth - $width) / 2 * (-1);
133
            }
134
135
            $this->dstImage = imagecreatetruecolor($width, $height);
136
        }
137
138
        imagecopyresampled($this->dstImage, $this->srcImage, $dstX, $dstY, 0, 0, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
139
140
        return $this;
141
    }
142
143
    /**
144
     * Save the thumbnail to a local file.
145
     *
146
     * @param string $filename
147
     *
148
     * @return Thumbnail
149
     */
150
    public function toFile($filename)
151
    {
152
        imagejpeg($this->dstImage, $filename);
153
        imagedestroy($this->dstImage);
154
        imagedestroy($this->srcImage);
155
156
        return $this;
157
    }
158
159
    /**
160
     * Return the thumbnail as a string.
161
     *
162
     * @return string
163
     */
164
    public function toString()
165
    {
166
        ob_start();
167
        imagejpeg($this->dstImage, null);
168
        imagedestroy($this->dstImage);
169
        imagedestroy($this->srcImage);
170
171
        return ob_get_clean();
172
    }
173
174
    /**
175
     * Output the thumbnail directly to the browser or stdout.
176
     */
177
    public function toOutput()
178
    {
179
        imagejpeg($this->dstImage, null);
180
        imagedestroy($this->dstImage);
181
        imagedestroy($this->srcImage);
182
    }
183
}
184