]> git.mxchange.org Git - friendica.git/blob - include/Photo.php
The temporary paths (temp, lock, itemcache) are now detected automatically when used.
[friendica.git] / include / Photo.php
1 <?php
2
3 if(! class_exists("Photo")) {
4 class Photo {
5
6     private $image;
7
8     /**
9      * Put back gd stuff, not everybody have Imagick
10      */
11     private $imagick;
12     private $width;
13     private $height;
14     private $valid;
15     private $type;
16     private $types;
17
18     /**
19      * supported mimetypes and corresponding file extensions
20      */
21     static function supportedTypes() {
22         if(class_exists('Imagick')) {
23             /**
24              * Imagick::queryFormats won't help us a lot there...
25              * At least, not yet, other parts of friendica uses this array
26              */
27             $t = array(
28                 'image/jpeg' => 'jpg',
29                 'image/png' => 'png',
30                 'image/gif' => 'gif'
31             );
32         } else {
33             $t = array();
34             $t['image/jpeg'] ='jpg';
35             if (imagetypes() & IMG_PNG) $t['image/png'] = 'png';
36         }
37
38         return $t;
39     }
40
41     public function __construct($data, $type=null) {
42         $this->imagick = class_exists('Imagick');
43         $this->types = $this->supportedTypes();
44         if (!array_key_exists($type,$this->types)){
45             $type='image/jpeg';
46         }
47         $this->type = $type;
48
49         if($this->is_imagick() && $this->load_data($data)) {
50                         return true;
51                 } else {
52                         // Failed to load with Imagick, fallback
53                         $this->imagick = false;
54                 }
55                 return $this->load_data($data);
56     }
57
58     public function __destruct() {
59         if($this->image) {
60             if($this->is_imagick()) {
61                 $this->image->clear();
62                 $this->image->destroy();
63                 return;
64             }
65             imagedestroy($this->image);
66         }
67     }
68
69     public function is_imagick() {
70         return $this->imagick;
71     }
72
73     /**
74      * Maps Mime types to Imagick formats
75      */
76     public function get_FormatsMap() {
77         $m = array(
78             'image/jpeg' => 'JPG',
79             'image/png' => 'PNG',
80             'image/gif' => 'GIF'
81         );
82         return $m;
83     }
84
85     private function load_data($data) {
86                 if($this->is_imagick()) {
87                         $this->image = new Imagick();
88             try {
89                                 $this->image->readImageBlob($data);
90                         }
91                         catch (Exception $e) {
92                                 // Imagick couldn't use the data
93                                 return false;
94                         }
95
96             /**
97              * Setup the image to the format it will be saved to
98              */
99             $map = $this->get_FormatsMap();
100             $format = $map[$type];
101             $this->image->setFormat($format);
102
103             // Always coalesce, if it is not a multi-frame image it won't hurt anyway
104             $this->image = $this->image->coalesceImages();
105
106             /**
107              * setup the compression here, so we'll do it only once
108              */
109             switch($this->getType()){
110                 case "image/png":
111                     $quality = get_config('system','png_quality');
112                     if((! $quality) || ($quality > 9))
113                         $quality = PNG_QUALITY;
114                     /**
115                      * From http://www.imagemagick.org/script/command-line-options.php#quality:
116                      *
117                      * 'For the MNG and PNG image formats, the quality value sets
118                      * the zlib compression level (quality / 10) and filter-type (quality % 10).
119                      * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
120                      * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
121                      */
122                     $quality = $quality * 10;
123                     $this->image->setCompressionQuality($quality);
124                     break;
125                 case "image/jpeg":
126                     $quality = get_config('system','jpeg_quality');
127                     if((! $quality) || ($quality > 100))
128                         $quality = JPEG_QUALITY;
129                     $this->image->setCompressionQuality($quality);
130             }
131
132                         // The 'width' and 'height' properties are only used by non-Imagick routines.
133                         $this->width  = $this->image->getImageWidth();
134                         $this->height = $this->image->getImageHeight();
135                         $this->valid  = true;
136
137                         return true;
138                 }
139
140                 $this->valid = false;
141                 $this->image = @imagecreatefromstring($data);
142                 if($this->image !== FALSE) {
143                         $this->width  = imagesx($this->image);
144                         $this->height = imagesy($this->image);
145                         $this->valid  = true;
146                         imagealphablending($this->image, false);
147                         imagesavealpha($this->image, true);
148
149                         return true;
150                 }
151                 
152                 return false;
153         }
154
155     public function is_valid() {
156         if($this->is_imagick())
157             return ($this->image !== FALSE);
158         return $this->valid;
159     }
160
161     public function getWidth() {
162         if(!$this->is_valid())
163             return FALSE;
164
165         if($this->is_imagick())
166             return $this->image->getImageWidth();
167         return $this->width;
168     }
169
170     public function getHeight() {
171         if(!$this->is_valid())
172             return FALSE;
173
174         if($this->is_imagick())
175             return $this->image->getImageHeight();
176         return $this->height;
177     }
178
179     public function getImage() {
180         if(!$this->is_valid())
181             return FALSE;
182
183         if($this->is_imagick()) {
184             /* Clean it */
185             $this->image = $this->image->deconstructImages();
186             return $this->image;
187         }
188         return $this->image;
189     }
190
191     public function getType() {
192         if(!$this->is_valid())
193             return FALSE;
194
195         return $this->type;
196     }
197
198     public function getExt() {
199         if(!$this->is_valid())
200             return FALSE;
201
202         return $this->types[$this->getType()];
203     }
204
205     public function scaleImage($max) {
206         if(!$this->is_valid())
207             return FALSE;
208
209         $width = $this->getWidth();
210         $height = $this->getHeight();
211
212         $dest_width = $dest_height = 0;
213
214         if((! $width)|| (! $height))
215             return FALSE;
216
217         if($width > $max && $height > $max) {
218
219                         // very tall image (greater than 16:9)
220                         // constrain the width - let the height float.
221
222                         if((($height * 9) / 16) > $width) {
223                                 $dest_width = $max;
224                 $dest_height = intval(( $height * $max ) / $width);
225                         }
226
227                         // else constrain both dimensions
228
229                         elseif($width > $height) {
230                 $dest_width = $max;
231                 $dest_height = intval(( $height * $max ) / $width);
232             }
233             else {
234                 $dest_width = intval(( $width * $max ) / $height);
235                 $dest_height = $max;
236             }
237         }
238         else {
239             if( $width > $max ) {
240                 $dest_width = $max;
241                 $dest_height = intval(( $height * $max ) / $width);
242             }
243             else {
244                 if( $height > $max ) {
245
246                                         // very tall image (greater than 16:9)
247                                         // but width is OK - don't do anything
248
249                                         if((($height * 9) / 16) > $width) {
250                                                 $dest_width = $width;
251                                 $dest_height = $height;
252                                         }
253                                         else {
254                             $dest_width = intval(( $width * $max ) / $height);
255                         $dest_height = $max;
256                                         }
257                 }
258                 else {
259                     $dest_width = $width;
260                     $dest_height = $height;
261                 }
262             }
263         }
264
265
266         if($this->is_imagick()) {
267                         /**
268                          * If it is not animated, there will be only one iteration here,
269                          * so don't bother checking
270                          */
271                         // Don't forget to go back to the first frame
272                         $this->image->setFirstIterator();
273                         do {
274
275                                 // FIXME - implement horizantal bias for scaling as in followin GD functions
276                                 // to allow very tall images to be constrained only horizontally. 
277
278                                 $this->image->scaleImage($dest_width, $dest_height);
279                         } while ($this->image->nextImage());
280
281                         // These may not be necessary any more
282                         $this->width  = $this->image->getImageWidth();
283                         $this->height = $this->image->getImageHeight();
284
285                         return;
286         }
287
288
289         $dest = imagecreatetruecolor( $dest_width, $dest_height );
290         imagealphablending($dest, false);
291         imagesavealpha($dest, true);
292         if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
293         imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
294         if($this->image)
295             imagedestroy($this->image);
296         $this->image = $dest;
297         $this->width  = imagesx($this->image);
298         $this->height = imagesy($this->image);
299     }
300
301     public function rotate($degrees) {
302         if(!$this->is_valid())
303             return FALSE;
304
305         if($this->is_imagick()) {
306             $this->image->setFirstIterator();
307             do {
308                 $this->image->rotateImage(new ImagickPixel(), -$degrees); // ImageMagick rotates in the opposite direction of imagerotate()
309             } while ($this->image->nextImage());
310             return;
311         }
312
313         $this->image  = imagerotate($this->image,$degrees,0);
314         $this->width  = imagesx($this->image);
315         $this->height = imagesy($this->image);
316     }
317
318     public function flip($horiz = true, $vert = false) {
319         if(!$this->is_valid())
320             return FALSE;
321
322         if($this->is_imagick()) {
323             $this->image->setFirstIterator();
324             do {
325                 if($horiz) $this->image->flipImage();
326                 if($vert) $this->image->flopImage();
327             } while ($this->image->nextImage());
328             return;
329         }
330
331         $w = imagesx($this->image);
332         $h = imagesy($this->image);
333         $flipped = imagecreate($w, $h);
334         if($horiz) {
335             for ($x = 0; $x < $w; $x++) {
336                 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
337             }
338         }
339         if($vert) {
340             for ($y = 0; $y < $h; $y++) {
341                 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
342             }
343         }
344         $this->image = $flipped;
345     }
346
347     public function orient($filename) {
348         // based off comment on http://php.net/manual/en/function.imagerotate.php
349
350         if(!$this->is_valid())
351             return FALSE;
352
353         if( (! function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg') )
354             return;
355
356         $exif = @exif_read_data($filename);
357
358                 if(! $exif)
359                         return;
360
361         $ort = $exif['Orientation'];
362
363         switch($ort)
364         {
365             case 1: // nothing
366                 break;
367
368             case 2: // horizontal flip
369                 $this->flip();
370                 break;
371
372             case 3: // 180 rotate left
373                 $this->rotate(180);
374                 break;
375
376             case 4: // vertical flip
377                 $this->flip(false, true);
378                 break;
379
380             case 5: // vertical flip + 90 rotate right
381                 $this->flip(false, true);
382                 $this->rotate(-90);
383                 break;
384
385             case 6: // 90 rotate right
386                 $this->rotate(-90);
387                 break;
388
389             case 7: // horizontal flip + 90 rotate right
390                 $this->flip();
391                 $this->rotate(-90);
392                 break;
393
394             case 8:    // 90 rotate left
395                 $this->rotate(90);
396                 break;
397         }
398     }
399
400
401
402     public function scaleImageUp($min) {
403         if(!$this->is_valid())
404             return FALSE;
405
406
407         $width = $this->getWidth();
408         $height = $this->getHeight();
409
410         $dest_width = $dest_height = 0;
411
412         if((! $width)|| (! $height))
413             return FALSE;
414
415         if($width < $min && $height < $min) {
416             if($width > $height) {
417                 $dest_width = $min;
418                 $dest_height = intval(( $height * $min ) / $width);
419             }
420             else {
421                 $dest_width = intval(( $width * $min ) / $height);
422                 $dest_height = $min;
423             }
424         }
425         else {
426             if( $width < $min ) {
427                 $dest_width = $min;
428                 $dest_height = intval(( $height * $min ) / $width);
429             }
430             else {
431                 if( $height < $min ) {
432                     $dest_width = intval(( $width * $min ) / $height);
433                     $dest_height = $min;
434                 }
435                 else {
436                     $dest_width = $width;
437                     $dest_height = $height;
438                 }
439             }
440         }
441
442         if($this->is_imagick())
443             return $this->scaleImage($dest_width,$dest_height);
444
445         $dest = imagecreatetruecolor( $dest_width, $dest_height );
446         imagealphablending($dest, false);
447         imagesavealpha($dest, true);
448         if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
449         imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
450         if($this->image)
451             imagedestroy($this->image);
452         $this->image = $dest;
453         $this->width  = imagesx($this->image);
454         $this->height = imagesy($this->image);
455     }
456
457
458
459     public function scaleImageSquare($dim) {
460         if(!$this->is_valid())
461             return FALSE;
462
463         if($this->is_imagick()) {
464             $this->image->setFirstIterator();
465             do {
466                 $this->image->scaleImage($dim, $dim);
467             } while ($this->image->nextImage());
468             return;
469         }
470
471         $dest = imagecreatetruecolor( $dim, $dim );
472         imagealphablending($dest, false);
473         imagesavealpha($dest, true);
474         if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
475         imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
476         if($this->image)
477             imagedestroy($this->image);
478         $this->image = $dest;
479         $this->width  = imagesx($this->image);
480         $this->height = imagesy($this->image);
481     }
482
483
484     public function cropImage($max,$x,$y,$w,$h) {
485         if(!$this->is_valid())
486             return FALSE;
487
488                 if($this->is_imagick()) {
489                         $this->image->setFirstIterator();
490                         do {
491                                 $this->image->cropImage($w, $h, $x, $y);
492                                 /**
493                                  * We need to remove the canva,
494                                  * or the image is not resized to the crop:
495                                  * http://php.net/manual/en/imagick.cropimage.php#97232
496                                  */
497                                 $this->image->setImagePage(0, 0, 0, 0);
498                         } while ($this->image->nextImage());
499                         return $this->scaleImage($max);
500                 }
501
502         $dest = imagecreatetruecolor( $max, $max );
503         imagealphablending($dest, false);
504         imagesavealpha($dest, true);
505         if ($this->type=='image/png') imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
506         imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
507         if($this->image)
508             imagedestroy($this->image);
509         $this->image = $dest;
510         $this->width  = imagesx($this->image);
511         $this->height = imagesy($this->image);
512     }
513
514     public function saveImage($path) {
515         if(!$this->is_valid())
516             return FALSE;
517
518         $string = $this->imageString();
519         file_put_contents($path, $string);
520     }
521
522     public function imageString() {
523         if(!$this->is_valid())
524             return FALSE;
525
526         if($this->is_imagick()) {
527             /* Clean it */
528             $this->image = $this->image->deconstructImages();
529             $string = $this->image->getImagesBlob();
530             return $string;
531         }
532
533         $quality = FALSE;
534
535         ob_start();
536
537         // Enable interlacing
538         imageinterlace($this->image, true);
539
540         switch($this->getType()){
541             case "image/png":
542                 $quality = get_config('system','png_quality');
543                 if((! $quality) || ($quality > 9))
544                     $quality = PNG_QUALITY;
545                 imagepng($this->image,NULL, $quality);
546                 break;
547             case "image/jpeg":
548                 $quality = get_config('system','jpeg_quality');
549                 if((! $quality) || ($quality > 100))
550                     $quality = JPEG_QUALITY;
551                 imagejpeg($this->image,NULL,$quality);
552         }
553         $string = ob_get_contents();
554         ob_end_clean();
555
556         return $string;
557     }
558
559
560
561     public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '') {
562
563         $r = q("select `guid` from photo where `resource-id` = '%s' and `guid` != '' limit 1",
564             dbesc($rid)
565         );
566         if(count($r))
567             $guid = $r[0]['guid'];
568         else
569             $guid = get_guid();
570
571         $x = q("select id from photo where `resource-id` = '%s' and uid = %d and `contact-id` = %d and `scale` = %d limit 1",
572                 dbesc($rid),
573                 intval($uid),
574                 intval($cid),
575                 intval($scale)
576         );
577         if(count($x)) {
578             $r = q("UPDATE `photo`
579                 set `uid` = %d,
580                 `contact-id` = %d,
581                 `guid` = '%s',
582                 `resource-id` = '%s',
583                 `created` = '%s',
584                 `edited` = '%s',
585                 `filename` = '%s',
586                 `type` = '%s',
587                 `album` = '%s',
588                 `height` = %d,
589                 `width` = %d,
590                                 `datasize` = %d,
591                 `data` = '%s',
592                 `scale` = %d,
593                 `profile` = %d,
594                 `allow_cid` = '%s',
595                 `allow_gid` = '%s',
596                 `deny_cid` = '%s',
597                 `deny_gid` = '%s'
598                 where id = %d",
599
600                 intval($uid),
601                 intval($cid),
602                 dbesc($guid),
603                 dbesc($rid),
604                 dbesc(datetime_convert()),
605                 dbesc(datetime_convert()),
606                 dbesc(basename($filename)),
607                 dbesc($this->getType()),
608                 dbesc($album),
609                 intval($this->getHeight()),
610                 intval($this->getWidth()),
611                                 dbesc(strlen($this->imageString())),
612                 dbesc($this->imageString()),
613                 intval($scale),
614                 intval($profile),
615                 dbesc($allow_cid),
616                 dbesc($allow_gid),
617                 dbesc($deny_cid),
618                 dbesc($deny_gid),
619                 intval($x[0]['id'])
620             );
621         }
622         else {
623             $r = q("INSERT INTO `photo`
624                 ( `uid`, `contact-id`, `guid`, `resource-id`, `created`, `edited`, `filename`, type, `album`, `height`, `width`, `datasize`, `data`, `scale`, `profile`, `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid` )
625                 VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s' )",
626                 intval($uid),
627                 intval($cid),
628                 dbesc($guid),
629                 dbesc($rid),
630                 dbesc(datetime_convert()),
631                 dbesc(datetime_convert()),
632                 dbesc(basename($filename)),
633                 dbesc($this->getType()),
634                 dbesc($album),
635                 intval($this->getHeight()),
636                 intval($this->getWidth()),
637                                 dbesc(strlen($this->imageString())),
638                 dbesc($this->imageString()),
639                 intval($scale),
640                 intval($profile),
641                 dbesc($allow_cid),
642                 dbesc($allow_gid),
643                 dbesc($deny_cid),
644                 dbesc($deny_gid)
645             );
646         }
647         return $r;
648     }
649 }}
650
651
652 /**
653  * Guess image mimetype from filename or from Content-Type header
654  *
655  * @arg $filename string Image filename
656  * @arg $fromcurl boolean Check Content-Type header from curl request
657  */
658 function guess_image_type($filename, $fromcurl=false) {
659     logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
660     $type = null;
661     if ($fromcurl) {
662         $a = get_app();
663         $headers=array();
664         $h = explode("\n",$a->get_curl_headers());
665         foreach ($h as $l) {
666             list($k,$v) = array_map("trim", explode(":", trim($l), 2));
667             $headers[$k] = $v;
668         }
669         if (array_key_exists('Content-Type', $headers))
670             $type = $headers['Content-Type'];
671     }
672     if (is_null($type)){
673         // Guessing from extension? Isn't that... dangerous?
674         if(class_exists('Imagick') && file_exists($filename) && is_readable($filename)) {
675             /**
676              * Well, this not much better,
677              * but at least it comes from the data inside the image,
678              * we won't be tricked by a manipulated extension
679              */
680             $image = new Imagick($filename);
681             $type = $image->getImageMimeType();
682             $image->setInterlaceScheme(Imagick::INTERLACE_PLANE);
683         } else {
684             $ext = pathinfo($filename, PATHINFO_EXTENSION);
685             $types = Photo::supportedTypes();
686             $type = "image/jpeg";
687             foreach ($types as $m=>$e){
688                 if ($ext==$e) $type = $m;
689             }
690         }
691     }
692     logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
693     return $type;
694
695 }
696
697 function import_profile_photo($photo,$uid,$cid) {
698
699     $a = get_app();
700
701     $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
702         intval($uid),
703         intval($cid)
704     );
705     if(count($r) && strlen($r[0]['resource-id'])) {
706         $hash = $r[0]['resource-id'];
707     }
708     else {
709         $hash = photo_new_resource();
710     }
711
712     $photo_failure = false;
713
714     $filename = basename($photo);
715     $img_str = fetch_url($photo,true);
716
717     $type = guess_image_type($photo,true);
718     $img = new Photo($img_str, $type);
719     if($img->is_valid()) {
720
721         $img->scaleImageSquare(175);
722
723         $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
724
725         if($r === false)
726             $photo_failure = true;
727
728         $img->scaleImage(80);
729
730         $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
731
732         if($r === false)
733             $photo_failure = true;
734
735         $img->scaleImage(48);
736
737         $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
738
739         if($r === false)
740             $photo_failure = true;
741
742         $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
743         $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
744         $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
745     }
746     else
747         $photo_failure = true;
748
749     if($photo_failure) {
750         $photo = $a->get_baseurl() . '/images/person-175.jpg';
751         $thumb = $a->get_baseurl() . '/images/person-80.jpg';
752         $micro = $a->get_baseurl() . '/images/person-48.jpg';
753     }
754
755     return(array($photo,$thumb,$micro));
756
757 }
758
759 function get_photo_info($url) {
760         $data = array();
761
762         $data = Cache::get($url);
763
764         if (is_null($data)) {
765                 $img_str = fetch_url($url, true, $redirects, 4);
766
767                 $tempfile = tempnam(get_temppath(), "cache");
768                 file_put_contents($tempfile, $img_str);
769                 $data = getimagesize($tempfile);
770                 unlink($tempfile);
771
772                 Cache::set($url, serialize($data));
773         } else
774                 $data = unserialize($data);
775
776         return $data;
777 }
778
779 function scale_image($width, $height, $max) {
780
781         $dest_width = $dest_height = 0;
782
783         if((!$width) || (!$height))
784                 return FALSE;
785
786         if($width > $max && $height > $max) {
787
788                 // very tall image (greater than 16:9)
789                 // constrain the width - let the height float.
790
791                 if((($height * 9) / 16) > $width) {
792                         $dest_width = $max;
793                         $dest_height = intval(( $height * $max ) / $width);
794                 } elseif($width > $height) {
795                         // else constrain both dimensions
796                         $dest_width = $max;
797                         $dest_height = intval(( $height * $max ) / $width);
798                 }  else {
799                         $dest_width = intval(( $width * $max ) / $height);
800                         $dest_height = $max;
801                 }
802         } else {
803                 if( $width > $max ) {
804                         $dest_width = $max;
805                         $dest_height = intval(( $height * $max ) / $width);
806                 }  else {
807                         if( $height > $max ) {
808
809                                 // very tall image (greater than 16:9)
810                                 // but width is OK - don't do anything
811
812                                 if((($height * 9) / 16) > $width) {
813                                         $dest_width = $width;
814                                         $dest_height = $height;
815                                 } else {
816                                         $dest_width = intval(( $width * $max ) / $height);
817                                         $dest_height = $max;
818                                 }
819                         } else {
820                                 $dest_width = $width;
821                                 $dest_height = $height;
822                         }
823                 }
824         }
825         return array("width" => $dest_width, "height" => $dest_height);
826 }