1
|
|
|
<?php |
2
|
|
|
namespace ntentan\honam\engines\php\helpers; |
3
|
|
|
|
4
|
|
|
use ntentan\honam\engines\php\Helper; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
* |
10
|
|
|
* @todo Completely rewrite this helper to make it more efficient. Expose the |
11
|
|
|
* interfaces as they are. Consider using Image Magick. |
12
|
|
|
*/ |
13
|
|
|
class ImagesHelper extends Helper |
14
|
|
|
{ |
15
|
|
|
private $quality = 90; |
16
|
|
|
|
17
|
|
View Code Duplication |
private function loadImage($path) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$array = explode(".", $path); |
20
|
|
|
switch(strtolower(end($array))) |
21
|
|
|
{ |
22
|
|
|
case 'png': |
23
|
|
|
$image = imagecreatefrompng($path); |
24
|
|
|
break; |
25
|
|
|
|
26
|
|
|
case 'jpeg': |
27
|
|
|
case 'jpg': |
28
|
|
|
$image = imagecreatefromjpeg($path); |
29
|
|
|
break; |
30
|
|
|
} |
31
|
|
|
return $image; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
private function writeImage($image, $path) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$array = explode(".", $path); |
37
|
|
|
switch(strtolower(end($array))) |
38
|
|
|
{ |
39
|
|
|
case 'png': |
40
|
|
|
$image = imagepng($image, $path); |
41
|
|
|
break; |
42
|
|
|
|
43
|
|
|
case 'jpeg': |
44
|
|
|
case 'jpg': |
45
|
|
|
$image = imagejpeg($image, $path, $this->quality); |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
return $image; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Resizes an image. |
53
|
|
|
* |
54
|
|
|
* @param string $src Path to the image to be resized |
55
|
|
|
* @param string $dest Path to sore the resized image |
56
|
|
|
* @param integer $width New width of the image |
57
|
|
|
* @param integer $height New Height of the image |
58
|
|
|
*/ |
59
|
|
|
public function resize($src, $dest, $width = 0, $height = 0) |
60
|
|
|
{ |
61
|
|
|
$im = $this->loadImage($src); |
62
|
|
|
$outputWidth = imagesx($im); |
63
|
|
|
$outputHeight = imagesy($im); |
64
|
|
|
|
65
|
|
|
$aspect = $outputWidth / $outputHeight; |
66
|
|
|
|
67
|
|
|
if($width<=0) |
68
|
|
|
{ |
69
|
|
|
$width = $aspect * $height; |
70
|
|
|
} |
71
|
|
|
else if($height<=0) |
72
|
|
|
{ |
73
|
|
|
$height = $width / $aspect; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$destinationImage = imagecreatetruecolor($width, $height); |
77
|
|
|
imagealphablending($destinationImage, false ); |
78
|
|
|
imagesavealpha($destinationImage, true ); |
79
|
|
|
imagecopyresampled($destinationImage, $im, 0, 0, 0, 0, $width, $height, $outputWidth, $outputHeight); |
80
|
|
|
|
81
|
|
|
$this->writeImage($destinationImage, $dest); |
82
|
|
|
|
83
|
|
|
imagedestroy($im); |
84
|
|
|
imagedestroy($destinationImage); |
85
|
|
|
|
86
|
|
|
return $dest; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Crops an image. This function crops by fitting the image into the center |
91
|
|
|
* of a new cropping area. If the cropping area is smaller than the image |
92
|
|
|
* the image is scaled to fit. |
93
|
|
|
* |
94
|
|
|
* @param string $src The path to the source image |
95
|
|
|
* @param string $dest The path to the destination image |
96
|
|
|
* @param int $width The cropping width |
97
|
|
|
* @param int $height The cropping height |
98
|
|
|
* @param boolean $head Place the cropping area on top of the image |
99
|
|
|
*/ |
100
|
|
|
public function crop($src, $dest, $width, $height,$head=false) |
101
|
|
|
{ |
102
|
|
|
$im = $this->loadImage($src); |
103
|
|
|
$o_width = imagesx($im); |
104
|
|
|
$o_height = imagesy($im); |
105
|
|
|
if($head==false) $top = ($o_height/2)-($height/2); else $top=0; |
|
|
|
|
106
|
|
|
$left = ($o_width/2)-($width/2); |
107
|
|
|
$im2 = imagecreatetruecolor ($width, $height); |
108
|
|
|
imagealphablending($im2, false ); |
109
|
|
|
imagesavealpha($im2, true ); |
110
|
|
|
imagecopyresampled($im2,$im,0,0,$left,$top,$width,$height,$width,$height); |
111
|
|
|
$this->writeImage($im2, $dest); |
112
|
|
|
imagedestroy($im); |
113
|
|
|
imagedestroy($im2); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* A smart thumbnailing function. Generates thumbnail images without |
118
|
|
|
* distorting the output of the final image. |
119
|
|
|
* @param string $file |
|
|
|
|
120
|
|
|
* @param string $width |
121
|
|
|
* @param string $height |
122
|
|
|
* @param string $head |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function thumbnail($source, $destination, $width, $height, $head=false, $overwrite=false) |
126
|
|
|
{ |
127
|
|
|
if(!is_file($source)) return; |
128
|
|
|
if(!is_file($destination) || (filemtime($source)>filemtime($destination)) || $overwrite === true) |
129
|
|
|
{ |
130
|
|
|
$image = $this->loadImage($source); |
131
|
|
|
$imageWidth = imagesx($image); |
132
|
|
|
$imageHeight = imagesy($image); |
133
|
|
|
imagedestroy($image); |
134
|
|
|
$tempImage = 'cache/' . uniqid() . ".png"; |
135
|
|
|
|
136
|
|
|
$aspect = $imageWidth / $imageHeight; |
137
|
|
|
|
138
|
|
|
if($aspect * $height >= $width) |
139
|
|
|
{ |
140
|
|
|
$this->resize($source, $tempImage, 0, $height); |
141
|
|
|
} |
142
|
|
|
else |
143
|
|
|
{ |
144
|
|
|
$this->resize($source, $tempImage, $width, 0); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->crop( $tempImage, $destination, $width, $height, $head); |
|
|
|
|
148
|
|
|
unlink($tempImage); |
149
|
|
|
} |
150
|
|
|
return $destination; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function quality($quality) |
154
|
|
|
{ |
155
|
|
|
$this->quality = $quality; |
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
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.