-
Notifications
You must be signed in to change notification settings - Fork 1
/
Amslib_Image.php
828 lines (688 loc) · 20.7 KB
/
Amslib_Image.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
<?php
/*******************************************************************************
* Copyright (c) {15/03/2008} {Christopher Thomas}
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contributors/Author:
* {Christopher Thomas} - Creator - chris.thomas@antimatter-studios.com
*
*******************************************************************************/
/**
* class: Amslib_Image
*
* group: core
*
* file: Amslib_Image.php
*
* description: Amslib Image manipulation object
*
* todo: write documentation
*
*/
class Amslib_Image
{
protected $commands = false;
protected $cache_dir = false;
protected $cache_params = false;
protected $image_params = false;
protected $permit_ext = false;
protected $error = false;
protected $errorState = false;
const COMMAND_SEQUENCE_INVALID = "The sequence of commands given was not acceptable";
const ERROR_PHPGD_NOT_FOUND = "PHP GD not found, cannot continue";
const ERROR_NO_CACHE_DIR = "There was no cache directory set";
const ERROR_CREATE_IMAGE_OBJECT = "The system could not create an image object to handle your request";
const ERROR_IMAGE_OBJECT_INVALID = "The PHP GD Image Object was invalid";
const ERROR_IMAGE_OBJECT_CLOSE = "It was not possible to close the image object";
const ERROR_IMAGE_DIMENSIONS_INVALID = "The dimensions of this image were not valid";
const ERROR_RESIZE_IMAGE_OBJECT_FAILED = "Resizing the image has failed";
const ERROR_FILE_EXTENSION_INVALID = "extension was invalid, permitted extensions are jpeg,jpg.png,gif";
const ERROR_FILE_NOT_EXIST = "The file requested cannot be found (doesn't exist?)";
const ERROR_WRITE_DIRECTORY_NOT_EXIST = "destination directory does not exist";
const ERROR_WRITE_FILE_EXIST = "file destination exists, delete original file first";
const ERROR_CACHE_NOT_FOUND = "The image could not be found in the cache";
const ERROR_MAXDIM_FAILED = "maxdim method failed to process correctly";
/**
* method: getMIMEType
*
* Obtain the mime type of a file based on the extension the file has, assuming it's not lying
*
* parameters:
* $filename - The filename to look for the file extension to determine the mime type to use
*
* returns:
* Boolean false if the file extension was not recognised, or a mime string for the file extension given
*
* notes:
* If the file is a GIF and the extension says .jpg, this function will not detect that
*/
protected function getMIMEType($filename)
{
$extension = end(explode(".",$filename));
switch($extension){
case "jpeg":
case "jpg":{ return "image/jpeg"; }break;
case "png":{ return "image/png"; }break;
case "gif":{ return "image/gif"; }break;
}
return false;
}
/**
* method: getFileExtension
*
* Obtain the file extension from a given filename and if found, store the mime type in the cache array
*
* parameters:
* $filename - The filename to obtain the extension from
*
* returns:
* Boolean false if the extension was not found, or the extension if it was found
*
* notes:
* It's inconsistent to set the cache param for the mime type here, but not the file extension
* As where this function is used, it's almost always used when setting the file extension, perhaps
* This function should be called setFileExtension instead and just return the extension afterwards
* Then we could hide the set cache param for the file extension inside here too and clean up the code
*/
protected function getFileExtension($filename)
{
$extension = Amslib_File::getFileExtension($filename);
$extension = (in_array($extension,$this->permit_ext)) ? $extension : false;
if($extension !== false){
$this->setCacheParam("mime_type",$this->getMIMEType($filename));
}
return $extension;
}
/**
* method: setCacheParam
*
* Set a parameter in the cache array, which is an object of valid data used when processing images
*
* parameters:
* $key - The key to set in the cache array
* $value - The value to set to the key
*
* returns:
* If the key was not a string, this function returns NULL, or the value that was set
*/
protected function setCacheParam($key,$value=NULL)
{
if(!is_string($key)) return NULL;
$this->cache_params[$key] = $value;
return $value;
}
/**
* method: getCacheParam
*
* Get a parameter from the cache array
*
* parameters:
* $key - The key to obtain
* $default - The default value to use if the key was not present
*
* returns:
* If the value is not present, it'll return the $default value, otherwise,
* the value from the cache array
*/
protected function getCacheParam($key,$default=NULL)
{
return array_key_exists($key,$this->cache_params) ? $this->cache_params[$key] : $default;
}
/**
* method: resetCacheParam
*
* Resets the cache array to completely empty, ready to fill again
*/
protected function resetCacheParam()
{
$this->cache_params = array();
}
/**
* method: setImageParam
*
* todo: write documentation
*/
protected function setImageParam($key,$value)
{
if(!is_string($key)) return NULL;
$this->image_params[$key] = $value;
return $value;
}
/**
* method: getImageParam
*
* todo: write documentation
*/
protected function getImageParam($key,$default=NULL)
{
return array_key_exists($key,$this->image_params) ? $this->image_params[$key] : $default;
}
/**
* method: resetImageParam
*
* todo: write documentation
*/
protected function resetImageParam()
{
$this->image_params = Amslib_Array::removeKeys($this->image_params,array("handle","width","height"));
}
/**
* method: getCacheKey
*
* todo: write documentation
*/
protected function getCacheKey()
{
return sha1(http_build_query($this->cache_params)).".".$this->cache_params["extension"];
}
/**
* method: getCacheFilename
*
* todo: write documentation
*/
protected function getCacheFilename()
{
return Amslib_File::reduceSlashes($this->cache_dir."/".$this->getCacheKey());
}
/**
* method: getDimensions
*
* todo: write documentation
*/
protected function getDimensions()
{
if(!$this->getImageParam("handle")){
return $this->setError(self::ERROR_IMAGE_OBJECT_INVALID);
}
return array(
"width" => intval($this->getImageParam("width")),
"height" => intval($this->getImageParam("height"))
);
}
/**
* method: __construct
*
* todo: write documentation
*/
public function __construct()
{
if(!function_exists('imagecreatetruecolor')) {
$this->setError(self::ERROR_PHPGD_NOT_FOUND);
}
$this->permit_ext = array("jpeg","jpg","gif","png");
$this->resetCacheParam();
$this->resetImageParam();
}
/**
* method: setCacheDirectory
*
* todo: write documentation
*/
public function setCacheDirectory($directory)
{
$directory = Amslib_File::reduceSlashes($directory);
if(!is_dir($directory) && !file_exists($directory)){
if(!Amslib_File::mkdir($directory)) return false;
}
$this->cache_dir = $directory;
}
/**
* method: setError
*
* todo: write documentation
*/
public function setError($error)
{
$this->error = $error;
return false;
}
/**
* method: getError
*
* todo: write documentation
*/
public function getError()
{
return is_array($this->error)
? Amslib_Debug::dump($this->error)
: $this->error;
}
/**
* method: setErrorState
*
* todo: write documentation
*/
public function setErrorState($state)
{
$this->errorState = $state;
}
/**
* method: getErrorState
*
* todo: write documentation
*/
public function getErrorState()
{
return $this->errorState;
}
/**
* method: clearCache
*
* todo: write documentation
*/
public function clearCache()
{
if(!is_dir($this->cache_dir)){
return $this->setError(self::ERROR_NO_CACHE_DIR);
}
$list = glob("$this->cache_dir/*");
foreach($list as $i){
// Skip directories of files with .. in the filename (PREVENT HACKS)
if(is_dir($i)) continue;
if(strpos($i,"..")) continue;
// Just in case someone managed to write a file with .. in it (back directory)
ob_start();
unlink($i);
$error = ob_get_clean();
if(strlen($error)){
return $this->setError($error);
}
}
return true;
}
/**
* method: setCommands
*
* todo: write documentation
*/
public function setCommands($source)
{
if(is_array($source)){
$this->commands = $source;
}elseif(is_string($source)){
$this->commands = explode("/",trim(Amslib_String::rchop($source,"?"),"/"));
}else{
return $this->setError(self::COMMAND_SEQUENCE_INVALID);
}
return true;
}
/**
* method: processCommands
*
* todo: write documentation
*/
public function processCommands($commands=NULL)
{
if(!$commands) $commands = $this->commands;
$cache = array_shift($commands);
if($cache != "cache") return false;
$filename = Amslib_Website::absolute();
$filename = rtrim($filename,"/");
$search_file = true;
while($commands){
$c = array_shift($commands);
if($search_file){
$filename .= "/$c";
if(is_file($filename)){
$this->open($filename);
$search_file = false;
}
}else{
switch($c){
case "regenerate":{
}break;
case "maxdim":{
$width = array_shift($commands);
$height = array_shift($commands);
$state = $this->maxdim($width,$height);
if(!$state) return $state;
}break;
case "crop":{
$x1 = array_shift($commands);
$y1 = array_shift($commands);
$x2 = array_shift($commands);
$y2 = array_shift($commands);
$state = $this->crop($x1,$y1,$x2,$y2);
if(!$state) return $state;
}break;
case "show_errors":{
$this->setErrorState(true);
}break;
};
}
}
return true;
}
/**
* method: open
*
* todo: write documentation
*/
public function open($filename)
{
// get file extension and if not valid, return false
$ext = $this->setImageParam("extension",$this->getFileExtension($filename));
// if false, ERROR_FILE_EXTENSION_INVALID
if(!$ext) return $this->setError(self::ERROR_FILE_EXTENSION_INVALID);
$filename = $this->setImageParam("filename",$filename);
// By default, set the handle to false (invalid)
$this->setImageParam("handle",false);
// If false, ERROR_FILE_NOT_EXIST
if(!file_exists($filename)) return $this->setError(self::ERROR_FILE_NOT_EXIST);
$handle = false;
switch($ext){
case "jpeg":
case "jpg":{ $handle = imagecreatefromjpeg($filename); }break;
case "png":{ $handle = imagecreatefrompng($filename); }break;
case "gif":{ $handle = imagecreatefromgif($filename); }break;
}
// If false, ERROR_CREATE_IMAGE_OBJECT
if(!$handle) return $this->setError(self::ERROR_CREATE_IMAGE_OBJECT);
$this->setImageParam("handle",$handle);
$this->setImageParam("width",intval(imagesx($handle)));
$this->setImageParam("height",intval(imagesy($handle)));
return true;
}
/**
* method: close
*
* todo: write documentation
*/
public function close()
{
$handle = $this->getImageParam("handle");
if(!$handle) return $this->setError(self::ERROR_IMAGE_OBJECT_INVALID);
imagedestroy($handle);
$this->resetImageParam();
return true;
}
/**
* method: maxdim
*
* todo: write documentation
*/
public function maxdim($width,$height)
{
if(!$this->getImageParam("handle")){
return $this->setError(self::ERROR_IMAGE_OBJECT_INVALID);
}
$d = $this->getDimensions();
$width = intval($width);
$height = intval($height);
// If the width and height passed are empty, we can default to the original size, so it at least works, but not optimally
if(!$width) $width = $d["width"];
if(!$height) $height = $d["height"];
// if failed, ERROR_IMAGE_DIMENSIONS_INVALID
if(!$d || !$width || !$height){
return $this->setError(self::ERROR_IMAGE_DIMENSIONS_INVALID.", w($width):h($height)");
}
$a = $d["width"] / $d["height"];
if($a > 1){
// wider image
if($d["width"] != $width){
$ratio = $width / $d["width"];
$d["width"] = $d["width"] * $ratio;
$d["height"] = $d["height"] * $ratio;
}
if($d["height"] > $height){
$ratio = $height / $d["height"];
$d["width"] = $d["width"] * $ratio;
$d["height"] = $d["height"] * $ratio;
}
}else{
// taller image
// wider image
if($d["height"] != $height){
$ratio = $height / $d["height"];
$d["width"] = $d["width"] * $ratio;
$d["height"] = $d["height"] * $ratio;
}
if($d["width"] > $width){
$ratio = $width / $d["width"];
$d["width"] = $d["width"] * $ratio;
$d["height"] = $d["height"] * $ratio;
}
}
return $this->resize($d["width"],$d["height"]);
}
/**
* method: crop
*
* todo: write documentation
*/
public function crop($x1,$y1,$x2,$y2)
{
$handle = $this->getImageParam("handle");
if(!$handle){
return $this->setError(self::ERROR_IMAGE_OBJECT_INVALID);
}
$x1 = intval($x1);
$y1 = intval($y1);
$x2 = intval($x2);
$y2 = intval($y2);
$dx = intval($x2-$x1);
$dy = intval($y2-$y1);
if($dx < 1 || $dy < 1){
return $this->setError(self::ERROR_IMAGE_DIMENSIONS_INVALID);
}
$tmp_image = imagecreatetruecolor($dx,$dy);
// if failed, ERROR_CREATE_IMAGE_OBJECT
if(!$tmp_image) return $this->setError(self::ERROR_CREATE_IMAGE_OBJECT);
//imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
$success = imagecopyresampled(
$tmp_image,
$handle,
0, 0,
$x1, $y1,
$dx, $dy,
$dx, $dy
);
// if failed, ERROR_RESIZE_IMAGE_OBJECT_FAILED
if(!$success) return $this->setError(self::ERROR_RESIZE_IMAGE_OBJECT_FAILED);
// if failed, ERROR_CLOSE_IMAGE_OBJECT
if(!$this->close()) return $this->setError(self::ERROR_IMAGE_OBJECT_CLOSE);
$this->setImageParam("handle",$tmp_image);
$this->setImageParam("width",$dx);
$this->setImageParam("height",$dy);
return true;
}
/**
* method: resize
*
* todo: write documentation
*/
public function resize($width,$height)
{
$handle = $this->getImageParam("handle");
if(!$handle){
return $this->setError(self::ERROR_IMAGE_OBJECT_INVALID);
}
$width = intval($width);
$height = intval($height);
if(!$width || !$height){
return $this->setError(self::ERROR_IMAGE_DIMENSIONS_INVALID);
}
$tmp_image = imagecreatetruecolor($width,$height);
// if failed, ERROR_CREATE_IMAGE_OBJECT
if(!$tmp_image) return $this->setError(self::ERROR_CREATE_IMAGE_OBJECT);
/** FUTURE IMPROVEMENT, NEEDS TESTING
* // preserve transparency
if($type == "gif" or $type == "png"){
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
}
*/
$success = imagecopyresampled(
$tmp_image,
$handle,
0, 0,
0, 0,
$width, $height,
$this->getImageParam("width"), $this->getImageParam("height")
);
// if failed, ERROR_RESIZE_IMAGE_OBJECT_FAILED
if(!$success) return $this->setError(self::ERROR_RESIZE_IMAGE_OBJECT_FAILED);
// if failed, ERROR_CLOSE_IMAGE_OBJECT
if(!$this->close()) return $this->setError(self::ERROR_IMAGE_OBJECT_CLOSE);
$this->setImageParam("handle",$tmp_image);
$this->setImageParam("width",$width);
$this->setImageParam("height",$height);
$this->sharpen();
return true;
}
/**
* method: sharpen
*
* todo: write documentation
*/
public function sharpen()
{
$handle = $this->getImageParam("handle");
if(!$handle){
return $this->setError(self::ERROR_IMAGE_OBJECT_INVALID);
}
$matrix = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($handle, $matrix, $divisor, $offset);
}
/**
* method: readCache
*
* todo: write documentation
*/
public function readCache($filename=NULL,$returnData=false)
{
// If the filename was not given, attempt to get it from the cache
// but only if it appears to be a processed image
// NOTE: if we cannot find the information inside the image params, then we didnt process it
// we are only really interested in working with files we've already processed
if($filename == NULL && $this->getCacheParam("filename") == $this->getImageParam("filename")){
$filename = $this->getCacheParam("filename");
}
// If cache is not enabled, return false
if(!$this->cache_dir) return $this->setError(self::ERROR_NO_CACHE_DIR);
// get file extension and if not valid, return false
$ext = $this->setCacheParam("extension",$this->getFileExtension($filename));
if(!$ext) return $this->setError(self::ERROR_FILE_EXTENSION_INVALID);
$this->setCacheParam("filename",$filename);
$cache_filename = $this->getCacheFilename();
// does the file exist in the cache
if(file_exists($cache_filename)){
// if yes, readfile($cache) or return data
if($returnData){
return file_get_contents($cache_filename);
}
//die($cache_filename);
header("Content-Type: ".$this->getCacheParam("mime_type"));
readfile($cache_filename);
return true;
}
// if no, read original file into cache, then attempt to get from cache again
if($this->writeCacheFromFile($filename)){
return $this->readCache($filename,$returnData);
}
// oh well, on images for you little jimmy...go cry to your mother...
return $this->setError(self::ERROR_CACHE_NOT_FOUND);
}
/**
* method: writeCacheFromFile
*
* todo: write documentation
*/
public function writeCacheFromFile($filename)
{
$filename = Amslib_Website::absolute($filename);
$cache = $this->getCacheFilename();
return copy($filename,$cache) && chmod($cache,0755);
}
/**
* method: writeCacheFromImage
*
* todo: write documentation
*/
public function writeCacheFromImage()
{
$handle = $this->getImageParam("handle");
$filename = $this->getImageParam("filename");
$ext = $this->getImageParam("extension");
$this->setCacheParam("extension",$ext);
$this->setCacheParam("filename",$filename);
$this->setCacheParam("width",$this->getImageParam("width"));
$this->setCacheParam("height",$this->getImageParam("height"));
$cache = $this->getCacheFilename();
switch($ext){
case "jpeg":
case "jpg":{ imagejpeg($handle,$cache); }break;
case "png":{ imagepng($handle,$cache); }break;
case "gif":{ imagegif($handle,$cache); }break;
};
// Write the file to a new destination, or to the browser
return file_exists($cache) && chmod($cache,0755);
}
/**
* method: imageCopyResampleBicubic
*
* todo: write documentation
*/
public function imageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
{
$scaleX = ($src_w - 1) / $dst_w;
$scaleY = ($src_h - 1) / $dst_h;
$scaleX2 = $scaleX / 2.0;
$scaleY2 = $scaleY / 2.0;
$tc = imageistruecolor($src_img);
for ($y = $src_y; $y < $src_y + $dst_h; $y++)
{
$sY = $y * $scaleY;
$siY = (int) $sY;
$siY2 = (int) $sY + $scaleY2;
for ($x = $src_x; $x < $src_x + $dst_w; $x++)
{
$sX = $x * $scaleX;
$siX = (int) $sX;
$siX2 = (int) $sX + $scaleX2;
if ($tc)
{
$c1 = imagecolorat($src_img, $siX, $siY2);
$c2 = imagecolorat($src_img, $siX, $siY);
$c3 = imagecolorat($src_img, $siX2, $siY2);
$c4 = imagecolorat($src_img, $siX2, $siY);
$r = (($c1 + $c2 + $c3 + $c4) >> 2) & 0xFF0000;
$g = ((($c1 & 0xFF00) + ($c2 & 0xFF00) + ($c3 & 0xFF00) + ($c4 & 0xFF00)) >> 2) & 0xFF00;
$b = ((($c1 & 0xFF) + ($c2 & 0xFF) + ($c3 & 0xFF) + ($c4 & 0xFF)) >> 2);
imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
}
else
{
$c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
$c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
$c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
$c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
$r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] ) << 14;
$g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
$b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] ) >> 2;
imagesetpixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r+$g+$b);
}
}
}
return true;
}
}