+
+function get_photo_info($url) {
+ $data = array();
+
+ $data = Cache::get($url);
+
+ if (is_null($data)) {
+ $img_str = fetch_url($url, true, $redirects, 4);
+
+ $tempfile = tempnam(get_temppath(), "cache");
+ file_put_contents($tempfile, $img_str);
+ $data = getimagesize($tempfile);
+ unlink($tempfile);
+
+ 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;
+ }
+ }
+ }
+ return array("width" => $dest_width, "height" => $dest_height);
+}