Changeset 79194 in spip-zone


Ignore:
Timestamp:
Dec 6, 2013, 9:25:33 AM (6 years ago)
Author:
cedric@…
Message:

Mise a jour de la librairie AdaptiveImages?. Prise en charge des URLs absolues dans le HTML, et d'un domaine static pour les images, defini par _ADAPTIVE_IMAGES_DOMAIN (domain sharding)

Location:
_plugins_/adaptive_images/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • _plugins_/adaptive_images/trunk/adaptive_images_options.php

    r79131 r79194  
    1414
    1515include_once(_DIR_PLUGIN_ADAPTIVE_IMAGES."lib/AdaptiveImages/AdaptiveImages.php");
    16 $AdaptiveImage = AdaptiveImages::getInstance();
     16class SPIPAdaptiveImages extends AdaptiveImages {
     17        protected function URL2filepath($url){
     18                $url = parent::URL2filepath($url);
     19                // URL absolue en URL relative
     20                if (preg_match(",^https?://,",$url)){
     21                        $base = url_de_base();
     22                        if (strncmp($url,$base,strlen($base))==0)
     23                                $url = _DIR_RACINE . substr($url,strlen($base));
     24                        elseif (defined('_ADAPTIVE_IMAGES_DOMAIN')
     25                          AND strncmp($url,_ADAPTIVE_IMAGES_DOMAIN,strlen(_ADAPTIVE_IMAGES_DOMAIN))==0){
     26                                $url = _DIR_RACINE . substr($url,strlen(_ADAPTIVE_IMAGES_DOMAIN));
     27                        }
     28                }
     29                return $url;
     30        }
     31
     32        protected function filepath2URL($filepath){
     33                $filepath = parent::filepath2URL($filepath);
     34                if (defined('_ADAPTIVE_IMAGES_DOMAIN')){
     35                        $filepath = rtrim(_ADAPTIVE_IMAGES_DOMAIN,"/")."/".$filepath;
     36                }
     37                return $filepath;
     38        }
     39}
     40$AdaptiveImages = SPIPAdaptiveImages::getInstance();
     41
    1742
    1843// utiliser le progressive rendering sur PNG et GIF si pas de JS
    1944if (defined('_ADAPTIVE_IMAGES_NOJS_PNGGIF_PROGRESSIVE_RENDERING'))
    20         $AdaptiveImage->nojsPngGifProgressiveRendering = _ADAPTIVE_IMAGES_NOJS_PNGGIF_PROGRESSIVE_RENDERING;
     45        $AdaptiveImages->nojsPngGifProgressiveRendering = _ADAPTIVE_IMAGES_NOJS_PNGGIF_PROGRESSIVE_RENDERING;
    2146// couleur de background pour les images lowsrc
    2247if (defined('_ADAPTIVE_IMAGES_LOWSRC_JPG_BG_COLOR'))
    23         $AdaptiveImage->lowsrcJpgBgColor = _ADAPTIVE_IMAGES_LOWSRC_JPG_BG_COLOR;
     48        $AdaptiveImages->lowsrcJpgBgColor = _ADAPTIVE_IMAGES_LOWSRC_JPG_BG_COLOR;
    2449// qualite des images lowsrc
    2550if (defined('_ADAPTIVE_IMAGES_LOWSRC_JPG_QUALITY'))
    26         $AdaptiveImage->lowsrcJpgQuality = _ADAPTIVE_IMAGES_LOWSRC_JPG_QUALITY;
     51        $AdaptiveImages->lowsrcJpgQuality = _ADAPTIVE_IMAGES_LOWSRC_JPG_QUALITY;
    2752// qualite de compression JPG pour les images 1.5x et 2x (on peut comprimer plus)
    2853if (defined('_ADAPTIVE_IMAGES_15x_JPG_QUALITY'))
    29         $AdaptiveImage->x15JpgQuality = _ADAPTIVE_IMAGES_15x_JPG_QUALITY;
     54        $AdaptiveImages->x15JpgQuality = _ADAPTIVE_IMAGES_15x_JPG_QUALITY;
    3055if (defined('_ADAPTIVE_IMAGES_20x_JPG_QUALITY'))
    31         $AdaptiveImage->x20JpgQuality = _ADAPTIVE_IMAGES_20x_JPG_QUALITY;
     56        $AdaptiveImages->x20JpgQuality = _ADAPTIVE_IMAGES_20x_JPG_QUALITY;
    3257// Breakpoints de taille d'ecran pour lesquels on generent des images
    3358if (defined('_ADAPTIVE_IMAGES_DEFAULT_BKPTS'))
    34         $AdaptiveImage->defaultBkpts = explode(",",_ADAPTIVE_IMAGES_DEFAULT_BKPTS);
     59        $AdaptiveImages->defaultBkpts = explode(",",_ADAPTIVE_IMAGES_DEFAULT_BKPTS);
    3560// les images 1x sont au maximum en _ADAPTIVE_IMAGES_MAX_WIDTH_1x px de large dans la page
    3661if (defined('_ADAPTIVE_IMAGES_MAX_WIDTH_1x'))
    37         $AdaptiveImage->maxWidth1x = _ADAPTIVE_IMAGES_MAX_WIDTH_1x;
     62        $AdaptiveImages->maxWidth1x = _ADAPTIVE_IMAGES_MAX_WIDTH_1x;
    3863// on ne traite pas les images de largeur inferieure a _ADAPTIVE_IMAGES_MIN_WIDTH_1x px
    3964if (defined('_ADAPTIVE_IMAGES_MIN_WIDTH_1x'))
    40         $AdaptiveImage->minWidth1x = _ADAPTIVE_IMAGES_MIN_WIDTH_1x;
     65        $AdaptiveImages->minWidth1x = _ADAPTIVE_IMAGES_MIN_WIDTH_1x;
    4166// Pour les ecrans plus petits, c'est la version mobile qui est fournie (recadree)
    4267if (defined('_ADAPTIVE_IMAGES_MAX_WIDTH_MOBILE_VERSION'))
    43         $AdaptiveImage->maxWidthMobileVersion = _ADAPTIVE_IMAGES_MAX_WIDTH_MOBILE_VERSION;
     68        $AdaptiveImages->maxWidthMobileVersion = _ADAPTIVE_IMAGES_MAX_WIDTH_MOBILE_VERSION;
    4469
    4570// GD memory limit
    4671if (defined('_IMG_GD_MAX_PIXELS'))
    47         $AdaptiveImage->maxImagePxGDMemoryLimit = _IMG_GD_MAX_PIXELS;
     72        $AdaptiveImages->maxImagePxGDMemoryLimit = _IMG_GD_MAX_PIXELS;
    4873
    4974// Pour generer chaque variante d'image uniquement quand elle est demandee pour la premiere fois
     
    6186define('_ADAPTIVE_IMAGES_ON_DEMAND_PRODUCTION',true);
    6287if (defined('_ADAPTIVE_IMAGES_ON_DEMAND_PRODUCTION'))
    63         $AdaptiveImage->onDemandImages = _ADAPTIVE_IMAGES_ON_DEMAND_PRODUCTION;
     88        $AdaptiveImages->onDemandImages = _ADAPTIVE_IMAGES_ON_DEMAND_PRODUCTION;
    6489
    6590// dossier de stockage des images adaptatives
    66 $AdaptiveImage->destDirectory = _DIR_VAR . "adapt-img/";
     91$AdaptiveImages->destDirectory = _DIR_VAR . "adapt-img/";
    6792
    6893
     
    80105function action_adapt_img_dist(){
    81106
    82         $AdaptiveImage = AdaptiveImages::getInstance();
     107        $AdaptiveImages = SPIPAdaptiveImages::getInstance();
    83108        try {
    84                 $AdaptiveImage->deliverBkptImage(_request('arg'));
     109                $AdaptiveImages->deliverBkptImage(_request('arg'));
    85110        }
    86111        catch (Exception $e){
     
    104129 */
    105130function adaptive_images($texte,$max_width_1x=null){
    106         $AdaptiveImage = AdaptiveImages::getInstance();
    107         return $AdaptiveImage->adaptHTMLPart($texte, $max_width_1x);
     131        $AdaptiveImages = SPIPAdaptiveImages::getInstance();
     132        return $AdaptiveImages->adaptHTMLPart($texte, $max_width_1x);
    108133}
    109134
     
    190215        if ($GLOBALS['html']){
    191216                #spip_timer();
    192                 $AdaptiveImage = AdaptiveImages::getInstance();
    193                 $texte = $AdaptiveImage->adaptHTMLPage($texte);
     217                $AdaptiveImages = SPIPAdaptiveImages::getInstance();
     218                $texte = $AdaptiveImages->adaptHTMLPage($texte);
    194219                #var_dump(spip_timer());
    195220        }
  • _plugins_/adaptive_images/trunk/lib/AdaptiveImages/AdaptiveImages.php

    r79131 r79194  
    33 * AdaptiveImages
    44 *
    5  * @version    1.0.2
     5 * @version    1.1.0
    66 * @copyright  2013
    77 * @author     Nursit
     
    1313class AdaptiveImages {
    1414        /**
    15          * @var AdaptiveImages
    16          */
    17         static protected $instance;
     15         * @var Array
     16         */
     17        static protected $instances = array();
    1818
    1919        /**
     
    119119         */
    120120        public function __get($property){
    121                 if(!property_exists($this,$property) OR $property=="instance") {
     121                if(!property_exists($this,$property) OR $property=="instances") {
    122122      throw new InvalidArgumentException("Property {$property} doesn't exist");
    123123    }
     
    133133         */
    134134        public function __set($property, $value){
    135                 if(!property_exists($this,$property) OR $property=="instance") {
     135                if(!property_exists($this,$property) OR $property=="instances") {
    136136      throw new InvalidArgumentException("Property {$property} doesn't exist");
    137137    }
     
    171171         */
    172172        static public function getInstance() {
    173          if (!(self::$instance instanceof self)) {
    174            self::$instance = new self;
    175          }
    176          return self::$instance;
     173                $class_name = get_called_class();
     174                if(!array_key_exists($class_name, self::$instances)) {
     175      self::$instances[$class_name] = new $class_name();
     176    }
     177    return self::$instances[$class_name];
    177178        }
    178179
     
    184185        protected function log($message){
    185186
     187        }
     188
     189        /**
     190         * Convert URL path to file system path
     191         * By default just remove existing timestamp
     192         * Should be overriden depending of your URL mapping rules vs DOCUMENT_ROOT
     193         * can also remap Absolute URL of current website to filesystem path
     194         * @param $url
     195         * @return string
     196         */
     197        protected function URL2filepath($url){
     198                // remove timestamp on URL
     199                if (($p=strpos($url,'?'))!==FALSE)
     200                        $url=substr($url,0,$p);
     201
     202                return $url;
     203        }
     204
     205        /**
     206         * Convert file system path to URL path
     207         * By default just add timestamp for webperf issue
     208         * Should be overriden depending of your URL mapping rules vs DOCUMENT_ROOT
     209         * can map URL on specific domain (domain sharding for Webperf purpose)
     210         * @param $filepath
     211         * @return string
     212         */
     213        protected function filepath2URL($filepath){
     214                // be carefull : maybe file doesn't exists yet (On demand generation)
     215                if ($t = @filemtime($filepath))
     216                        $filepath = "$filepath?$t";
     217                return $filepath;
    186218        }
    187219
     
    494526                                '20x' => $src,
    495527                        );
    496                 $src = preg_replace(',[?][0-9]+$,', '', $src);
     528
     529                $src = $this->URL2filepath($src);
     530                if ($srcMobile)
     531                        $srcMobile = $this->URL2filepath($srcMobile);
    497532
    498533                // don't do anyting if we can't find file
     
    555590                $src = $this->processBkptImage($src,$maxWidth1x,$maxWidth1x,'10x',$extension,true);
    556591                list($w,$h) = $this->imgSize($src);
    557                 $img = $this->setTagAttribute($img,"src",$src);
     592                $img = $this->setTagAttribute($img,"src",$this->filepath2URL($src));
    558593                $img = $this->setTagAttribute($img,"width",$w);
    559594                $img = $this->setTagAttribute($img,"height",$h);
     
    634669                                        $mw = $mwdpi[$kx];
    635670                                        $not = $htmlsel[$kx];
    636                                         $medias[$mw] = "@media $mw{{$not} span.$cid,{$not} span.$cid:after{background-image:url($file);}}";
     671                                        $url = $this->filepath2URL($file);
     672                                        $medias[$mw] = "@media $mw{{$not} span.$cid,{$not} span.$cid:after{background-image:url($url);}}";
    637673                                }
    638674                        }
     
    644680                if ($wandroid){
    645681                        $file = $bkptImages[$wandroid]['15x'];
    646                         $medias['android2'] = "html.android2 span.$cid,html.android2 span.$cid:after{background-image:url($file);}";
     682                        $url = $this->filepath2URL($file);
     683                        $medias['android2'] = "html.android2 span.$cid,html.android2 span.$cid:after{background-image:url($url);}";
    647684                }
    648685
     
    683720                        $srcWidth = $this->tagAttribute($img,'width');
    684721                        $srcHeight = $this->tagAttribute($img,'height');
     722                        if ($srcWidth AND $srcHeight)
     723                                return array($srcWidth,$srcHeight);
     724                        $source = $this->URL2filepath($source);
    685725                }
    686726
     
    689729                        return array(0,0);
    690730                }
    691                 // remove timestamp on URL
    692                 if (($p=strpos($source,'?'))!==FALSE)
    693                         $source=substr($source,0,$p);
    694731
    695732                if (isset($largeur_img[$source]))
     
    11441181                        return false;
    11451182                }
     1183                else
     1184                        $source = $this->URL2filepath($source);
    11461185
    11471186                // don't process distant images
     
    11491188                        return false;
    11501189                }
    1151 
    1152                 // remove timestamp as query string
    1153                 $source=preg_replace(',[?][0-9]+$,','',$source);
    11541190
    11551191                $extension_dest = "";
  • _plugins_/adaptive_images/trunk/lib/AdaptiveImages/README.md

    r79131 r79194  
    1919<pre>
    2020require_once "AdaptiveImages.php"
    21 $AdaptiveImage = AdaptiveImages::getInstance();
    22 $html = $AdaptiveImage->adaptHTMLPage($html,780);
     21$AdaptiveImages = AdaptiveImages::getInstance();
     22$html = $AdaptiveImages->adaptHTMLPage($html,780);
    2323</pre>
    2424
     
    3131<pre>
    3232require_once "AdaptiveImages.php"
    33 $AdaptiveImage = AdaptiveImages::getInstance();
    34 return $AdaptiveImage->adaptHTMLPart($texte, 780);
     33$AdaptiveImages = AdaptiveImages::getInstance();
     34return $AdaptiveImages->adaptHTMLPart($texte, 780);
    3535</pre>
    3636
     
    3838
    3939<pre>
    40 $AdaptiveImage = AdaptiveImages::getInstance();
    41 $html = $AdaptiveImage->adaptHTMLPage($html,780);
     40$AdaptiveImages = AdaptiveImages::getInstance();
     41$html = $AdaptiveImages->adaptHTMLPage($html,780);
     42</pre>
     43
     44### URL vs filepath
     45
     46By default AdaptiveImages considers that relative URLs are also relative file system path.
     47If this is not the case in your URL scheme, you can override the 2 methods `URL2filepath` and `filepath2URL` that are used to make transpositions.
     48
     49In the following example we transpose absolutes URLs to relative file system path and, if defined we add special domain _ADAPTIVE_IMAGES_DOMAIN to file path in the final URL (domain sharding)
     50
     51<pre>
     52class MyAdaptiveImages extends AdaptiveImages {
     53        protected function URL2filepath($url){
     54                $url = parent::URL2filepath($url);
     55                // absolute URL to relative file path
     56                if (preg_match(",^https?://,",$url)){
     57                        $base = url_de_base();
     58                        if (strncmp($url,$base,strlen($base))==0)
     59                                $url = _DIR_RACINE . substr($url,strlen($base));
     60                        elseif (defined('_ADAPTIVE_IMAGES_DOMAIN')
     61                          AND strncmp($url,_ADAPTIVE_IMAGES_DOMAIN,strlen(_ADAPTIVE_IMAGES_DOMAIN))==0){
     62                                $url = _DIR_RACINE . substr($url,strlen(_ADAPTIVE_IMAGES_DOMAIN));
     63                        }
     64                }
     65                return $url;
     66        }
     67
     68        protected function filepath2URL($filepath){
     69                $filepath = parent::filepath2URL($filepath);
     70                if (defined('_ADAPTIVE_IMAGES_DOMAIN')){
     71                        $filepath = rtrim(_ADAPTIVE_IMAGES_DOMAIN,"/")."/".$filepath;
     72                }
     73                return $filepath;
     74        }
     75}
     76$AdaptiveImages = MyAdaptiveImages::getInstance();
    4277</pre>
    4378
     
    74109function action_adapt_img_dist(){
    75110
    76         $AdaptiveImage = AdaptiveImages::getInstance();
     111        $AdaptiveImages = AdaptiveImages::getInstance();
    77112        try {
    78                 $AdaptiveImage->deliverBkptImage(_request('arg'));
     113                $AdaptiveImages->deliverBkptImage(_request('arg'));
    79114        }
    80115        catch (Exception $e){
     
    89124
    90125* Directory for storing adaptive images
    91   <pre>$AdaptiveImage->destDirectory = "local/adapt-img/";</pre>
     126  <pre>$AdaptiveImages->destDirectory = "local/adapt-img/";</pre>
    92127* Default Maximum display width for images
    93   <pre>$AdaptiveImage->maxWidth1x = 640;</pre>
     128  <pre>$AdaptiveImages->maxWidth1x = 640;</pre>
    94129* Minimum display width for adaptive images (smaller will be unchanged)
    95   <pre>$AdaptiveImage->minWidth1x = 320;</pre>
     130  <pre>$AdaptiveImages->minWidth1x = 320;</pre>
    96131* Maximum width for delivering mobile version in data-src-mobile=""
    97   <pre>$AdaptiveImage->maxWidthMobileVersion = 320;</pre>
     132  <pre>$AdaptiveImages->maxWidthMobileVersion = 320;</pre>
    98133* Activade On-Demand images generation
    99   <pre>$AdaptiveImage->onDemandImages = true;</pre>
     134  <pre>$AdaptiveImages->onDemandImages = true;</pre>
    100135* Background color for JPG lowsrc generation (if source has transparency layer)
    101   <pre>$AdaptiveImage->lowsrcJpgBgColor = '#eeeeee';</pre>
     136  <pre>$AdaptiveImages->lowsrcJpgBgColor = '#eeeeee';</pre>
    102137* Breakpoints width for image generation
    103   <pre>$AdaptiveImage->defaultBkpts = array(160,320,480,640,960,1440);</pre>
     138  <pre>$AdaptiveImages->defaultBkpts = array(160,320,480,640,960,1440);</pre>
    104139* Allow progressive rendering og PNG and GIF even without JS :
    105   <pre>$AdaptiveImage->nojsPngGifProgressiveRendering = true;</pre>
     140  <pre>$AdaptiveImages->nojsPngGifProgressiveRendering = true;</pre>
    106141* JPG compression quality for JPG lowsrc
    107   <pre>$AdaptiveImage->lowsrcJpgQuality = 10;</pre>
     142  <pre>$AdaptiveImages->lowsrcJpgQuality = 10;</pre>
    108143* JPG compression quality for 1x JPG images
    109   <pre>$AdaptiveImage->x10JpgQuality = 75;</pre>
     144  <pre>$AdaptiveImages->x10JpgQuality = 75;</pre>
    110145* JPG compression quality for 1.5x JPG images
    111   <pre>$AdaptiveImage->x15JpgQuality = 65;</pre>
     146  <pre>$AdaptiveImages->x15JpgQuality = 65;</pre>
    112147* JPG compression quality for 2x JPG images
    113   <pre>$AdaptiveImage->x15JpgQuality = 45;</pre>
     148  <pre>$AdaptiveImages->x15JpgQuality = 45;</pre>
    114149* GD maximum px size (width x height) of image that can be manipulated without Fatal Memory Error (0=no limit)
    115   <pre>$AdaptiveImage->maxImagePxGDMemoryLimit = 2000*2000;</pre>
     150  <pre>$AdaptiveImages->maxImagePxGDMemoryLimit = 2000*2000;</pre>
    116151
    117152
  • _plugins_/adaptive_images/trunk/paquet.xml

    r79131 r79194  
    22        prefix="adaptive_images"
    33        categorie="multimedia"
    4         version="1.3.1"
     4        version="1.4.0"
    55        etat="stable"
    66        compatibilite="[3.0.0;3.0.*]"
Note: See TracChangeset for help on using the changeset viewer.