/** * Populate this object from an array * * @since Version 3.9.1 * @return void */ public function populateFromArray($row) { $this->provider = $row['provider']; $this->photo_id = $row['photo_id']; $this->Date = new DateTime($row['modified']); $this->DateCaptured = !isset($row['captured']) || is_null($row['captured']) ? null : new DateTime($row['captured']); $this->title = !empty($row['meta']['title']) ? ContentUtility::FormatTitle($row['meta']['title']) : "Untitled"; $this->description = $row['meta']['description']; $this->sizes = $row['meta']['sizes']; $this->links = $row['meta']['links']; $this->meta = $row['meta']['data']; $this->lat = $row['lat']; $this->lon = $row['lon']; #printArray($row['meta']);die; if (!$this->DateCaptured instanceof DateTime) { if (isset($row['meta']['data']['dates']['taken'])) { $this->DateCaptured = new DateTime($row['meta']['data']['dates']['taken']); } } /** * Normalize some sizes */ if (count($this->sizes)) { $this->sizes = Images::normaliseSizes($this->sizes); } $this->url = Utility\Url::CreateFromImageID($this->id); if (empty($this->mckey)) { $this->mckey = sprintf("railpage:image=%d", $this->id); } }
/** * Get thumbnails of images from an array of image IDs * @since Version 3.9.1 * @param array $ids * @return array */ public static function GetThumbnails($ids) { $Memcached = AppCore::getMemcached(); $Database = (new AppCore())->getDatabaseConnection(); if (is_array($ids)) { $ids = implode(",", $ids); } $cachekey = md5($ids) . ".v2"; if (!($return = $Memcached->fetch($cachekey))) { $query = "SELECT id, meta, title FROM image WHERE id IN (" . $ids . ")"; $return = array(); #echo $query; foreach ($Database->fetchAll($query) as $row) { $meta = json_decode($row['meta'], true); $meta['sizes'] = Images::normaliseSizes($meta['sizes']); $return[] = array("id" => $row['id'], "url" => sprintf("/photos/%d", $row['id']), "thumbnail" => $meta['sizes']['thumb']['source'], "html" => sprintf("<a href='%s' class='thumbnail' style='background-image:url(%s);'></a>", sprintf("/photos/%d", $row['id']), $meta['sizes']['thumb']['source'])); } $Memcached->save($cachekey, $return); } return $return; }
/** * Get the image from the provider * @since Version 3.9.1 * @param int $id The ID of the image from the provider * @return array */ public function getImage($id, $force = 0) { $params = array("ImageKey" => $id); $force = (bool) $force; $response = $this->send("smugmug.images.getInfo", $params); if ($response['stat'] == "ok") { // List of image sizes to look for $sizes = array("TinyURL", "ThumbURL", "SmallURL", "MediumURL", "LargeURL", "XLargeURL", "X2LargeURL", "X3LargeURL", "OriginalURL"); // List of image links to look for $links = array("URL", "LightboxURL"); /** * Start assembling the photo data */ $this->photo = array("provider" => self::PROVIDER_NAME, "id" => $id, "dates" => array("taken" => new DateTime($response['Image']['LastUpdated']), "updated" => new DateTime($response['Image']['LastUpdated']), "uploaded" => new DateTime($response['Image']['LastUpdated'])), "author" => array(), "title" => $response['Image']['Caption'], "description" => $response['Image']['FileName'], "tags" => $response['Image']['Keywords'], "sizes" => array(), "urls" => array()); /** * Grab all the image sizes */ foreach ($sizes as $size) { if (isset($response['Image'][$size]) && !empty($response['Image'][$size])) { $dimensions = getimagesize($response['Image'][$size]); ini_set("max_execution_time", 3200); $this->photo['sizes'][$size] = array("source" => $response['Image'][$size], "width" => $dimensions[0], "height" => $dimensions[1]); } } $this->photo['sizes'] = Images::normaliseSizes($this->photo['sizes']); /** * Grab all the image links */ foreach ($links as $link) { if (isset($response['Image'][$link]) && !empty($response['Image'][$link])) { $this->photo['urls'][$link] = $response['Image'][$link]; } } /** * Grab the image owner */ if (preg_match("#(http|https)://([a-zA-Z0-9\\-_]+).smugmug#", $response['Image']['URL'], $matches)) { $nickname = $matches[2]; if ($response = $this->send("smugmug.users.getInfo", array("NickName" => $nickname))) { if ($response['stat'] == "ok") { $this->photo['author'] = array("id" => $nickname, "username" => $response['User']['NickName'], "realname" => $response['User']['Name'], "url" => $response['User']['URL']); } } } return $this->photo; } }
/** * Find and set the hero image * @since Version 3.10.0 * @return \Railpage\Newsletters\Weekly */ private function getHeroImage() { $query = "SELECT f.image_id, i.meta FROM image_flags AS f LEFT JOIN image AS i ON f.image_id = i.id WHERE f.screened_pick = 1"; $ids = []; foreach ($this->db->fetchAll($query) as $row) { $row['meta'] = json_decode($row['meta'], true); $sizes = Images::normaliseSizes($row['meta']['sizes']); if ($sizes['medium']['height'] > $sizes['medium']['width']) { continue; } $ids[] = $row['image_id']; } $image_id = $ids[array_rand($ids)]; if (filter_var($image_id, FILTER_VALIDATE_INT)) { Debug::LogCLI("Creating instance of Image for the hero photo"); $this->HeroImage = ImageFactory::CreateImage($image_id); $this->Newsletter->setHeroImage($this->HeroImage); } return $this; }
/** * Get a random image as an array * @since Version 3.10.0 * @param string $namespace An optional linked namespace to filter by * @param int $namespaceKey An optional linked namespace key to filter by * @return array */ public static function randomImage($namespace, $namespaceKey) { $Database = (new AppCore())->getDatabaseConnection(); if (is_null($namespace) && !is_null($namespaceKey)) { throw new InvalidArgumentException("A namespace key was specified but an associated namespace value was not."); } if (is_null($namespace) && is_null($namespaceKey)) { $query = "SELECT * FROM image AS r1 JOIN (SELECT CEIL(RAND() * (SELECT MAX(id) FROM image)) AS randomid) AS r2 WHERE r1.id >= r2.randomid ORDER BY r1.id ASC LIMIT 1"; $row = $Database->fetchRow($query); $row['meta'] = json_decode($row['meta'], true); $row['sizes'] = Images::normaliseSizes($row['meta']['sizes']); $row['url'] = Url::CreateFromImageID($row['id']); $row['url'] = $row['url']->getURLs(); return $row; } if (!is_null($namespace)) { $query = "SELECT il.image_id FROM image_link AS il LEFT JOIN image AS i ON i.id = il.image_id WHERE il.namespace = ? AND i.provider IS NOT NULL"; $params = [$namespace]; if (!is_null($namespaceKey)) { $query .= " AND namespace_key = ?"; $params[] = $namespaceKey; } $ids = []; foreach ($Database->fetchAll($query, $params) as $row) { $ids[] = $row['image_id']; } $image_id = $ids[array_rand($ids)]; $query = "SELECT * FROM image WHERE id = ?"; $row = $Database->fetchRow($query, $image_id); $row['meta'] = json_decode($row['meta'], true); $row['sizes'] = Images::normaliseSizes($row['meta']['sizes']); $row['url'] = Url::CreateFromImageID($row['id']); $row['url'] = $row['url']->getURLs(); return $row; } return; }
/** * Get skipped images * @since Version 3.10.0 * @param int $page * @param int $itemsPerPage * @return array */ public function getSkippedImages($page = 1, $itemsPerPage = 25) { $query = "SELECT SQL_CALC_FOUND_ROWS i.*\r\n FROM image_flags_skip AS f\r\n LEFT JOIN image AS i ON f.image_id = i.id\r\n GROUP BY f.image_id\r\n ORDER BY f.date DESC\r\n LIMIT ?, ?"; $params = [($page - 1) * $itemsPerPage, $itemsPerPage]; $return = array("total" => 0, "page" => $page, "items_per_page" => $itemsPerPage, "photos" => array()); foreach ($this->db->fetchAll($query, $params) as $row) { $row['meta'] = json_decode($row['meta'], true); $row['meta']['sizes'] = Images::normaliseSizes($row['meta']['sizes']); $return['photos'][] = $row; } $return['total'] = $this->db->fetchOne("SELECT FOUND_ROWS() AS total"); return $return; }
/** * Get previous photos * @since Version 3.10.0 * @return array * @param int $page * @param int $itemsPerPage */ public function getPreviousPhotos($page = 1, $itemsPerPage = 25) { $Date = self::getStartOfWeek(new DateTime()); $query = "SELECT SQL_CALC_FOUND_ROWS i.*, u.username, iw.added_by AS user_id FROM image_weekly AS iw\r\n LEFT JOIN image AS i ON iw.image_id = i.id\r\n LEFT JOIN nuke_users AS u ON u.user_id = iw.added_by\r\n WHERE iw.datefrom < ? LIMIT ?, ?"; $params = [$Date->format("Y-m-d"), ($page - 1) * $itemsPerPage, $itemsPerPage]; $result = $this->db->fetchAll($query, $params); $return = ["total" => 0, "page" => $page, "items_per_page" => $itemsPerPage, "photos" => []]; $return['total'] = $this->db->fetchOne("SELECT FOUND_ROWS() AS total"); foreach ($result as $row) { $row['meta'] = json_decode($row['meta'], true); $row['meta']['sizes'] = Images::normaliseSizes($row['meta']['sizes']); $row['url'] = Utility\Url::CreateFromImageID($row['id'])->getURLs(); $return['photos'][] = $row; } return $return; }
/** * Get photos from the Sphinx search API within or adjacent to this place * @since Version 3.9.1 * @param int $num * @return array */ public function getPhotosFromSphinx($num = 10) { $Sphinx = AppCore::getSphinxAPI(); $Sphinx->SetGeoAnchor("lat", "lon", deg2rad($this->lat), deg2rad($this->lon)); $Sphinx->SetFilterRange("@geodist", 0, 5000); // 1km radius $Sphinx->SetSortMode(SPH_SORT_EXTENDED, '@geodist ASC'); $result = $Sphinx->query("", "idx_images"); $return = array("stat" => "ok"); if (!$result) { $return['stat'] = "err"; $return['message'] = $Sphinx->getLastError(); return $return; } if (empty($result['matches'])) { return $return; } foreach ($result['matches'] as $row) { $meta = json_decode($row['attrs']['meta'], true); $return['photos'][] = array("id" => $row['attrs']['image_id'], "provider" => $row['attrs']['provider'], "photo_id" => $row['attrs']['photo_id'], "url" => $row['attrs']['url'], "distance" => round($row['attrs']['@geodist']), "lat" => rad2deg($row['attrs']['lat']), "lon" => rad2deg($row['attrs']['lon']), "title" => empty(trim($row['attrs']['title'])) ? "Untitled" : $row['attrs']['title'], "description" => $row['attrs']['description'], "sizes" => Images::normaliseSizes($meta['sizes'])); } $return['photos'] = array_slice($return['photos'], 0, $num); return $return; }
/** * Get the top 5 photos viewed today/weekly/overall * @since Version 3.10.0 * @param string $lookup * @param int $num * @return array */ public function getMostViewedPhotos($lookup = self::HITS_WEEKLY, $num = 5) { $allowed = [self::HITS_DAILY => "hits_today", self::HITS_WEEKLY => "hits_weekly", self::HITS_OVERALL => "hits_overall"]; if (!in_array($lookup, array_keys($allowed))) { throw new InvalidArgumentException("Parameter supplied for lookup type is invalid"); } $query = "SELECT i.*,\r\n u.username,\r\n f.*\r\n FROM image AS i\r\n LEFT JOIN image_flags AS f ON f.image_id = i.id\r\n LEFT JOIN nuke_users AS u ON i.user_id = u.user_id\r\n ORDER BY " . $allowed[$lookup] . " DESC \r\n LIMIT 0, ?"; $result = $this->db->fetchAll($query, $num); foreach ($result as $key => $val) { $result[$key]['meta'] = json_decode($val['meta'], true); $result[$key]['meta']['author']['url'] = UserUrlUtility::MakeURLs($val); $result[$key]['meta']['sizes'] = Images::normaliseSizes($result[$key]['meta']['sizes']); if ($result[$key]['meta']['author']['url'] instanceof Url) { $result[$key]['meta']['author']['url'] = $result[$key]['meta']['author']['url']->getURLs(); } } return $result; }