-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageResize.php
202 lines (167 loc) · 5.6 KB
/
ImageResize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Copyright (c) 2019 PHP-QuickORM ImageResize
* Author: Rytia Leung
* Email: [email protected]
* Github: github.com/php-quickorm/ImageResize
*/
class ImageResize
{
private $path;
private $filename;
private $extension;
private $width;
private $height;
private $rawResource;
private $newResource;
/**
* ImageResize constructor.
* @param string $path
* @param int $width
* @param int $height
* @param string $sourceFormat
*/
public function __construct($path, $width = 170, $height = 128, $sourceFormat = null)
{
// File and new size
$this->path = $path;
$this->width = $width;
$this->height = $height;
if (is_null($sourceFormat)) {
list($this->filename,$this->extension) = explode(".",$path);
} else {
$this->extension = $sourceFormat;
}
if(!in_array($this->extension,['png', 'jpg', 'gif'])){
trigger_error('no supported format.');
}
$this->generate();
}
/**
* Generate image
*/
private function generate(){
// Get new sizes
list($oldWidth, $oldHeight) = getimagesize($this->path);
$condition = $oldWidth / $oldHeight;
$resizeWidthPercent = $this->width / $oldWidth;
$resizeHeightPercent = $this->height / $oldHeight;
$offsetHeight = 0;
$offsetWidth = 0;
if ($condition < ($this->width / $this->height)){
$offsetHeight = ( $resizeWidthPercent * $oldHeight - $this->height ) * 0.5 / ($resizeWidthPercent);
} else {
$offsetWidth = ( $resizeHeightPercent * $oldWidth - $this->width ) * 0.5 / ($resizeHeightPercent);
}
// Load
$this->newResource = imagecreatetruecolor($this->width, $this->height);
switch ($this->extension){
case 'jpg' : $this->rawResource = imagecreatefromjpeg($this->path); break;
case 'png' : $this->rawResource = imagecreatefrompng($this->path); break;
case 'gif' : $this->rawResource = imagecreatefromgif($this->path); break;
}
// Resize
imagecopyresized($this->newResource, $this->rawResource, 0, 0, $offsetWidth, $offsetHeight, $this->width, $this->height, $oldWidth - 2 * $offsetWidth, $oldHeight - 2 * $offsetHeight);
}
/**
* Add water mark to image
* @param string $path
* @param int $width
* @param int $height
* @param string $positionX
* @param string $positionY
* @param int $margin
*/
public function addImageMark($path, $width = 50, $height = 50, $positionX = "right", $positionY = "bottom", $margin = 10){
$im = (new ImageResize($path, $width, $height))->getImageResource();
if (strtolower($positionX) == "right"){
$dstX = $this->width - $width - $margin;
} else {
$dstX = $margin;
}
if (strtolower($positionY) == "bottom"){
$dstY = $this->height - $height - $margin;
} else {
$dstY = $margin;
}
imagecopy($this->newResource, $im, $dstX, $dstY, 0, 0, $width, $height);
}
/**
* Add text to image
* @param string $text
* @param int $fontSize
* @param string $color
* @param string $positionX
* @param string $positionY
* @param int $margin
*/
public function addTextMark($text, $fontSize = 5, $color = "#0000ff", $positionX = "right", $positionY = "bottom", $margin = 10){
$textWidth = imagefontwidth($fontSize) * strlen($text);
$textHeight = imagefontheight($fontSize);
if (strtolower($positionX) == "right"){
$dstX = $this->width - $textWidth - $margin;
} else {
$dstX = $margin;
}
if (strtolower($positionY) == "bottom"){
$dstY = $this->height - $textHeight - $margin;
} else {
$dstY = $margin;
}
$textColor = imagecolorallocate($this->newResource, hexdec($color[1].$color[2]), hexdec($color[3].$color[4]), hexdec($color[5].$color[6]));
imagestring ( $this->newResource , $fontSize, $dstX , $dstY , $text , $textColor );
}
/**
* Get PHP GD resource
* @return resource
*/
public function getImageResource()
{
return $this->newResource;
}
/**
* Save the image as file
* @param string $path
* @param null $format
*/
public function save($path, $format = null){
if (is_null($format)) {
list($filename,$extension) = explode(".",$path);
} else {
$extension = $format;
}
if(!in_array($extension,['png', 'jpg', 'gif'])){
trigger_error('no supported format.');
}
switch ($extension){
case 'jpg' :
imagejpeg($this->newResource, $path);
break;
case 'png' :
imagepng($this->newResource, $path);
break;
case 'gif' :
imagegif($this->newResource, $path);
break;
}
}
/**
* Send HTTP Response
*/
public function render(){
switch ($this->extension){
case 'jpg' :
header('Content-Type: image/jpeg');
imagejpeg($this->newResource);
break;
case 'png' :
header('Content-Type: image/png');
imagepng($this->newResource);
break;
case 'gif' :
header('Content-Type: image/gif');
imagegif($this->newResource);
break;
}
}
}