Ejemplo n.º 1
0
 /**
  * Automatically find tags for the image and assign them.
  *
  * @return boolean
  */
 public function autoFindTags()
 {
     $url = str_finish(Config::get('services.alchemyapi.url'), '/') . 'URLGetRankedImageKeywords';
     $imageUrl = null;
     $previewSize = $this->getImageSize('preview');
     if ($previewSize > 1024 * 1024 || $previewSize === 0) {
         $imageUrl = $this->thumbnailUrl;
     } else {
         $imageUrl = $this->previewUrl;
     }
     $params = ['url' => $imageUrl, 'apikey' => Config::get('services.alchemyapi.key'), 'outputMode' => 'json'];
     $url .= '?' . http_build_query($params);
     $tags = json_decode(file_get_contents($url));
     if (strtolower($tags->status) != 'ok') {
         Log::error("Unsuccessfull response from tagging api for image #{$this->id}: " . serialize($tags));
         return false;
     }
     if (count($tags->imageKeywords) === 0) {
         $tag = Tag::firstOrCreate(['title' => 'unknown']);
         $this->tags()->attach($tag->id);
         return true;
     }
     foreach ($tags->imageKeywords as $tagRaw) {
         $tag = Tag::firstOrCreate(['title' => $tagRaw->text]);
         $this->tags()->attach($tag->id);
     }
     return true;
 }