3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or (at
9
* your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22
* An implementation of the GdFunctionality that offers pseudo-gd-
23
* functionality. It allows to set the PHP environment and will then
24
* simulate its inner workings.
28
* @author Ernesto Baschny <ernst@baschny.de>
29
* @version $Revision: 15513 $
31
class TestGdFunctionality {
34
* Our current environment
39
* Keep track of our resources
41
var $_resourceCounter = 0;
42
var $_undestroyedResourceCounter = 0;
46
function TestGdFunctionality() {
50
* Set the environment we want to simulate
51
* -> 1. PHP 4.1.0, GD 1.8.2 not bundled (4.1.0|1.6.2|0)
52
* 2. PHP 4.2.0, GD 1.8.2 not bundled
53
* -> 3. PHP 4.3.1, GD 2.0 bundled (4.3.1|2.0|1)
54
* 4. PHP 4.3.9, GD 1.6.2 not bundled
55
* -> 5. PHP 4.3.9RC1, GD 2.0.28 bundled (4.3.9RC1|2.0.28|1)
56
* 6. PHP 5.0.1, GD 2.0.28 bundled
58
function setEnvironment($environment) {
59
$this->_gdEnvironment = $environment;
62
function getEnvironmentName() {
63
return $this->_gdEnvironment['name'];
67
* Return the name of a new resource
69
* From a filename like '200x300.gif' set the sizes x=200 y=300
72
function getNewResource($filename=null) {
73
$this->_resourceCounter++;
74
$this->_undestroyedResourceCounter++;
75
$name = 'res #' . $this->_resourceCounter;
76
$this->_resources[$name] = array(
81
if (isset($filename)) {
82
$this->_resources[$name]['filename'] = $filename;
83
if (preg_match('/(\d+)x(\d+)\./i', $filename, $matches)) {
84
$this->_resources[$name]['x'] = (int) $matches[1];
85
$this->_resources[$name]['y'] = (int) $matches[2];
91
* Make a copy of some resource
93
function copyResource($otherRes) {
94
$this->_resourceCounter++;
95
$this->_undestroyedResourceCounter++;
96
$newRes = 'Resource #' . $this->_resourceCounter;
97
$this->_resources[$newRes] = $this->_resources[$otherRes];
100
function setResourceData($res, $field, $value) {
101
$this->_resources[$res][$field] = $value;
103
function getResourceData($res, $field) {
104
if (isset($this->_resources[$res][$field])) {
105
return $this->_resources[$res][$field];
109
function isValidResource($res) {
110
return isset($this->_resources[$res]);
112
function saveResource($res, $filename, $mimeType) {
113
$this->_outputFiles[$filename] = array(
114
'x' => $this->getResourceData($res, 'x'),
115
'y' => $this->getResourceData($res, 'y'),
116
'mimeType' => $mimeType,
118
$other = $this->getResourceData($res, 'other');
119
if (is_array($other)) {
120
$this->_outputFiles[$filename] = array_merge($this->_outputFiles[$filename], $other);
123
function getOutputFileData($filename) {
124
if (!isset($this->_outputFiles[$filename])) {
127
return $this->_outputFiles[$filename];
130
function isValidFile($filename) {
131
if (preg_match('/valid/i', $filename)) {
139
* Here is our implementation of the GdFunctionality-interface
140
* This will do nothing with images, but simulate what might
144
function phpinfo($section=null) {
145
if ($section != '8') {
146
return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE), null);
148
if (!isset($this->_gdEnvironment['phpinfo(8)'])) {
149
return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE, __FILE__, __LINE__,
150
'Environment: ' . $this->getEnvironmentName()), null);
152
$out = strtr($this->_gdEnvironment['phpinfo(8)'],
153
array_flip(get_html_translation_table(HTML_ENTITIES)));
154
return array(null, $out);
157
function tempnam($dir, $prefix) {
161
function copy($filename) {
165
function unlink($filename) {
166
if (!isset($this->_outputFiles[$filename])) {
169
unset($this->_outputFiles[$filename]);
173
function chmod($file) {
174
/* ignore this for now */
177
function filesize($filename) {
179
case 'compressMe.jpg':
181
case '/valid/compressed.jpg':
182
switch ($this->_counter--) {
191
print "filesize $filename<br/>\n";
195
function functionExists($fct) {
196
$fct = strtolower($fct);
197
if (isset($this->_gdEnvironment['functions'][$fct])) {
198
return $this->_gdEnvironment['functions'][$fct];
203
function imageTypes() {
204
return $this->_gdEnvironment['imageTypes'];
208
if (!$this->functionExists('gd_info')) {
209
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
210
'Environment: ' . $this->getEnvironmentName()), null);
212
if (!isset($this->_gdEnvironment['gd_info'])) {
213
return array(GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE), null);
215
return array(null, $this->_gdEnvironment['gd_info']);
218
function getImageSize($filename) {
222
function imageCreate($width, $height) {
223
if (!$this->functionExists('imageCreate')) {
224
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
225
'Environment: ' . $this->getEnvironmentName()),
228
$res = $this->getNewResource();
229
$this->setResourceData($res, 'x', $width);
230
$this->setResourceData($res, 'y', $height);
231
$this->setResourceData($res, 'trueColor', false);
232
return array(null, $res);
235
function imageCreateTruecolor($width, $height) {
236
if (!$this->functionExists('imageCreateTruecolor')) {
237
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
238
'Environment: ' . $this->getEnvironmentName()),
241
$res = $this->getNewResource();
242
$this->setResourceData($res, 'x', $width);
243
$this->setResourceData($res, 'y', $height);
244
$this->setResourceData($res, 'trueColor', true);
245
return array(null, $res);
248
function imageCreateFromGif($filename) {
249
if (!$this->functionExists('imageCreateFromGif')) {
250
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
251
'Environment: ' . $this->getEnvironmentName()),
254
$res = $this->getNewResource($filename);
255
return array(null, $res);
258
function imageCreateFromJpeg($filename) {
259
if (!$this->functionExists('imageCreateFromJpeg')) {
260
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
261
'Environment: ' . $this->getEnvironmentName()),
264
$res = $this->getNewResource($filename);
265
return array(null, $res);
268
function imageCreateFromPng($filename) {
269
if (!$this->functionExists('imageCreateFromPng')) {
270
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
271
'Environment: ' . $this->getEnvironmentName()),
274
$res = $this->getNewResource($filename);
275
return array(null, $res);
278
function imageCreateFromWbmp($filename) {
279
if (!$this->functionExists('imageCreateFromWbmp')) {
280
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
281
'Environment: ' . $this->getEnvironmentName()),
284
$res = $this->getNewResource($filename);
285
return array(null, $res);
288
function imageCreateFromXbm($filename) {
289
if (!$this->functionExists('imageCreateFromXbm')) {
290
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
291
'Environment: ' . $this->getEnvironmentName()),
294
$res = $this->getNewResource($filename);
295
return array(null, $res);
298
function imageCreateFromXpm($filename) {
299
if (!$this->functionExists('imageCreateFromXpm')) {
300
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
301
'Environment: ' . $this->getEnvironmentName()),
304
$res = $this->getNewResource($filename);
305
return array(null, $res);
308
function imageGif($res, $filename) {
309
if (!$this->functionExists('imageGif')) {
310
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
311
'Environment: ' . $this->getEnvironmentName());
313
if (!$this->isValidResource($res)) {
314
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
316
if (!$this->isValidFile($filename)) {
317
return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
319
$this->saveResource($res, $filename, 'image/gif');
323
function imageJpeg($res, $filename, $quality) {
324
if (!$this->functionExists('imageJpeg')) {
325
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
326
'Environment: ' . $this->getEnvironmentName());
328
if (!$this->isValidResource($res)) {
329
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
331
if (!$this->isValidFile($filename)) {
332
return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
334
$this->saveResource($res, $filename, 'image/jpeg');
338
function imagePng($res, $filename) {
339
if (!$this->functionExists('imagePng')) {
340
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
341
'Environment: ' . $this->getEnvironmentName());
343
if (!$this->isValidResource($res)) {
344
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
346
if (!$this->isValidFile($filename)) {
347
return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
349
$this->saveResource($res, $filename, 'image/png');
353
function imageWbmp($res, $filename) {
354
if (!$this->functionExists('imageWbmp')) {
355
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
356
'Environment: ' . $this->getEnvironmentName());
358
if (!$this->isValidResource($res)) {
359
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
361
if (!$this->isValidFile($filename)) {
362
return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
364
$this->saveResource($res, $filename, 'image/vnd.wap.wbmp');
368
function imageXbm($res, $filename) {
369
if (!$this->functionExists('imageXbm')) {
370
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
371
'Environment: ' . $this->getEnvironmentName());
373
if (!$this->isValidResource($res)) {
374
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
376
if (!$this->isValidFile($filename)) {
377
return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
379
$this->saveResource($res, $filename, 'image/x-xbitmap');
383
function imageXpm($res, $filename) {
384
if (!$this->functionExists('imageXpm')) {
385
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
386
'Environment: ' . $this->getEnvironmentName());
388
if (!$this->isValidResource($res)) {
389
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
391
if (!$this->isValidFile($filename)) {
392
return GalleryCoreApi::error(ERROR_TOOLKIT_FAILURE);
394
$this->saveResource($res, $filename, 'image/x-xpixmap');
398
function imageCopyResampled($dstRes, $srcRes, $dstX, $dstY, $srcX,
399
$srcY, $dstW, $dstH, $srcW, $srcH) {
400
if (!$this->functionExists('imageCopyResampled')) {
401
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
402
'Environment: ' . $this->getEnvironmentName());
404
if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
405
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
410
function imageCopyResized($dstRes, $srcRes, $dstX, $dstY, $srcX,
411
$srcY, $dstW, $dstH, $srcW, $srcH) {
412
if (!$this->functionExists('imageCopyResized')) {
413
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
414
'Environment: ' . $this->getEnvironmentName());
416
if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
417
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
422
function imageCopyMerge($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH, $pct) {
423
if (!$this->functionExists('imageCopyMerge')) {
424
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
425
'Environment: ' . $this->getEnvironmentName());
427
if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
428
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
433
function imageCopy($dstRes, $srcRes, $dstX, $dstY, $srcX, $srcY, $srcW, $srcH) {
434
if (!$this->functionExists('imageCopy')) {
435
return GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
436
'Environment: ' . $this->getEnvironmentName());
438
if (!$this->isValidResource($srcRes) || !$this->isValidResource($dstRes)) {
439
return GalleryCoreApi::error(ERROR_BAD_PARAMETER);
442
$this->setResourceData(
444
array('copy' => array(
445
$this->getResourceData($srcRes, 'filename'),
446
$dstX, $dstY, $srcX, $srcY, $srcW, $srcH)));
450
function imageSx($res) {
451
if (!$this->functionExists('imageSx')) {
452
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
453
'Environment: ' . $this->getEnvironmentName()),
456
if (!$this->isValidResource($res)) {
457
return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER),
460
return array(null, $this->getResourceData($res, 'x'));
463
function imageSy($res) {
464
if (!$this->functionExists('imageSy')) {
465
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
466
'Environment: ' . $this->getEnvironmentName()),
469
if (!$this->isValidResource($res)) {
470
return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER),
473
return array(null, $this->getResourceData($res, 'y'));
476
/* Currently this only handles rotation in 90deg-steps */
477
function imageRotate($srcRes, $angle, $bgdColor) {
478
if (!$this->functionExists('imageRotate')) {
479
return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
480
'Environment: ' . $this->getEnvironmentName()),
483
if ($angle % 90 == 0) {
484
/* Swap width and height */
485
$newX = $this->getResourceData($srcRes, 'y');
486
$newY = $this->getResourceData($srcRes, 'x');
488
$newX = $this->getResourceData($srcRes, 'x');
489
$newY = $this->getResourceData($srcRes, 'y');
491
$res = $this->copyResource($srcRes);
492
$this->setResourceData($res, 'x', $newX);
493
$this->setResourceData($res, 'y', $newY);
494
return array(null, $res);
497
function imagedestroy($res) {
498
$this->_undestroyedResourceCounter--;
499
$this->_resources[$res]['destroyed'] = true;