/** * Generate the JSON data string * * @return $this */ public function getJSON() { if (isset($this->author)) { $author = clone $this->author; if (isset($author->User) && $author->User instanceof User) { $author->User = $author->User->getArray(); } } $data = array("id" => $this->id, "title" => $this->title, "description" => $this->description, "score" => $this->getScore(), "provider" => array("name" => $this->provider, "photo_id" => $this->photo_id), "sizes" => Images::NormaliseSizes($this->sizes), "srcset" => implode(", ", Utility\ImageUtility::generateSrcSet($this)), "author" => isset($author) ? $author : false, "url" => $this->url instanceof Url ? $this->url->getURLs() : array(), "dates" => array()); $times = ["posted", "taken"]; #printArray($this->meta['dates']);die; foreach ($times as $time) { if (isset($this->meta['dates'][$time])) { $Date = filter_var($this->meta['dates'][$time], FILTER_VALIDATE_INT) ? new DateTime("@" . $this->meta['dates'][$time]) : new DateTime($this->meta['dates'][$time]); $data['dates'][$time] = array("absolute" => $Date->format("Y-m-d H:i:s"), "nice" => $Date->Format("d H:i:s") == "01 00:00:00" ? $Date->Format("F Y") : $Date->format("F j, Y, g:i a"), "relative" => ContentUtility::RelativeTime($Date)); } } if ($this->Place instanceof Place) { $data['place'] = array("url" => $this->Place->url, "lat" => $this->Place->lat, "lon" => $this->Place->lon, "name" => $this->Place->name, "country" => array("code" => $this->Place->Country->code, "name" => $this->Place->Country->name, "url" => $this->Place->Country->url)); } $this->json = json_encode($data); return $this; }
/** * Get a associative array of this photo data in a standardised format * @since Version 3.9 * @return array */ public function getArray() { return array("id" => $this->id, "title" => $this->title, "description" => $this->description, "provider" => $this->getProviderName(), "url" => $this->url->getURLs(), "sizes" => Images::NormaliseSizes($this->sizes), "dates" => $this->dates); }
/** * Find a suitable cover photo * @since Version 3.10.0 * @param string|object $searchQuery * @return string */ public static function GuessCoverPhoto($searchQuery) { $defaultPhoto = "https://static.railpage.com.au/i/logo-fb.jpg"; $cachekey = sprintf("railpage:coverphoto=%s", md5($searchQuery)); $Memcached = AppCore::getMemcached(); #if ($image = $Memcached->fetch($cachekey)) { # return $image; #} $SphinxQL = AppCore::getSphinx(); if (!is_string($searchQuery)) { return $defaultPhoto; } preg_match_all('/([a-zA-Z]|\\xC3[\\x80-\\x96\\x98-\\xB6\\xB8-\\xBF]|\\xC5[\\x92\\x93\\xA0\\xA1\\xB8\\xBD\\xBE]){4,}/', $searchQuery, $match_arr); $word_arr = $match_arr[0]; $words = implode(" || ", $word_arr); $SphinxQL->select()->from("idx_images")->match(array("title", "description"), $words, true); $rs = $SphinxQL->execute(); if (!count($rs)) { return $defaultPhoto; } $photo = $rs[0]; $photo['meta'] = json_decode($photo['meta'], true); $photo['sizes'] = Images::NormaliseSizes($photo['meta']['sizes']); foreach ($photo['sizes'] as $size) { if ($size['width'] > 400 && $size['height'] > 300) { $Memcached->save($cachekey, $size['source'], 0); return $size['source']; } } }