Vignette.php 4.18 KB
Newer Older
1 2 3 4
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
5
 * BOKEH is free software; you can redistribute it and/or modify
6 7 8 9 10 11
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
12
 * BOKEH is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
18
 * along with BOKEH; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
20
 */
21 22

class Class_WebService_Vignette extends Class_WebService_Abstract {
23
  use Trait_StormFileSystem;
24

25
  const NO_DATA = 'NO';
26

27 28 29
  public static function getClefControle() {
    $clef = "IMG" . date("DxzxYxM", static::getTimeSource()->time()) . "VIG";
    return md5($clef);
Patrick Barroca's avatar
Patrick Barroca committed
30
  }
31 32


33 34 35
  public function isThirdParty($record) {
    return $this->getThumbnailProviderFor($record)
                ->isThirdParty();
Patrick Barroca's avatar
Patrick Barroca committed
36
  }
37

38

39 40
  public function isNoData($value) {
    return static::NO_DATA === $value;
Patrick Barroca's avatar
Patrick Barroca committed
41
  }
42

43

Patrick Barroca's avatar
Patrick Barroca committed
44
  public function getThumbnailProviderFor($notice) {
Patrick Barroca's avatar
Patrick Barroca committed
45 46
    $factory = new Class_Notice_Thumbnail_ProviderFactory();
    return $factory->newWith($this->getFileSystem(), $notice);
Patrick Barroca's avatar
Patrick Barroca committed
47
  }
48 49


Patrick Barroca's avatar
Patrick Barroca committed
50
  public function updateUrlsFromCacheServer($notice) {
51
    $this->getThumbnailProviderFor($notice)->updateNotice();
Patrick Barroca's avatar
Patrick Barroca committed
52 53
    return $this;
  }
54

55

Patrick Barroca's avatar
Patrick Barroca committed
56 57 58 59 60 61 62 63 64
  public static function getAjaxUrl($notice) {
    $clef_controle = Class_WebService_Vignette::getClefControle();
    return Class_Url::assemble(['controller' => 'recherche',
                                'action' => 'vignette',
                                'clef' => $clef_controle,
                                'id_notice' => $notice->getId()],
                               null,
                               true);
  }
65

66 67

  public static function getSource($url) {
Patrick Barroca's avatar
Patrick Barroca committed
68
    // règles de reconnaissance
69 70 71 72 73 74 75 76 77
    $defaut = "Serveur Afi";
    $sources = ["Amazon"=>"amazon",
                "Decitre"=>"decitre",
                "Last-fm"=>"lastfm",
                "Deezer"=>"deezer",
                "Virgin"=>"virginmega",
                "Comme-au-cinema"=>"commeaucinema",
                "Fan-de-cinema"=>"fan-de-cinema",
                "Premiere"=>"premiere"];
Patrick Barroca's avatar
Patrick Barroca committed
78 79

    // renvoyer la source
80 81 82
    if(!$url)
      return $defaut;

Patrick Barroca's avatar
Patrick Barroca committed
83
    foreach($sources as $key => $valeur)
84 85 86
      if(stripos($url, $valeur) > 0)
        return $key;

Patrick Barroca's avatar
Patrick Barroca committed
87 88 89 90
    return $defaut;
  }


91
  static function renderThumbnail($id_notice,$titre,$type_doc)  {
92
    $image = static::createImage($titre, 300, 400, $type_doc);
93
    imagepng($image);
Patrick Barroca's avatar
Patrick Barroca committed
94 95 96
  }


97
  public static function deleteVignetteCacheForNotice($id) {
98 99 100
    $vignette_cache = PATH_TEMP.'vignettes_titre/notice_'.$id.'.';
    foreach(['jpg', 'png'] as $ext) {
      $filepath = $vignette_cache.$ext;
Patrick Barroca's avatar
Patrick Barroca committed
101
      if (file_exists($filepath)) unlink($filepath);
102
    }
Patrick Barroca's avatar
Patrick Barroca committed
103
  }
104 105


Patrick Barroca's avatar
Patrick Barroca committed
106 107
  /** download and save image at given url */
  public function writeImageCache($notice, $url)  {
108
    return $this->getThumbnailProviderFor($notice)->writeImageCache($url);
Patrick Barroca's avatar
Patrick Barroca committed
109
  }
110

111 112

  public static function createImage($titre, $width=100, $height=110, $type_doc) {
113
    $image = imagecreatetruecolor($width, $height);
114

Patrick Barroca's avatar
Patrick Barroca committed
115 116 117
    // Couleur de fond
    $fond = imagecolorallocate($image, 255, 255, 255);
    imagefill($image, 0, 0, $fond);
118

Patrick Barroca's avatar
Patrick Barroca committed
119 120
    // Titre
    if($titre > "")
121
      $image = static::writeText($titre, $image);
Patrick Barroca's avatar
Patrick Barroca committed
122 123
    return $image;
  }
124

125

Patrick Barroca's avatar
Patrick Barroca committed
126 127 128 129 130 131 132
  public static function writeText ($titre, $image_obj, $position = 0) {
    // Tronçonner le texte
    $texte=wordwrap(utf8_decode(str_replace('<br />', ';', $titre)), 12, ';');
    $texte=explode(";", $texte);

    // Parametres
    $couleur = imagecolorallocate($image_obj, 102, 102, 102);
133 134 135 136 137
    $pos_x=20;
    $pos_y=50;
    $hauteur=50;
    $largeur=200;

Patrick Barroca's avatar
Patrick Barroca committed
138 139

    // Afficher
140
    foreach($texte as $ligne)  {
141 142
      imagettftext($image_obj, 30, 0, $pos_x, $pos_y, $couleur, PATH_FONTS . 'Vera.ttf', trim($ligne));
      $pos_y += $hauteur;
Patrick Barroca's avatar
Patrick Barroca committed
143
    }
144

145
    return $image_obj;
Patrick Barroca's avatar
Patrick Barroca committed
146
  }
147
}