SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | /** | |
3 | * This classes allows you to create thumbnails. Input images will be resized, | |
4 | * while the ratio keeps intact, and will cropped to the size given. | |
5 | * @author CornΓ© Dorrestijn | |
6 | * @license http://www.gnu.org/copyleft/lesser.html GNU Library General Public License | |
7 | * @package Default | |
8 | */ | |
9 | class Thumbnail | |
10 | { | |
11 | private $imageData, $newImage, $sourceWidth, $sourceHeight; | |
12 | public $width, $height; | |
13 | public $quality = 1; | |
14 | /** | |
15 | * Constructor | |
16 | * @param string $url The url to the local file. | |
17 | * @param integer $width The width of the thumbnail, default 100px. | |
18 | * @param integer $height The height of the thumbnail, default 100px. | |
19 | * @return void|String returns a string if there is a error. | |
20 | */ | |
21 | public function __construct($url, $width = 100, $height = 100) | |
22 | { | |
23 | - | if(!isset($url) && $url === undefined && $url === '') die("No image url given."); |
23 | + | if(!isset($url) && $url === undefined && $url === '') return "No image url given."; |
24 | - | if(!is_readable($url)) die('Image "' . $url . '" file not readable.'); |
24 | + | if(!is_readable($url)) return 'Image "' . $url . '" file not readable.'; |
25 | $this->width = $width; | |
26 | $this->height = $height; | |
27 | $this->imageData = imagecreatefromstring(file_get_contents($url)); | |
28 | list($this->sourceWidth, $this->sourceHeight) = getimagesize($url); | |
29 | $this->redraw(); | |
30 | return; | |
31 | } | |
32 | ||
33 | /** | |
34 | * Generates a jpeg image. | |
35 | * @param boolean $redraw If set to true it will redraw the image. | |
36 | * @param string $path If set it will save the image to the given path. | |
37 | * @return void|String returns a string if there is a error. | |
38 | */ | |
39 | public function getJpeg($redraw = true, $path = '') | |
40 | { | |
41 | if($redraw) $this->redraw(); | |
42 | $q = round($this->quality * 99); | |
43 | if($path !== '') | |
44 | { | |
45 | if(imagejpeg($this->newImage, $path, $q)) | |
46 | { | |
47 | ||
48 | } | |
49 | else | |
50 | { | |
51 | - | echo "Image NOT saved"; |
51 | + | return "Image NOT saved"; |
52 | } | |
53 | } | |
54 | else | |
55 | { | |
56 | header ('Content-Type: image/jpeg'); | |
57 | imagejpeg($this->newImage, NULL, $q); | |
58 | } | |
59 | ||
60 | imagedestroy($this->newImage); | |
61 | return; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Generates a png image. | |
66 | * @param boolean $redraw If set to true it will redraw the image. | |
67 | * @param string $path If set it will save the image to the given path. | |
68 | * @return void|String returns a string if there is a error. | |
69 | */ | |
70 | public function getPng($redraw = true, $path = '') | |
71 | { | |
72 | if($redraw) $this->redraw(); | |
73 | $q = 9-round($this->quality * 9); | |
74 | if($path !== '') | |
75 | { | |
76 | if(imagepng($this->newImage, $path, $q)) | |
77 | { | |
78 | ||
79 | } | |
80 | else | |
81 | { | |
82 | - | echo "Image NOT saved"; |
82 | + | return "Image NOT saved"; |
83 | } | |
84 | } | |
85 | else | |
86 | { | |
87 | header ('Content-Type: image/png'); | |
88 | imagepng($this->newImage, NULL, $q); | |
89 | } | |
90 | ||
91 | imagedestroy($this->newImage); | |
92 | return; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Generates a gif image. | |
97 | * @param boolean $redraw If set to true it will redraw the image. | |
98 | * @param string $path If set it will save the image to the given path. | |
99 | * @return void|String returns a string if there is a error. | |
100 | */ | |
101 | public function getGif($redraw = true, $path = '') | |
102 | { | |
103 | if($redraw) $this->redraw(); | |
104 | if($path !== '') | |
105 | { | |
106 | if(imagegif($this->newImage, $path)) | |
107 | { | |
108 | ||
109 | } | |
110 | else | |
111 | { | |
112 | - | echo "Image NOT saved"; |
112 | + | return "Image NOT saved"; |
113 | } | |
114 | } | |
115 | else | |
116 | { | |
117 | header ('Content-Type: image/gif'); | |
118 | imagegif($this->newImage); | |
119 | } | |
120 | imagedestroy($this->newImage); | |
121 | return; | |
122 | } | |
123 | ||
124 | /** | |
125 | * Draws the scaled and cropped image in $this->newImage. | |
126 | * @return void. | |
127 | */ | |
128 | private function redraw() | |
129 | { | |
130 | $newWidth = $this->width; | |
131 | $newHeight = $this->height; | |
132 | $offsetX = 0; | |
133 | $offsetY = 0; | |
134 | $pr = $this->sourceWidth/$this->sourceHeight; | |
135 | $tr = $this->width/$this->height; | |
136 | if($pr > $tr) | |
137 | { | |
138 | $newWidth = round($this->height*$pr); | |
139 | $offsetX = -round(($newWidth - $this->width) / 2); | |
140 | } | |
141 | else | |
142 | { | |
143 | $newHeight = round($this->width/$pr); | |
144 | $offsetY = -round(($newHeight - $this->height) / 2); | |
145 | } | |
146 | $this->newImage = imagecreatetruecolor($this->width, $this->height); | |
147 | imagecopyresampled($this->newImage, $this->imageData, $offsetX, $offsetY, 0, 0, $newWidth, $newHeight, $this->sourceWidth, $this->sourceHeight); | |
148 | return; | |
149 | } | |
150 | } | |
151 | ?> |