+ if($r === false)
+ $photo_failure = true;
+
+ $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.' . $img->getExt();
+ $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.' . $img->getExt();
+ $micro = $a->get_baseurl() . '/photo/' . $hash . '-6.' . $img->getExt();
+ } else
+ $photo_failure = true;
+
+ if($photo_failure AND $quit_on_error)
+ return false;
+
+ if($photo_failure) {
+ $photo = $a->get_baseurl() . '/images/person-175.jpg';
+ $thumb = $a->get_baseurl() . '/images/person-80.jpg';
+ $micro = $a->get_baseurl() . '/images/person-48.jpg';
+ }
+
+ return(array($photo,$thumb,$micro));
+
+}
+
+function get_photo_info($url) {
+ $data = array();
+
+ $data = Cache::get($url);
+
+ // Unserialise to be able to check in the next step if the cached data is alright.
+ if (!is_null($data))
+ $data = unserialize($data);
+
+ if (is_null($data) OR !$data) {
+ $img_str = fetch_url($url, true, $redirects, 4);
+ $filesize = strlen($img_str);
+
+ if (function_exists("getimagesizefromstring"))
+ $data = getimagesizefromstring($img_str);
+ else {
+ $tempfile = tempnam(get_temppath(), "cache");
+
+ $a = get_app();
+ $stamp1 = microtime(true);
+ file_put_contents($tempfile, $img_str);
+ $a->save_timestamp($stamp1, "file");
+
+ $data = getimagesize($tempfile);
+ unlink($tempfile);
+ }
+
+ if ($data)
+ $data["size"] = $filesize;
+
+ Cache::set($url, serialize($data));
+ }
+
+ return $data;
+}
+
+function scale_image($width, $height, $max) {
+
+ $dest_width = $dest_height = 0;
+
+ if((!$width) || (!$height))
+ return FALSE;
+
+ if($width > $max && $height > $max) {
+
+ // very tall image (greater than 16:9)
+ // constrain the width - let the height float.
+
+ if((($height * 9) / 16) > $width) {
+ $dest_width = $max;
+ $dest_height = intval(( $height * $max ) / $width);
+ } elseif($width > $height) {
+ // else constrain both dimensions
+ $dest_width = $max;
+ $dest_height = intval(( $height * $max ) / $width);
+ } else {
+ $dest_width = intval(( $width * $max ) / $height);
+ $dest_height = $max;
+ }
+ } else {
+ if( $width > $max ) {
+ $dest_width = $max;
+ $dest_height = intval(( $height * $max ) / $width);
+ } else {
+ if( $height > $max ) {
+
+ // very tall image (greater than 16:9)
+ // but width is OK - don't do anything
+
+ if((($height * 9) / 16) > $width) {
+ $dest_width = $width;
+ $dest_height = $height;
+ } else {
+ $dest_width = intval(( $width * $max ) / $height);
+ $dest_height = $max;
+ }
+ } else {
+ $dest_width = $width;
+ $dest_height = $height;
+ }
+ }
+ }
+ return array("width" => $dest_width, "height" => $dest_height);
+}
+
+function store_photo($a, $uid, $imagedata = "", $url = "") {
+ $r = q("SELECT `user`.`nickname`, `user`.`page-flags`, `contact`.`id` FROM `user` INNER JOIN `contact` on `user`.`uid` = `contact`.`uid`
+ WHERE `user`.`uid` = %d AND `user`.`blocked` = 0 and `contact`.`self` = 1 LIMIT 1",
+ intval($uid));
+
+ if(!count($r)) {
+ logger("Can't detect user data for uid ".$uid, LOGGER_DEBUG);
+ return(array());
+ }
+
+ $page_owner_nick = $r[0]['nickname'];
+
+ /// @TODO
+ /// $default_cid = $r[0]['id'];
+ /// $community_page = (($r[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
+
+ if ((strlen($imagedata) == 0) AND ($url == "")) {
+ logger("No image data and no url provided", LOGGER_DEBUG);
+ return(array());
+ } elseif (strlen($imagedata) == 0) {
+ logger("Uploading picture from ".$url, LOGGER_DEBUG);
+
+ $stamp1 = microtime(true);
+ $imagedata = @file_get_contents($url);
+ $a->save_timestamp($stamp1, "file");
+ }
+
+ $maximagesize = get_config('system','maximagesize');
+
+ if(($maximagesize) && (strlen($imagedata) > $maximagesize)) {
+ logger("Image exceeds size limit of ".$maximagesize, LOGGER_DEBUG);
+ return(array());
+ }