+ $r = q("select `resource-id` from photo where `uid` = %d and `contact-id` = %d and `scale` = 4 and `album` = 'Contact Photos' limit 1",
+ intval($uid),
+ intval($cid)
+ );
+ if(count($r) && strlen($r[0]['resource-id'])) {
+ $hash = $r[0]['resource-id'];
+ }
+ else {
+ $hash = photo_new_resource();
+ }
+
+ $photo_failure = false;
+
+ $filename = basename($photo);
+ $img_str = fetch_url($photo,true);
+
+ $type = guess_image_type($photo,true);
+ $img = new Photo($img_str, $type);
+ if($img->is_valid()) {
+
+ $img->scaleImageSquare(175);
+
+ $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 4 );
+
+ if($r === false)
+ $photo_failure = true;
+
+ $img->scaleImage(80);
+
+ $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 5 );
+
+ if($r === false)
+ $photo_failure = true;
+
+ $img->scaleImage(48);
+
+ $r = $img->store($uid, $cid, $hash, $filename, 'Contact Photos', 6 );
+
+ 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) {
+ $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);
+
+ if (is_null($data)) {
+ $img_str = fetch_url($url, true, $redirects, 4);
+
+ $filesize = strlen($img_str);
+
+ $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));
+ } else
+ $data = unserialize($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;
+ }
+ }