3 * @file include/Photo.php
4 * @brief This file contains the Photo class for image processing
9 require_once("include/photos.php");
16 * Put back gd stuff, not everybody have Imagick
26 * @brief supported mimetypes and corresponding file extensions
28 static function supportedTypes() {
29 if (class_exists('Imagick')) {
31 // Imagick::queryFormats won't help us a lot there...
32 // At least, not yet, other parts of friendica uses this array
34 'image/jpeg' => 'jpg',
40 $t['image/jpeg'] ='jpg';
41 if (imagetypes() & IMG_PNG) {
42 $t['image/png'] = 'png';
49 public function __construct($data, $type=null) {
50 $this->imagick = class_exists('Imagick');
51 $this->types = $this->supportedTypes();
52 if (!array_key_exists($type, $this->types)){
57 if ($this->is_imagick() && $this->load_data($data)) {
60 // Failed to load with Imagick, fallback
61 $this->imagick = false;
63 return $this->load_data($data);
66 public function __destruct() {
68 if ($this->is_imagick()) {
69 $this->image->clear();
70 $this->image->destroy();
73 if (is_resource($this->image)) {
74 imagedestroy($this->image);
79 public function is_imagick() {
80 return $this->imagick;
84 * @brief Maps Mime types to Imagick formats
85 * @return arr With with image formats (mime type as key)
87 public function get_FormatsMap() {
89 'image/jpeg' => 'JPG',
96 private function load_data($data) {
97 if ($this->is_imagick()) {
98 $this->image = new Imagick();
100 $this->image->readImageBlob($data);
101 } catch (Exception $e) {
102 // Imagick couldn't use the data
107 * Setup the image to the format it will be saved to
109 $map = $this->get_FormatsMap();
110 $format = $map[$type];
111 $this->image->setFormat($format);
113 // Always coalesce, if it is not a multi-frame image it won't hurt anyway
114 $this->image = $this->image->coalesceImages();
117 * setup the compression here, so we'll do it only once
119 switch($this->getType()){
121 $quality = get_config('system', 'png_quality');
122 if ((! $quality) || ($quality > 9)) {
123 $quality = PNG_QUALITY;
126 * From http://www.imagemagick.org/script/command-line-options.php#quality:
128 * 'For the MNG and PNG image formats, the quality value sets
129 * the zlib compression level (quality / 10) and filter-type (quality % 10).
130 * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
131 * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
133 $quality = $quality * 10;
134 $this->image->setCompressionQuality($quality);
137 $quality = get_config('system', 'jpeg_quality');
138 if ((! $quality) || ($quality > 100)) {
139 $quality = JPEG_QUALITY;
141 $this->image->setCompressionQuality($quality);
144 // The 'width' and 'height' properties are only used by non-Imagick routines.
145 $this->width = $this->image->getImageWidth();
146 $this->height = $this->image->getImageHeight();
152 $this->valid = false;
153 $this->image = @imagecreatefromstring($data);
154 if ($this->image !== false) {
155 $this->width = imagesx($this->image);
156 $this->height = imagesy($this->image);
158 imagealphablending($this->image, false);
159 imagesavealpha($this->image, true);
167 public function is_valid() {
168 if ($this->is_imagick()) {
169 return ($this->image !== false);
174 public function getWidth() {
175 if (!$this->is_valid()) {
179 if ($this->is_imagick()) {
180 return $this->image->getImageWidth();
185 public function getHeight() {
186 if (!$this->is_valid()) {
190 if ($this->is_imagick()) {
191 return $this->image->getImageHeight();
193 return $this->height;
196 public function getImage() {
197 if (!$this->is_valid()) {
201 if ($this->is_imagick()) {
203 $this->image = $this->image->deconstructImages();
209 public function getType() {
210 if (!$this->is_valid()) {
217 public function getExt() {
218 if (!$this->is_valid()) {
222 return $this->types[$this->getType()];
225 public function scaleImage($max) {
226 if (!$this->is_valid()) {
230 $width = $this->getWidth();
231 $height = $this->getHeight();
233 $dest_width = $dest_height = 0;
235 if ((! $width)|| (! $height)) {
239 if ($width > $max && $height > $max) {
241 // very tall image (greater than 16:9)
242 // constrain the width - let the height float.
244 if ((($height * 9) / 16) > $width) {
246 $dest_height = intval(($height * $max) / $width);
247 } elseif ($width > $height) {
248 // else constrain both dimensions
250 $dest_height = intval(($height * $max) / $width);
252 $dest_width = intval(($width * $max) / $height);
258 $dest_height = intval(($height * $max) / $width);
260 if ($height > $max) {
262 // very tall image (greater than 16:9)
263 // but width is OK - don't do anything
265 if ((($height * 9) / 16) > $width) {
266 $dest_width = $width;
267 $dest_height = $height;
269 $dest_width = intval(($width * $max) / $height);
273 $dest_width = $width;
274 $dest_height = $height;
280 if ($this->is_imagick()) {
282 * If it is not animated, there will be only one iteration here,
283 * so don't bother checking
285 // Don't forget to go back to the first frame
286 $this->image->setFirstIterator();
289 // FIXME - implement horizantal bias for scaling as in followin GD functions
290 // to allow very tall images to be constrained only horizontally.
292 $this->image->scaleImage($dest_width, $dest_height);
293 } while ($this->image->nextImage());
295 // These may not be necessary any more
296 $this->width = $this->image->getImageWidth();
297 $this->height = $this->image->getImageHeight();
303 $dest = imagecreatetruecolor($dest_width, $dest_height);
304 imagealphablending($dest, false);
305 imagesavealpha($dest, true);
306 if ($this->type=='image/png') {
307 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
309 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
311 imagedestroy($this->image);
313 $this->image = $dest;
314 $this->width = imagesx($this->image);
315 $this->height = imagesy($this->image);
318 public function rotate($degrees) {
319 if (!$this->is_valid()) {
323 if ($this->is_imagick()) {
324 $this->image->setFirstIterator();
326 $this->image->rotateImage(new ImagickPixel(), -$degrees); // ImageMagick rotates in the opposite direction of imagerotate()
327 } while ($this->image->nextImage());
331 // if script dies at this point check memory_limit setting in php.ini
332 $this->image = imagerotate($this->image,$degrees,0);
333 $this->width = imagesx($this->image);
334 $this->height = imagesy($this->image);
337 public function flip($horiz = true, $vert = false) {
338 if (!$this->is_valid()) {
342 if ($this->is_imagick()) {
343 $this->image->setFirstIterator();
346 $this->image->flipImage();
349 $this->image->flopImage();
351 } while ($this->image->nextImage());
355 $w = imagesx($this->image);
356 $h = imagesy($this->image);
357 $flipped = imagecreate($w, $h);
359 for ($x = 0; $x < $w; $x++) {
360 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
364 for ($y = 0; $y < $h; $y++) {
365 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
368 $this->image = $flipped;
371 public function orient($filename) {
372 if ($this->is_imagick()) {
373 // based off comment on http://php.net/manual/en/imagick.getimageorientation.php
374 $orientation = $this->image->getImageOrientation();
375 switch ($orientation) {
376 case imagick::ORIENTATION_BOTTOMRIGHT:
377 $this->image->rotateimage("#000", 180);
379 case imagick::ORIENTATION_RIGHTTOP:
380 $this->image->rotateimage("#000", 90);
382 case imagick::ORIENTATION_LEFTBOTTOM:
383 $this->image->rotateimage("#000", -90);
387 $this->image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
390 // based off comment on http://php.net/manual/en/function.imagerotate.php
392 if (!$this->is_valid()) {
396 if ((!function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg')) {
400 $exif = @exif_read_data($filename,null,true);
405 $ort = $exif['IFD0']['Orientation'];
412 case 2: // horizontal flip
416 case 3: // 180 rotate left
420 case 4: // vertical flip
421 $this->flip(false, true);
424 case 5: // vertical flip + 90 rotate right
425 $this->flip(false, true);
429 case 6: // 90 rotate right
433 case 7: // horizontal flip + 90 rotate right
438 case 8: // 90 rotate left
443 // logger('exif: ' . print_r($exif,true));
450 public function scaleImageUp($min) {
451 if (!$this->is_valid()) {
456 $width = $this->getWidth();
457 $height = $this->getHeight();
459 $dest_width = $dest_height = 0;
461 if ((!$width)|| (!$height)) {
465 if ($width < $min && $height < $min) {
466 if ($width > $height) {
468 $dest_height = intval(($height * $min) / $width);
470 $dest_width = intval(($width * $min) / $height);
476 $dest_height = intval(($height * $min) / $width);
478 if ($height < $min) {
479 $dest_width = intval(($width * $min) / $height);
482 $dest_width = $width;
483 $dest_height = $height;
488 if ($this->is_imagick()) {
489 return $this->scaleImage($dest_width, $dest_height);
492 $dest = imagecreatetruecolor($dest_width, $dest_height);
493 imagealphablending($dest, false);
494 imagesavealpha($dest, true);
495 if ($this->type=='image/png') {
496 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
498 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
500 imagedestroy($this->image);
502 $this->image = $dest;
503 $this->width = imagesx($this->image);
504 $this->height = imagesy($this->image);
509 public function scaleImageSquare($dim) {
510 if (!$this->is_valid()) {
514 if ($this->is_imagick()) {
515 $this->image->setFirstIterator();
517 $this->image->scaleImage($dim, $dim);
518 } while ($this->image->nextImage());
522 $dest = imagecreatetruecolor($dim, $dim);
523 imagealphablending($dest, false);
524 imagesavealpha($dest, true);
525 if ($this->type=='image/png') {
526 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
528 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
530 imagedestroy($this->image);
532 $this->image = $dest;
533 $this->width = imagesx($this->image);
534 $this->height = imagesy($this->image);
538 public function cropImage($max, $x, $y, $w, $h) {
539 if (!$this->is_valid()) {
543 if ($this->is_imagick()) {
544 $this->image->setFirstIterator();
546 $this->image->cropImage($w, $h, $x, $y);
548 * We need to remove the canva,
549 * or the image is not resized to the crop:
550 * http://php.net/manual/en/imagick.cropimage.php#97232
552 $this->image->setImagePage(0, 0, 0, 0);
553 } while ($this->image->nextImage());
554 return $this->scaleImage($max);
557 $dest = imagecreatetruecolor($max, $max);
558 imagealphablending($dest, false);
559 imagesavealpha($dest, true);
560 if ($this->type=='image/png') {
561 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
563 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
565 imagedestroy($this->image);
567 $this->image = $dest;
568 $this->width = imagesx($this->image);
569 $this->height = imagesy($this->image);
572 public function saveImage($path) {
573 if (!$this->is_valid()) {
577 $string = $this->imageString();
581 $stamp1 = microtime(true);
582 file_put_contents($path, $string);
583 $a->save_timestamp($stamp1, "file");
586 public function imageString() {
587 if (!$this->is_valid()) {
591 if ($this->is_imagick()) {
593 $this->image = $this->image->deconstructImages();
594 $string = $this->image->getImagesBlob();
602 // Enable interlacing
603 imageinterlace($this->image, true);
605 switch($this->getType()){
607 $quality = get_config('system', 'png_quality');
608 if ((!$quality) || ($quality > 9)) {
609 $quality = PNG_QUALITY;
611 imagepng($this->image, null, $quality);
614 $quality = get_config('system', 'jpeg_quality');
615 if ((!$quality) || ($quality > 100)) {
616 $quality = JPEG_QUALITY;
618 imagejpeg($this->image, null, $quality);
620 $string = ob_get_contents();
628 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '') {
630 $r = q("SELECT `guid` FROM `photo` WHERE `resource-id` = '%s' AND `guid` != '' LIMIT 1",
633 if (dbm::is_result($r)) {
634 $guid = $r[0]['guid'];
639 $x = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' AND `uid` = %d AND `contact-id` = %d AND `scale` = %d LIMIT 1",
645 if (dbm::is_result($x)) {
646 $r = q("UPDATE `photo`
650 `resource-id` = '%s',
673 dbesc(datetime_convert()),
674 dbesc(datetime_convert()),
675 dbesc(basename($filename)),
676 dbesc($this->getType()),
678 intval($this->getHeight()),
679 intval($this->getWidth()),
680 dbesc(strlen($this->imageString())),
681 dbesc($this->imageString()),
692 $r = q("INSERT INTO `photo`
693 (`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`, `desc`)
694 VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, '%s', %d, %d, '%s', '%s', '%s', '%s', '%s')",
699 dbesc(datetime_convert()),
700 dbesc(datetime_convert()),
701 dbesc(basename($filename)),
702 dbesc($this->getType()),
704 intval($this->getHeight()),
705 intval($this->getWidth()),
706 dbesc(strlen($this->imageString())),
707 dbesc($this->imageString()),
724 * Guess image mimetype from filename or from Content-Type header
726 * @arg $filename string Image filename
727 * @arg $fromcurl boolean Check Content-Type header from curl request
729 function guess_image_type($filename, $fromcurl=false) {
730 logger('Photo: guess_image_type: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
735 $h = explode("\n",$a->get_curl_headers());
737 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
740 if (array_key_exists('Content-Type', $headers))
741 $type = $headers['Content-Type'];
744 // Guessing from extension? Isn't that... dangerous?
745 if (class_exists('Imagick') && file_exists($filename) && is_readable($filename)) {
747 * Well, this not much better,
748 * but at least it comes from the data inside the image,
749 * we won't be tricked by a manipulated extension
751 $image = new Imagick($filename);
752 $type = $image->getImageMimeType();
753 $image->setInterlaceScheme(Imagick::INTERLACE_PLANE);
755 $ext = pathinfo($filename, PATHINFO_EXTENSION);
756 $types = Photo::supportedTypes();
757 $type = "image/jpeg";
758 foreach ($types as $m => $e){
765 logger('Photo: guess_image_type: type='.$type, LOGGER_DEBUG);
771 * @brief Updates the avatar links in a contact only if needed
773 * @param string $avatar Link to avatar picture
774 * @param int $uid User id of contact owner
775 * @param int $cid Contact id
776 * @param bool $force force picture update
778 * @return array Returns array of the different avatar sizes
780 function update_contact_avatar($avatar, $uid, $cid, $force = false) {
781 $r = q("SELECT `avatar`, `photo`, `thumb`, `micro`, `nurl` FROM `contact` WHERE `id` = %d LIMIT 1", intval($cid));
782 if (!dbm::is_result($r)) {
785 $data = array($r[0]["photo"], $r[0]["thumb"], $r[0]["micro"]);
788 if (($r[0]["avatar"] != $avatar) OR $force) {
789 $photos = import_profile_photo($avatar, $uid, $cid, true);
792 q("UPDATE `contact` SET `avatar` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s', `avatar-date` = '%s' WHERE `id` = %d",
793 dbesc($avatar), dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]),
794 dbesc(datetime_convert()), intval($cid));
796 // Update the public contact (contact id = 0)
798 $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1));
799 if (dbm::is_result($pcontact)) {
800 update_contact_avatar($avatar, 0, $pcontact['id'], $force);
811 function import_profile_photo($photo, $uid, $cid, $quit_on_error = false) {
813 $r = q("SELECT `resource-id` FROM `photo` WHERE `uid` = %d AND `contact-id` = %d AND `scale` = 4 AND `album` = 'Contact Photos' LIMIT 1",
817 if (dbm::is_result($r) && strlen($r[0]['resource-id'])) {
818 $hash = $r[0]['resource-id'];
820 $hash = photo_new_resource();
823 $photo_failure = false;
825 $filename = basename($photo);
826 $img_str = fetch_url($photo, true);
828 if ($quit_on_error AND ($img_str == "")) {
832 $type = guess_image_type($photo, true);
833 $img = new Photo($img_str, $type);
834 if ($img->is_valid()) {
836 $img->scaleImageSquare(175);
838 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4);
841 $photo_failure = true;
843 $img->scaleImage(80);
845 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5);
848 $photo_failure = true;
850 $img->scaleImage(48);
852 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6);
855 $photo_failure = true;
858 $suffix = '?ts='.time();
860 $photo = App::get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt() . $suffix;
861 $thumb = App::get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt() . $suffix;
862 $micro = App::get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt() . $suffix;
864 // Remove the cached photo
866 $basepath = $a->get_basepath();
868 if (is_dir($basepath."/photo")) {
869 $filename = $basepath.'/photo/'.$hash.'-4.'.$img->getExt();
870 if (file_exists($filename)) {
873 $filename = $basepath.'/photo/'.$hash.'-5.'.$img->getExt();
874 if (file_exists($filename)) {
877 $filename = $basepath.'/photo/'.$hash.'-6.'.$img->getExt();
878 if (file_exists($filename)) {
883 $photo_failure = true;
886 if ($photo_failure AND $quit_on_error) {
890 if ($photo_failure) {
891 $photo = App::get_baseurl() . '/images/person-175.jpg';
892 $thumb = App::get_baseurl() . '/images/person-80.jpg';
893 $micro = App::get_baseurl() . '/images/person-48.jpg';
896 return(array($photo,$thumb,$micro));
900 function get_photo_info($url) {
903 $data = Cache::get($url);
905 if (is_null($data) OR !$data OR !is_array($data)) {
906 $img_str = fetch_url($url, true, $redirects, 4);
907 $filesize = strlen($img_str);
909 if (function_exists("getimagesizefromstring")) {
910 $data = getimagesizefromstring($img_str);
912 $tempfile = tempnam(get_temppath(), "cache");
915 $stamp1 = microtime(true);
916 file_put_contents($tempfile, $img_str);
917 $a->save_timestamp($stamp1, "file");
919 $data = getimagesize($tempfile);
924 $data["size"] = $filesize;
927 Cache::set($url, $data);
933 function scale_image($width, $height, $max) {
935 $dest_width = $dest_height = 0;
937 if ((!$width) || (!$height)) {
941 if ($width > $max && $height > $max) {
943 // very tall image (greater than 16:9)
944 // constrain the width - let the height float.
946 if ((($height * 9) / 16) > $width) {
948 $dest_height = intval(($height * $max) / $width);
949 } elseif ($width > $height) {
950 // else constrain both dimensions
952 $dest_height = intval(($height * $max) / $width);
954 $dest_width = intval(($width * $max) / $height);
960 $dest_height = intval(($height * $max) / $width);
962 if ($height > $max) {
964 // very tall image (greater than 16:9)
965 // but width is OK - don't do anything
967 if ((($height * 9) / 16) > $width) {
968 $dest_width = $width;
969 $dest_height = $height;
971 $dest_width = intval(($width * $max) / $height);
975 $dest_width = $width;
976 $dest_height = $height;
980 return array("width" => $dest_width, "height" => $dest_height);
983 function store_photo(App $a, $uid, $imagedata = "", $url = "") {
984 $r = q("SELECT `user`.`nickname`, `user`.`page-flags`, `contact`.`id` FROM `user` INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
985 WHERE `user`.`uid` = %d AND `user`.`blocked` = 0 AND `contact`.`self` = 1 LIMIT 1",
988 if (!dbm::is_result($r)) {
989 logger("Can't detect user data for uid ".$uid, LOGGER_DEBUG);
993 $page_owner_nick = $r[0]['nickname'];
996 /// $default_cid = $r[0]['id'];
997 /// $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
999 if ((strlen($imagedata) == 0) AND ($url == "")) {
1000 logger("No image data and no url provided", LOGGER_DEBUG);
1002 } elseif (strlen($imagedata) == 0) {
1003 logger("Uploading picture from ".$url, LOGGER_DEBUG);
1005 $stamp1 = microtime(true);
1006 $imagedata = @file_get_contents($url);
1007 $a->save_timestamp($stamp1, "file");
1010 $maximagesize = get_config('system', 'maximagesize');
1012 if (($maximagesize) && (strlen($imagedata) > $maximagesize)) {
1013 logger("Image exceeds size limit of ".$maximagesize, LOGGER_DEBUG);
1018 $r = q("select sum(octet_length(data)) as total from photo where uid = %d and scale = 0 and album != 'Contact Photos' ",
1022 $limit = service_class_fetch($uid,'photo_upload_limit');
1024 if (($limit !== false) && (($r[0]['total'] + strlen($imagedata)) > $limit)) {
1025 logger("Image exceeds personal limit of uid ".$uid, LOGGER_DEBUG);
1030 $tempfile = tempnam(get_temppath(), "cache");
1032 $stamp1 = microtime(true);
1033 file_put_contents($tempfile, $imagedata);
1034 $a->save_timestamp($stamp1, "file");
1036 $data = getimagesize($tempfile);
1038 if (!isset($data["mime"])) {
1040 logger("File is no picture", LOGGER_DEBUG);
1044 $ph = new Photo($imagedata, $data["mime"]);
1046 if (!$ph->is_valid()) {
1048 logger("Picture is no valid picture", LOGGER_DEBUG);
1052 $ph->orient($tempfile);
1055 $max_length = get_config('system', 'max_image_length');
1056 if (! $max_length) {
1057 $max_length = MAX_IMAGE_LENGTH;
1059 if ($max_length > 0) {
1060 $ph->scaleImage($max_length);
1063 $width = $ph->getWidth();
1064 $height = $ph->getHeight();
1066 $hash = photo_new_resource();
1070 // Pictures are always public by now
1071 //$defperm = '<'.$default_cid.'>';
1075 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 0, 0, $defperm);
1078 logger("Picture couldn't be stored", LOGGER_DEBUG);
1082 $image = array("page" => App::get_baseurl().'/photos/'.$page_owner_nick.'/image/'.$hash,
1083 "full" => App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt());
1085 if ($width > 800 || $height > 800) {
1086 $image["large"] = App::get_baseurl()."/photo/{$hash}-0.".$ph->getExt();
1089 if ($width > 640 || $height > 640) {
1090 $ph->scaleImage(640);
1091 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 1, 0, $defperm);
1093 $image["medium"] = App::get_baseurl()."/photo/{$hash}-1.".$ph->getExt();
1097 if ($width > 320 || $height > 320) {
1098 $ph->scaleImage(320);
1099 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 2, 0, $defperm);
1101 $image["small"] = App::get_baseurl()."/photo/{$hash}-2.".$ph->getExt();
1105 if ($width > 160 AND $height > 160) {
1109 $min = $ph->getWidth();
1111 $x = ($min - 160) / 2;
1114 if ($ph->getHeight() < $min) {
1115 $min = $ph->getHeight();
1117 $y = ($min - 160) / 2;
1122 $ph->cropImage(160, $x, $y, $min, $min);
1124 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 3, 0, $defperm);
1126 $image["thumb"] = App::get_baseurl()."/photo/{$hash}-3.".$ph->getExt();
1130 // Set the full image as preview image. This will be overwritten, if the picture is larger than 640.
1131 $image["preview"] = $image["full"];
1133 // Deactivated, since that would result in a cropped preview, if the picture wasn't larger than 320
1134 //if (isset($image["thumb"]))
1135 // $image["preview"] = $image["thumb"];
1137 // Unsure, if this should be activated or deactivated
1138 //if (isset($image["small"]))
1139 // $image["preview"] = $image["small"];
1141 if (isset($image["medium"])) {
1142 $image["preview"] = $image["medium"];