Completed
Pull Request — master (#263)
by
unknown
04:37
created

Thumbnail::generate()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 7
nop 0
dl 0
loc 28
ccs 0
cts 14
cp 0
crap 42
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/skeleton
5
 */
6
7
/**
8
 * @namespace
9
 */
10
namespace Image;
11
12
/**
13
 * Image resize and crop
14
 *
15
 * @category Application
16
 * @package  Library
17
 *
18
 * @author   Anton Shevchuk
19
 * @created  21.06.13 11:29
20
 */
21
class Thumbnail
22
{
23
    /**
24
     * Full path to file
25
     *
26
     * @var string
27
     */
28
    protected $path;
29
30
    /**
31
     * Filename
32
     *
33
     * @var string
34
     */
35
    protected $file;
36
37
    /**
38
     * Width of thumbnail, zero means leave original size
39
     * @var int
40
     */
41
    protected $width = 0;
42
43
    /**
44
     * Height of thumbnail, zero means leave original size
45
     * @var int
46
     */
47
    protected $height = 0;
48
49
    /**
50
     * Constructor of Image Tool
51
     *
52
     * @access  public
53
     * @param string $file
54
     */
55
    public function __construct($file)
56
    {
57
        $this->path = dirname($file);
58
        $this->file = substr($file, strlen($this->path)+1);
59
    }
60
61
    /**
62
     * Setup width
63
     *
64
     * @param int $width
65
     * @return self
66
     */
67
    public function setWidth($width)
68
    {
69
        $this->width = (int)$width;
70
        return $this;
71
    }
72
73
    /**
74
     * Setup height
75
     *
76
     * @param int $height
77
     * @return self
78
     */
79
    public function setHeight($height)
80
    {
81
        $this->height = (int)$height;
82
        return $this;
83
    }
84
85
    /**
86
     * Generate thumbnail
87
     *
88
     * @throws Exception
89
     * @return string Path to new file
90
     */
91
    public function generate()
92
    {
93
        $dir = $this->path .'/.thumb/'.$this->width.'x'.$this->height;
94
95
        if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
96
            throw new Exception("Thumbnail image can't be save. Parent directory is not writable");
97
        }
98
99
        // Thumbnail already exists
100
        // then remove it and regenerate
101
        if (file_exists($dir.'/'.$this->file)) {
102
            unlink($dir.'/'.$this->file);
103
        }
104
105
        if (class_exists('\\Imagick')) {
106
            $image = new \Imagick($this->path.'/'.$this->file);
107
        } elseif (function_exists('gd_info')) {
108
            $image = new Gd($this->path.'/'.$this->file);
109
        } else {
110
            // return original file
111
            return $this->path .'/'. $this->file;
112
        }
113
114
        $image->cropThumbnailImage($this->width, $this->height);
115
        $image->writeimage($dir.'/'.$this->file);
116
117
        return $dir .'/'. $this->file;
118
    }
119
}
120