Repository navigation
Expand file tree
/
Copy pathclass.WMThumbnail.inc.php
More file actions
305 lines (284 loc) · 9.02 KB
/
Copy pathclass.WMThumbnail.inc.php
File metadata and controls
305 lines (284 loc) · 9.02 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
//+----------------------------------------------------------------------+
//| WAMP (XP-SP2/2.2/5.2/5.1.0) |
//+----------------------------------------------------------------------+
//| Copyright(c) 2001-2008 Michael Wimmer |
//+----------------------------------------------------------------------+
//| Licence: GNU General Public License v3 |
//+----------------------------------------------------------------------+
//| Authors: Michael Wimmer <flaimo@gmail.com> |
//+----------------------------------------------------------------------+
//
// $Id$
/**
* include base class
*/
namespace SlightPHP;
require_once 'class.Thumbnail.inc.php';
/**
* @package Thumbnail
*/
/**
* Creates a thumbnail from a source image, put a watermark/logo on it and caches it for a given time
*
* Tested with Apache 2.2 and PHP 5.2.
* Last change: 2008-04-06
*
* @access public
* @author Michael Wimmer <flaimo@gmail.com>
* @copyright Michael Wimmer
* @link http://code.google.com/p/flaimo-php
* @package Thumbnail
* @version 2.000
*/
class WMThumbnail extends Thumbnail {
/*-------------------*/
/* V A R I A B L E S */
/*-------------------*/
/**
* path/filename of logo / watermark
*
* @var string
*/
protected $wm_image_path;
/**
* @var resource
*/
protected $wm_image;
/**
* @var int
*/
protected $wm_image_height;
/**
* @var int
*/
protected $wm_image_width;
/**
* image format of logo
*
* @var int
*/
protected $wm_image_type;
/**
* holds all logos/watermarks
*
* @var array
*/
protected $logos = array();
/*-----------------------*/
/* C O N S T R U C T O R */
/*-----------------------*/
/**
* Constructor
*
* @param string $file path/filename of picture
* @param int $seconds amount of seconds thumbs should be cached. 0 = no cache
* @return void
* @uses CachedThumbnail::CachedThumbnail()
* @uses $wm_image_path
*/
function __construct($file = '', $seconds = 0) {
parent::__construct($file, $seconds);
} // end constructor
/**
* reads metadata of the logo image
*
* @param string $path path/filename of picture
* @return void
* @uses $wm_image_width
* @uses $wm_image_height
* @uses $wm_image_type
* @uses $formats
*/
protected function readWMImageData($path = '') {
if (strlen(trim($path)) > 0) {
list($this->wm_image_width, $this->wm_image_height, $this->wm_image_type, $attr) = getimagesize($path);
unset($attr);
if (!in_array($this->wm_image_type, $this->formats)) {
die("Can't create thumbnail from '" . $this->wm_image_type .
"' source: " . $this->wm_image_path);
} // end if
} // end if
} // end function
/**
* reads the logo pic into a variable
*
* @param string $path path/filename of picture
* @return void
* @uses $wm_image
* @uses readWMImageData()
* @uses $wm_image_type
* @uses $wm_image_path
*/
protected function readWMImage($path = '') {
if (strlen(trim($path)) > 0 && !isset($this->wm_image)) {
$this->readWMImageData($path);
switch ($this->wm_image_type) {
case 1:
$this->wm_image = imagecreatefromgif($path);
break;
case 2:
$this->wm_image = imagecreatefromjpeg($path);
break;
case 3:
$this->wm_image = imagecreatefrompng($path);
break;
case 15:
$this->wm_image = imagecreatefromwbmp($path);
break;
case 999:
default:
$this->wm_image = imagecreatefromstring($path);
break;
} // end switch
} // end if
} // end function
/**
* sets the position of the logo /watermark
*
* @param string $logo path/filename of the logo
* @param int $position 1 = left-top, 2 = right-top, 3 = right-bottom, 4 = left-bottom, 5 = center
* @param int $margin margin to the border of the thumbnail
* @return void
* @uses $position
*/
public function addLogo($logo = '', $position = 3, $margin = 1) {
if (file_exists($logo) && ($position > 0 && $position < 6)) {
$this->logos[] = array('path' => trim($logo), 'pos' => $position, 'margin' => $margin);
} // end if
} // end function
/**
* creates the thumbnail and saves it to a variable
*
* @return void
* @uses Thumbnail::createThumbnail()
* @uses readWMImage()
* @uses $thumbnail
* @uses $thumbnail_width
* @uses $thumbnail_height
* @uses $wm_image_width
* @uses $wm_image_height
* @uses $position
* @uses $wm_image
* @uses $logos
*/
protected function createThumbnail() {
parent::createThumbnail();
imagealphablending($this->thumbnail, true);
foreach ($this->logos as $logo) {
if (strlen(trim($logo['path'])) > 0) {
$this->readWMImage($logo['path']);
$start_pos_x = $this->thumbnail_width - $logo['margin'] - $this->wm_image_width;
$start_pos_y = $this->thumbnail_height - $logo['margin'] - $this->wm_image_height;
switch ($logo['pos']) {
case 1: // left-top
imagecopy($this->thumbnail, $this->wm_image,
$logo['margin'], $logo['margin'], 0, 0,
$this->wm_image_width,
$this->wm_image_height);
break;
case 2: // right-top
imagecopy($this->thumbnail, $this->wm_image, $start_pos_x,
$logo['margin'], 0, 0, $this->wm_image_width,
$this->wm_image_height);
break;
case 3: // right-bottom
imagecopy($this->thumbnail, $this->wm_image, $start_pos_x,
$start_pos_y, 0, 0, $this->wm_image_width,
$this->wm_image_height);
break;
case 4: // left-bottom
imagecopy($this->thumbnail, $this->wm_image,
$logo['margin'], $start_pos_y, 0, 0,
$this->wm_image_width, $this->wm_image_height);
break;
case 5: // center
default:
$middle_x = ($this->thumbnail_width >> 1) - ($this->wm_image_width >> 1);
$middle_y = ($this->thumbnail_height >> 1) - ($this->wm_image_height >> 1);
imagecopy($this->thumbnail, $this->wm_image, $middle_x,
$middle_y, 0, 0, $this->wm_image_width,
$this->wm_image_height);
break;
} // end switch
unset($this->wm_image);
} // end if
} // end foreach
} // end function
/**
* outputs the thumbnail to the browser
*
* overrides method of base class
*
* @param string $format gif, jpg, png, wbmp
* @param int $quality jpg-quality: 0-100
* @return mixed
* @uses createThumbnail()
* @uses CachedThumbnail::outputThumbnail()
*/
public function outputThumbnail($format = 'png', $quality = 75) {
parent::setOutputFormat($format);
//parent::setCache();
/*
if ($this->cache_time === 0 || $this->cache->isPictureCached() === FALSE) {
$this->createThumbnail();
if ($this->cache_time > 0) {
$this->cache->writePictureCache($this->thumbnail, 100);
} // end if
} // end if
*/
parent::outputThumbnail($format, $quality);
} // end function
/**
* returns the variable with the thumbnail image
*
* @param string $format gif, jpg, png, wbmp
* @return mixed
* @uses createThumbnail()
* @uses CachedThumbnail::returnThumbnail()
*/
public function returnThumbnail($format = 'png') {
parent::setOutputFormat($format);
/*parent::setCache();
if ($this->cache_time === 0 || $this->cache->isPictureCached() === FALSE) {
$this->createThumbnail();
if ($this->cache_time > 0) {
$this->cache->writePictureCache($this->thumbnail, 100);
} // end if
} // end if
*/
return parent::returnThumbnail($format);
} // end function
/**
* returns the path/filename of the cached thumbnail
*
* if cached pic is not available, tries to create it with the given parameters
*
* @param string $format gif, jpg, png, wbmp
* @param int $quality jpg-quality: 0-100
* @return mixed string or FALSE if no cached pic is available
* @uses $cache_time
* @uses PictureCache::isPictureCached()
* @uses setOutputFormat()
* @uses PictureCache::writePictureCache()
* @uses Thumbnail::createThumbnail()
*/
/*
public function getCacheFilepath($format = 'png', $quality = 75) {
if ($this->cache_time === 0) {
return (boolean) FALSE; // no cached thumb available
} // end if
parent::setOutputFormat($format);
parent::setCache();
$path = $this->cache->getCacheFilepath($format, $quality);
if ($path != FALSE) {
return (string) $path;
} else { // trys to create cache and return filename
$this->createThumbnail();
$this->cache->writePictureCache($this->thumbnail, $quality);
return $this->cache->getCacheFilepath($format, $quality);
} // end if
} // end function
*/
} // end class CachedThumbnail