-
Notifications
You must be signed in to change notification settings - Fork 12
/
Thumbnail.php
executable file
·54 lines (44 loc) · 1.19 KB
/
Thumbnail.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
<?php
namespace Avatar;
class Thumbnail {
private $image;
public $width;
public $height;
public $type;
private function __construct($url) {
$imageInfo = getimagesize($url);
list($this->width, $this->height, $this->type) = $imageInfo;
switch ($this->type) {
case IMAGETYPE_GIF:
$this->image = imagecreatefromgif($url);
break;
case IMAGETYPE_PNG:
$this->image = imagecreatefrompng($url);
break;
case IMAGETYPE_JPEG:
$this->image = imagecreatefromjpeg($url);
break;
}
}
public static function open($url) {
return new self($url);
}
public function cleanup() {
imagedestroy($this->image);
$this->image = null;
}
public function createThumbnail($dimension, $file) {
if ($dimension > $this->width) {
$dimension = $this->width;
}
$thumb = imagecreatetruecolor($dimension, $dimension);
imagesavealpha($thumb, true);
$transparent = imagecolorallocatealpha($thumb, 0, 0, 0, 127);
imagefill($thumb, 0, 0, $transparent);
imagecopyresampled($thumb, $this->image, 0, 0, 0, 0, $dimension, $dimension, $this->width, $this->height);
if (!imagepng($thumb, $file)) {
throw new \Exception('Failed to save image ' . $file);
}
imagedestroy($thumb);
}
}