3 * @file src/Object/Photo.php
4 * @brief This file contains the Photo class for image processing
6 namespace Friendica\Object;
9 use Friendica\Core\Cache;
10 use Friendica\Core\Config;
11 use Friendica\Core\System;
12 use Friendica\Database\DBM;
14 require_once "include/photos.php";
17 * Class to handle Photos
24 * Put back gd stuff, not everybody have Imagick
34 * @brief supported mimetypes and corresponding file extensions
36 public static function supportedTypes()
38 if (class_exists('Imagick')) {
39 // Imagick::queryFormats won't help us a lot there...
40 // At least, not yet, other parts of friendica uses this array
42 'image/jpeg' => 'jpg',
48 $t['image/jpeg'] ='jpg';
49 if (imagetypes() & IMG_PNG) {
50 $t['image/png'] = 'png';
59 * @param object $data data
60 * @param boolean $type optional, default null
63 public function __construct($data, $type = null)
65 $this->imagick = class_exists('Imagick');
66 $this->types = static::supportedTypes();
67 if (!array_key_exists($type, $this->types)) {
72 if ($this->isImagick() && $this->loadData($data)) {
75 // Failed to load with Imagick, fallback
76 $this->imagick = false;
78 return $this->loadData($data);
85 public function __destruct()
88 if ($this->isImagick()) {
89 $this->image->clear();
90 $this->image->destroy();
93 if (is_resource($this->image)) {
94 imagedestroy($this->image);
102 public function isImagick()
104 return $this->imagick;
108 * @brief Maps Mime types to Imagick formats
109 * @return arr With with image formats (mime type as key)
111 public function getFormatsMap()
114 'image/jpeg' => 'JPG',
115 'image/png' => 'PNG',
122 * @param object $data data
125 private function loadData($data)
127 if ($this->isImagick()) {
128 $this->image = new Imagick();
130 $this->image->readImageBlob($data);
131 } catch (Exception $e) {
132 // Imagick couldn't use the data
137 * Setup the image to the format it will be saved to
139 $map = $this->getFormatsMap();
140 $format = $map[$type];
141 $this->image->setFormat($format);
143 // Always coalesce, if it is not a multi-frame image it won't hurt anyway
144 $this->image = $this->image->coalesceImages();
147 * setup the compression here, so we'll do it only once
149 switch ($this->getType()) {
151 $quality = Config::get('system', 'png_quality');
152 if ((! $quality) || ($quality > 9)) {
153 $quality = PNG_QUALITY;
156 * From http://www.imagemagick.org/script/command-line-options.php#quality:
158 * 'For the MNG and PNG image formats, the quality value sets
159 * the zlib compression level (quality / 10) and filter-type (quality % 10).
160 * The default PNG "quality" is 75, which means compression level 7 with adaptive PNG filtering,
161 * unless the image has a color map, in which case it means compression level 7 with no PNG filtering'
163 $quality = $quality * 10;
164 $this->image->setCompressionQuality($quality);
167 $quality = Config::get('system', 'jpeg_quality');
168 if ((! $quality) || ($quality > 100)) {
169 $quality = JPEG_QUALITY;
171 $this->image->setCompressionQuality($quality);
174 // The 'width' and 'height' properties are only used by non-Imagick routines.
175 $this->width = $this->image->getImageWidth();
176 $this->height = $this->image->getImageHeight();
182 $this->valid = false;
183 $this->image = @imagecreatefromstring($data);
184 if ($this->image !== false) {
185 $this->width = imagesx($this->image);
186 $this->height = imagesy($this->image);
188 imagealphablending($this->image, false);
189 imagesavealpha($this->image, true);
200 public function isValid()
202 if ($this->isImagick()) {
203 return ($this->image !== false);
211 public function getWidth()
213 if (!$this->isValid()) {
217 if ($this->isImagick()) {
218 return $this->image->getImageWidth();
226 public function getHeight()
228 if (!$this->isValid()) {
232 if ($this->isImagick()) {
233 return $this->image->getImageHeight();
235 return $this->height;
241 public function getImage()
243 if (!$this->isValid()) {
247 if ($this->isImagick()) {
249 $this->image = $this->image->deconstructImages();
258 public function getType()
260 if (!$this->isValid()) {
270 public function getExt()
272 if (!$this->isValid()) {
276 return $this->types[$this->getType()];
280 * @param integer $max max dimension
283 public function scaleImage($max)
285 if (!$this->isValid()) {
289 $width = $this->getWidth();
290 $height = $this->getHeight();
292 $dest_width = $dest_height = 0;
294 if ((! $width)|| (! $height)) {
298 if ($width > $max && $height > $max) {
299 // very tall image (greater than 16:9)
300 // constrain the width - let the height float.
302 if ((($height * 9) / 16) > $width) {
304 $dest_height = intval(($height * $max) / $width);
305 } elseif ($width > $height) {
306 // else constrain both dimensions
308 $dest_height = intval(($height * $max) / $width);
310 $dest_width = intval(($width * $max) / $height);
316 $dest_height = intval(($height * $max) / $width);
318 if ($height > $max) {
319 // very tall image (greater than 16:9)
320 // but width is OK - don't do anything
322 if ((($height * 9) / 16) > $width) {
323 $dest_width = $width;
324 $dest_height = $height;
326 $dest_width = intval(($width * $max) / $height);
330 $dest_width = $width;
331 $dest_height = $height;
337 if ($this->isImagick()) {
339 * If it is not animated, there will be only one iteration here,
340 * so don't bother checking
342 // Don't forget to go back to the first frame
343 $this->image->setFirstIterator();
345 // FIXME - implement horizantal bias for scaling as in followin GD functions
346 // to allow very tall images to be constrained only horizontally.
348 $this->image->scaleImage($dest_width, $dest_height);
349 } while ($this->image->nextImage());
351 // These may not be necessary any more
352 $this->width = $this->image->getImageWidth();
353 $this->height = $this->image->getImageHeight();
359 $dest = imagecreatetruecolor($dest_width, $dest_height);
360 imagealphablending($dest, false);
361 imagesavealpha($dest, true);
362 if ($this->type=='image/png') {
363 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
365 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
367 imagedestroy($this->image);
369 $this->image = $dest;
370 $this->width = imagesx($this->image);
371 $this->height = imagesy($this->image);
375 * @param integer $degrees degrees to rotate image
378 public function rotate($degrees)
380 if (!$this->isValid()) {
384 if ($this->isImagick()) {
385 $this->image->setFirstIterator();
387 $this->image->rotateImage(new ImagickPixel(), -$degrees); // ImageMagick rotates in the opposite direction of imagerotate()
388 } while ($this->image->nextImage());
392 // if script dies at this point check memory_limit setting in php.ini
393 $this->image = imagerotate($this->image, $degrees, 0);
394 $this->width = imagesx($this->image);
395 $this->height = imagesy($this->image);
399 * @param boolean $horiz optional, default true
400 * @param boolean $vert optional, default false
403 public function flip($horiz = true, $vert = false)
405 if (!$this->isValid()) {
409 if ($this->isImagick()) {
410 $this->image->setFirstIterator();
413 $this->image->flipImage();
416 $this->image->flopImage();
418 } while ($this->image->nextImage());
422 $w = imagesx($this->image);
423 $h = imagesy($this->image);
424 $flipped = imagecreate($w, $h);
426 for ($x = 0; $x < $w; $x++) {
427 imagecopy($flipped, $this->image, $x, 0, $w - $x - 1, 0, 1, $h);
431 for ($y = 0; $y < $h; $y++) {
432 imagecopy($flipped, $this->image, 0, $y, 0, $h - $y - 1, $w, 1);
435 $this->image = $flipped;
439 * @param string $filename filename
442 public function orient($filename)
444 if ($this->isImagick()) {
445 // based off comment on http://php.net/manual/en/imagick.getimageorientation.php
446 $orientation = $this->image->getImageOrientation();
447 switch ($orientation) {
448 case imagick::ORIENTATION_BOTTOMRIGHT:
449 $this->image->rotateimage("#000", 180);
451 case imagick::ORIENTATION_RIGHTTOP:
452 $this->image->rotateimage("#000", 90);
454 case imagick::ORIENTATION_LEFTBOTTOM:
455 $this->image->rotateimage("#000", -90);
459 $this->image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
462 // based off comment on http://php.net/manual/en/function.imagerotate.php
464 if (!$this->isValid()) {
468 if ((!function_exists('exif_read_data')) || ($this->getType() !== 'image/jpeg')) {
472 $exif = @exif_read_data($filename, null, true);
477 $ort = $exif['IFD0']['Orientation'];
483 case 2: // horizontal flip
487 case 3: // 180 rotate left
491 case 4: // vertical flip
492 $this->flip(false, true);
495 case 5: // vertical flip + 90 rotate right
496 $this->flip(false, true);
500 case 6: // 90 rotate right
504 case 7: // horizontal flip + 90 rotate right
509 case 8: // 90 rotate left
514 // logger('exif: ' . print_r($exif,true));
519 * @param integer $min minimum dimension
522 public function scaleImageUp($min)
524 if (!$this->isValid()) {
528 $width = $this->getWidth();
529 $height = $this->getHeight();
531 $dest_width = $dest_height = 0;
533 if ((!$width)|| (!$height)) {
537 if ($width < $min && $height < $min) {
538 if ($width > $height) {
540 $dest_height = intval(($height * $min) / $width);
542 $dest_width = intval(($width * $min) / $height);
548 $dest_height = intval(($height * $min) / $width);
550 if ($height < $min) {
551 $dest_width = intval(($width * $min) / $height);
554 $dest_width = $width;
555 $dest_height = $height;
560 if ($this->isImagick()) {
561 return $this->scaleImage($dest_width, $dest_height);
564 $dest = imagecreatetruecolor($dest_width, $dest_height);
565 imagealphablending($dest, false);
566 imagesavealpha($dest, true);
567 if ($this->type=='image/png') {
568 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
570 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dest_width, $dest_height, $width, $height);
572 imagedestroy($this->image);
574 $this->image = $dest;
575 $this->width = imagesx($this->image);
576 $this->height = imagesy($this->image);
580 * @param integer $dim dimension
583 public function scaleImageSquare($dim)
585 if (!$this->isValid()) {
589 if ($this->isImagick()) {
590 $this->image->setFirstIterator();
592 $this->image->scaleImage($dim, $dim);
593 } while ($this->image->nextImage());
597 $dest = imagecreatetruecolor($dim, $dim);
598 imagealphablending($dest, false);
599 imagesavealpha($dest, true);
600 if ($this->type=='image/png') {
601 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
603 imagecopyresampled($dest, $this->image, 0, 0, 0, 0, $dim, $dim, $this->width, $this->height);
605 imagedestroy($this->image);
607 $this->image = $dest;
608 $this->width = imagesx($this->image);
609 $this->height = imagesy($this->image);
613 * @param integer $max maximum
614 * @param integer $x x coordinate
615 * @param integer $y y coordinate
616 * @param integer $w width
617 * @param integer $h height
620 public function cropImage($max, $x, $y, $w, $h)
622 if (!$this->isValid()) {
626 if ($this->isImagick()) {
627 $this->image->setFirstIterator();
629 $this->image->cropImage($w, $h, $x, $y);
631 * We need to remove the canva,
632 * or the image is not resized to the crop:
633 * http://php.net/manual/en/imagick.cropimage.php#97232
635 $this->image->setImagePage(0, 0, 0, 0);
636 } while ($this->image->nextImage());
637 return $this->scaleImage($max);
640 $dest = imagecreatetruecolor($max, $max);
641 imagealphablending($dest, false);
642 imagesavealpha($dest, true);
643 if ($this->type=='image/png') {
644 imagefill($dest, 0, 0, imagecolorallocatealpha($dest, 0, 0, 0, 127)); // fill with alpha
646 imagecopyresampled($dest, $this->image, 0, 0, $x, $y, $max, $max, $w, $h);
648 imagedestroy($this->image);
650 $this->image = $dest;
651 $this->width = imagesx($this->image);
652 $this->height = imagesy($this->image);
656 * @param string $path file path
659 public function saveImage($path)
661 if (!$this->isValid()) {
665 $string = $this->imageString();
669 $stamp1 = microtime(true);
670 file_put_contents($path, $string);
671 $a->save_timestamp($stamp1, "file");
677 public function imageString()
679 if (!$this->isValid()) {
683 if ($this->isImagick()) {
685 $this->image = $this->image->deconstructImages();
686 $string = $this->image->getImagesBlob();
694 // Enable interlacing
695 imageinterlace($this->image, true);
697 switch ($this->getType()) {
699 $quality = Config::get('system', 'png_quality');
700 if ((!$quality) || ($quality > 9)) {
701 $quality = PNG_QUALITY;
703 imagepng($this->image, null, $quality);
706 $quality = Config::get('system', 'jpeg_quality');
707 if ((!$quality) || ($quality > 100)) {
708 $quality = JPEG_QUALITY;
710 imagejpeg($this->image, null, $quality);
712 $string = ob_get_contents();
719 * @param integer $uid uid
720 * @param integer $cid cid
721 * @param integer $rid rid
722 * @param string $filename filename
723 * @param string $album album name
724 * @param integer $scale scale
725 * @param integer $profile optional, default = 0
726 * @param string $allow_cid optional, default = ''
727 * @param string $allow_gid optional, default = ''
728 * @param string $deny_cid optional, default = ''
729 * @param string $deny_gid optional, default = ''
730 * @param string $desc optional, default = ''
733 public function store($uid, $cid, $rid, $filename, $album, $scale, $profile = 0, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '', $desc = '')
735 $r = dba::select('photo', array('guid'), array("`resource-id` = ? AND `guid` != ?", $rid, ''), array('limit' => 1));
736 if (DBM::is_result($r)) {
742 $x = dba::select('photo', array('id'), array('resource-id' => $rid, 'uid' => $uid, 'contact-id' => $cid, 'scale' => $scale), array('limit' => 1));
744 $fields = array('uid' => $uid, 'contact-id' => $cid, 'guid' => $guid, 'resource-id' => $rid, 'created' => datetime_convert(), 'edited' => datetime_convert(),
745 'filename' => basename($filename), 'type' => $this->getType(), 'album' => $album, 'height' => $this->getHeight(), 'width' => $this->getWidth(),
746 'datasize' => strlen($this->imageString()), 'data' => $this->imageString(), 'scale' => $scale, 'profile' => $profile,
747 'allow_cid' => $allow_cid, 'allow_gid' => $allow_gid, 'deny_cid' => $deny_cid, 'deny_gid' => $deny_gid, 'desc' => $desc);
749 if (DBM::is_result($x)) {
750 $r = dba::update('photo', $fields, array('id' => $x['id']));
752 $r = dba::insert('photo', $fields);
759 * Guess image mimetype from filename or from Content-Type header
761 * @param string $filename Image filename
762 * @param boolean $fromcurl Check Content-Type header from curl request
766 public function guessImageType($filename, $fromcurl = false)
768 logger('Photo: guessImageType: '.$filename . ($fromcurl?' from curl headers':''), LOGGER_DEBUG);
773 $h = explode("\n", $a->get_curl_headers());
775 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
778 if (array_key_exists('Content-Type', $headers))
779 $type = $headers['Content-Type'];
781 if (is_null($type)) {
782 // Guessing from extension? Isn't that... dangerous?
783 if (class_exists('Imagick') && file_exists($filename) && is_readable($filename)) {
785 * Well, this not much better,
786 * but at least it comes from the data inside the image,
787 * we won't be tricked by a manipulated extension
789 $image = new Imagick($filename);
790 $type = $image->getImageMimeType();
791 $image->setInterlaceScheme(Imagick::INTERLACE_PLANE);
793 $ext = pathinfo($filename, PATHINFO_EXTENSION);
794 $types = $this->supportedTypes();
795 $type = "image/jpeg";
796 foreach ($types as $m => $e) {
803 logger('Photo: guessImageType: type='.$type, LOGGER_DEBUG);
808 * @brief Updates the avatar links in a contact only if needed
810 * @param string $avatar Link to avatar picture
811 * @param int $uid User id of contact owner
812 * @param int $cid Contact id
813 * @param bool $force force picture update
815 * @return array Returns array of the different avatar sizes
817 public function updateContactAvatar($avatar, $uid, $cid, $force = false)
819 // Limit = 1 returns the row so no need for dba:inArray()
820 $r = dba::select('contact', array('avatar', 'photo', 'thumb', 'micro', 'nurl'), array('id' => $cid), array('limit' => 1));
821 if (!DBM::is_result($r)) {
824 $data = array($r["photo"], $r["thumb"], $r["micro"]);
827 if (($r["avatar"] != $avatar) || $force) {
828 $photos = $this->importProfilePhoto($avatar, $uid, $cid, true);
833 array('avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => datetime_convert()),
837 // Update the public contact (contact id = 0)
839 $pcontact = dba::select('contact', array('id'), array('nurl' => $r[0]['nurl']), array('limit' => 1));
840 if (DBM::is_result($pcontact)) {
841 $this->updateContactAvatar($avatar, 0, $pcontact['id'], $force);
853 * @param string $photo photo
854 * @param integer $uid user id
855 * @param integer $cid contact id
856 * @param boolean $quit_on_error optional, default false
859 private function importProfilePhoto($photo, $uid, $cid, $quit_on_error = false)
863 array('resource-id'),
864 array('uid' => $uid, 'contact-id' => $cid, 'scale' => 4, 'album' => 'Contact Photos'),
868 if (DBM::is_result($r) && strlen($r['resource-id'])) {
869 $hash = $r['resource-id'];
871 $hash = photo_new_resource();
874 $photo_failure = false;
876 $filename = basename($photo);
877 $img_str = fetch_url($photo, true);
879 if ($quit_on_error && ($img_str == "")) {
883 $type = $this->guessImageType($photo, true);
884 $img = new Photo($img_str, $type);
885 if ($img->isValid()) {
886 $img->scaleImageSquare(175);
888 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4);
891 $photo_failure = true;
894 $img->scaleImage(80);
896 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5);
899 $photo_failure = true;
902 $img->scaleImage(48);
904 $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6);
907 $photo_failure = true;
910 $suffix = '?ts='.time();
912 $photo = System::baseUrl() . '/photo/' . $hash . '-4.' . $img->getExt() . $suffix;
913 $thumb = System::baseUrl() . '/photo/' . $hash . '-5.' . $img->getExt() . $suffix;
914 $micro = System::baseUrl() . '/photo/' . $hash . '-6.' . $img->getExt() . $suffix;
916 // Remove the cached photo
918 $basepath = $a->get_basepath();
920 if (is_dir($basepath."/photo")) {
921 $filename = $basepath.'/photo/'.$hash.'-4.'.$img->getExt();
922 if (file_exists($filename)) {
925 $filename = $basepath.'/photo/'.$hash.'-5.'.$img->getExt();
926 if (file_exists($filename)) {
929 $filename = $basepath.'/photo/'.$hash.'-6.'.$img->getExt();
930 if (file_exists($filename)) {
935 $photo_failure = true;
938 if ($photo_failure && $quit_on_error) {
942 if ($photo_failure) {
943 $photo = System::baseUrl() . '/images/person-175.jpg';
944 $thumb = System::baseUrl() . '/images/person-80.jpg';
945 $micro = System::baseUrl() . '/images/person-48.jpg';
948 return(array($photo, $thumb, $micro));
952 * @param string $url url
955 public function getPhotoInfo($url)
959 $data = Cache::get($url);
961 if (is_null($data) || !$data || !is_array($data)) {
962 $img_str = fetch_url($url, true, $redirects, 4);
963 $filesize = strlen($img_str);
965 if (function_exists("getimagesizefromstring")) {
966 $data = getimagesizefromstring($img_str);
968 $tempfile = tempnam(get_temppath(), "cache");
971 $stamp1 = microtime(true);
972 file_put_contents($tempfile, $img_str);
973 $a->save_timestamp($stamp1, "file");
975 $data = getimagesize($tempfile);
980 $data["size"] = $filesize;
983 Cache::set($url, $data);
990 * @param integer $width width
991 * @param integer $height height
992 * @param integer $max max
995 public function scaleImageTo($width, $height, $max)
997 $dest_width = $dest_height = 0;
999 if ((!$width) || (!$height)) {
1003 if ($width > $max && $height > $max) {
1004 // very tall image (greater than 16:9)
1005 // constrain the width - let the height float.
1007 if ((($height * 9) / 16) > $width) {
1009 $dest_height = intval(($height * $max) / $width);
1010 } elseif ($width > $height) {
1011 // else constrain both dimensions
1013 $dest_height = intval(($height * $max) / $width);
1015 $dest_width = intval(($width * $max) / $height);
1016 $dest_height = $max;
1019 if ($width > $max) {
1021 $dest_height = intval(($height * $max) / $width);
1023 if ($height > $max) {
1024 // very tall image (greater than 16:9)
1025 // but width is OK - don't do anything
1027 if ((($height * 9) / 16) > $width) {
1028 $dest_width = $width;
1029 $dest_height = $height;
1031 $dest_width = intval(($width * $max) / $height);
1032 $dest_height = $max;
1035 $dest_width = $width;
1036 $dest_height = $height;
1040 return array("width" => $dest_width, "height" => $dest_height);
1044 * @brief This function doesn't seem to be used
1045 * @param object $a App
1046 * @param integer $uid user id
1047 * @param string $imagedata optional, default empty
1048 * @param string $url optional, default empty
1051 private function storePhoto(App $a, $uid, $imagedata = "", $url = "")
1054 "SELECT `user`.`nickname`, `user`.`page-flags`, `contact`.`id` FROM `user` INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
1055 WHERE `user`.`uid` = %d AND `user`.`blocked` = 0 AND `contact`.`self` = 1 LIMIT 1",
1059 if (!DBM::is_result($r)) {
1060 logger("Can't detect user data for uid ".$uid, LOGGER_DEBUG);
1064 $page_owner_nick = $r[0]['nickname'];
1067 /// $default_cid = $r[0]['id'];
1068 /// $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
1070 if ((strlen($imagedata) == 0) && ($url == "")) {
1071 logger("No image data and no url provided", LOGGER_DEBUG);
1073 } elseif (strlen($imagedata) == 0) {
1074 logger("Uploading picture from ".$url, LOGGER_DEBUG);
1076 $stamp1 = microtime(true);
1077 $imagedata = @file_get_contents($url);
1078 $a->save_timestamp($stamp1, "file");
1081 $maximagesize = Config::get('system', 'maximagesize');
1083 if (($maximagesize) && (strlen($imagedata) > $maximagesize)) {
1084 logger("Image exceeds size limit of ".$maximagesize, LOGGER_DEBUG);
1088 $tempfile = tempnam(get_temppath(), "cache");
1090 $stamp1 = microtime(true);
1091 file_put_contents($tempfile, $imagedata);
1092 $a->save_timestamp($stamp1, "file");
1094 $data = getimagesize($tempfile);
1096 if (!isset($data["mime"])) {
1098 logger("File is no picture", LOGGER_DEBUG);
1102 $ph = new Photo($imagedata, $data["mime"]);
1104 if (!$ph->isValid()) {
1106 logger("Picture is no valid picture", LOGGER_DEBUG);
1110 $ph->orient($tempfile);
1113 $max_length = Config::get('system', 'max_image_length');
1114 if (! $max_length) {
1115 $max_length = MAX_IMAGE_LENGTH;
1117 if ($max_length > 0) {
1118 $ph->scaleImage($max_length);
1121 $width = $ph->getWidth();
1122 $height = $ph->getHeight();
1124 $hash = photo_new_resource();
1128 // Pictures are always public by now
1129 //$defperm = '<'.$default_cid.'>';
1133 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 0, 0, $defperm);
1136 logger("Picture couldn't be stored", LOGGER_DEBUG);
1140 $image = array("page" => System::baseUrl().'/photos/'.$page_owner_nick.'/image/'.$hash,
1141 "full" => System::baseUrl()."/photo/{$hash}-0.".$ph->getExt());
1143 if ($width > 800 || $height > 800) {
1144 $image["large"] = System::baseUrl()."/photo/{$hash}-0.".$ph->getExt();
1147 if ($width > 640 || $height > 640) {
1148 $ph->scaleImage(640);
1149 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 1, 0, $defperm);
1151 $image["medium"] = System::baseUrl()."/photo/{$hash}-1.".$ph->getExt();
1155 if ($width > 320 || $height > 320) {
1156 $ph->scaleImage(320);
1157 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 2, 0, $defperm);
1159 $image["small"] = System::baseUrl()."/photo/{$hash}-2.".$ph->getExt();
1163 if ($width > 160 && $height > 160) {
1167 $min = $ph->getWidth();
1169 $x = ($min - 160) / 2;
1172 if ($ph->getHeight() < $min) {
1173 $min = $ph->getHeight();
1175 $y = ($min - 160) / 2;
1180 $ph->cropImage(160, $x, $y, $min, $min);
1182 $r = $ph->store($uid, $visitor, $hash, $tempfile, t('Wall Photos'), 3, 0, $defperm);
1184 $image["thumb"] = System::baseUrl()."/photo/{$hash}-3.".$ph->getExt();
1188 // Set the full image as preview image. This will be overwritten, if the picture is larger than 640.
1189 $image["preview"] = $image["full"];
1191 // Deactivated, since that would result in a cropped preview, if the picture wasn't larger than 320
1192 //if (isset($image["thumb"]))
1193 // $image["preview"] = $image["thumb"];
1195 // Unsure, if this should be activated or deactivated
1196 //if (isset($image["small"]))
1197 // $image["preview"] = $image["small"];
1199 if (isset($image["medium"])) {
1200 $image["preview"] = $image["medium"];