diff --git a/AdaptiveImages.php b/AdaptiveImages.php index 70c254f..0b374e9 100644 --- a/AdaptiveImages.php +++ b/AdaptiveImages.php @@ -1,20 +1,21 @@ array('webp'), 'png' => array('webp'), 'jpg' => array('webp') */ - ); + ]; /** * separator used between source extension and alternative extension @@ -128,7 +127,7 @@ class AdaptiveImages { * directory for storing adaptive images * @var string */ - protected $destDirectory = "local/adapt-img/"; + protected $destDirectory = 'local/adapt-img/'; /** * Maximum number of px for image that can be loaded in memory by GD @@ -166,17 +165,17 @@ class AdaptiveImages { /** * Constructor */ - protected function __construct(){ + protected function __construct() + { } /** * get - * @param $property * @return mixed - * @throws InvalidArgumentException */ - public function __get($property){ - if (!property_exists($this, $property) or $property=="instances"){ + public function __get($property) + { + if (! property_exists($this, $property) or $property === 'instances') { throw new InvalidArgumentException("Property {$property} doesn't exist"); } return $this->{$property}; @@ -184,42 +183,49 @@ public function __get($property){ /** * set - * @param $property - * @param $value * @return mixed - * @throws InvalidArgumentException */ - public function __set($property, $value){ - if (!property_exists($this, $property) or $property=="instances"){ + public function __set($property, $value) + { + if (! property_exists($this, $property) or $property === 'instances') { throw new InvalidArgumentException("Property {$property} doesn't exist"); } - if (in_array($property, array("nojsPngGifProgressiveRendering", "onDemandImages", "lazyload", "alwaysIntrinsic"))){ - if (!is_bool($value)){ + if (in_array( + $property, + ['nojsPngGifProgressiveRendering', 'onDemandImages', 'lazyload', 'alwaysIntrinsic'], + true + )) { + if (! is_bool($value)) { throw new InvalidArgumentException("Property {$property} needs a bool value"); } - } elseif (in_array($property, array("lowsrcJpgBgColor", "destDirectory", "thumbnailGeneratorCallback", "markupMethod"))) { - if (!is_string($value)){ + } elseif (in_array( + $property, + ['lowsrcJpgBgColor', 'destDirectory', 'thumbnailGeneratorCallback', 'markupMethod'], + true + )) { + if (! is_string($value)) { throw new InvalidArgumentException("Property {$property} needs a string value"); } - } elseif (in_array($property, array("defaultBkpts", "acceptedFormats", "alternativeFormats"))) { - if (!is_array($value)){ + } elseif (in_array($property, ['defaultBkpts', 'acceptedFormats', 'alternativeFormats'], true)) { + if (! is_array($value)) { throw new InvalidArgumentException("Property {$property} needs an array value"); } - } elseif (!is_int($value)) { + } elseif (! is_int($value)) { throw new InvalidArgumentException("Property {$property} needs an int value"); } - if ($property=="defaultBkpts"){ + if ($property === 'defaultBkpts') { sort($value); } - return ($this->{$property} = $value); + return $this->{$property} = $value; } /** * Disable cloning */ - protected function __clone(){ - trigger_error("Cannot clone a singleton class", E_USER_ERROR); + protected function __clone() + { + trigger_error('Cannot clone a singleton class', E_USER_ERROR); } /** @@ -227,107 +233,15 @@ protected function __clone(){ * * @return AdaptiveImages */ - static public function getInstance(){ - $class_name = (function_exists("get_called_class") ? get_called_class() : "AdaptiveImages"); - if (!array_key_exists($class_name, self::$instances)){ + public static function getInstance() + { + $class_name = (function_exists('get_called_class') ? static::class : 'AdaptiveImages'); + if (! array_key_exists($class_name, self::$instances)) { self::$instances[$class_name] = new $class_name(); } return self::$instances[$class_name]; } - /** - * Log function for internal warning if we can avoid to throw an Exception - * Do nothing, should be overriden with your personal log function - * @param $message - */ - protected function log($message){ - - } - - /** - * Convert URL path to file system path - * By default just remove existing timestamp - * Should be overriden depending of your URL mapping rules vs DOCUMENT_ROOT - * can also remap Absolute URL of current website to filesystem path - * @param $url - * @return string - */ - protected function URL2filepath($url){ - // remove timestamp on URL - if (($p = strpos($url, '?'))!==FALSE){ - $url = substr($url, 0, $p); - } - - return $url; - } - - /** - * Convert file system path to URL path - * By default just add timestamp for webperf issue - * Should be overriden depending of your URL mapping rules vs DOCUMENT_ROOT - * can map URL on specific domain (domain sharding for Webperf purpose) - * @param string $filepath - * @param bool $relative - * @return string - */ - protected function filepath2URL($filepath, $relative = false){ - // be carefull : maybe file doesn't exists yet (On demand generation) - if ($t = @filemtime($filepath)){ - $filepath = "$filepath?$t"; - } - return $filepath; - } - - /** - * This hook allows to personalize markup depending on source img style and class attributes - * This do-noting method should be adapted to source markup generated by your CMS - * - * For instance : could be adapted in - * - * - * @param string $markup - * @param string $originalClass - * @param string $originalStyle - * @return mixed - */ - protected function imgMarkupHook(&$markup, $originalClass, $originalStyle){ - return $markup; - } - - /** - * Translate src of original image to URL subpath of adapted image - * the result will makes subdirectory of $destDirectory/320/10x/ and other variants - * the result must allow to retrive src from url in adaptedURLToSrc() methof - * @param string $src - * @return string - */ - protected function adaptedSrcToURL($src){ - $url = $this->filepath2URL($src, true); - if (($p = strpos($url, '?'))!==FALSE){ - $url = substr($url, 0, $p); - } - // avoid / starting url : replace / by root/ - if (strncmp($url, "/", 1)==0){ - $url = "root" . $url; - } - return $url; - } - - /** - * Translate URL of subpath of adapted image to original image src - * This reverse the adaptedSrcToURL() method - * @param string $url - * @return string - */ - protected function adaptedURLToSrc($url){ - // replace root/ by / - if (strncmp($url, "root/", 5)==0){ - $url = substr($url, 4); - } - $src = $this->URL2filepath($url); - return $src; - } - /** * Process the full HTML page : * - adapt all in the HTML @@ -342,40 +256,41 @@ protected function adaptedURLToSrc($url){ * @return string * HTML modified page */ - public function adaptHTMLPage($html, $maxWidth1x = null, $bkpt = null){ + public function adaptHTMLPage($html, $maxWidth1x = null, $bkpt = null) + { // adapt all images that need it, if not already $html = $this->adaptHTMLPart($html, $maxWidth1x, $bkpt); // if there is adapted images in the page, add the necessary CSS and JS - if (strpos($html, "adapt-img-wrapper")!==false){ - $ins_style = ""; + if (strpos($html, 'adapt-img-wrapper') !== false) { + $ins_style = ''; // collect all adapt-img ),Ums", $html, $matches); - if (count($matches[2])){ - $html = str_replace($matches[1], "", $html); + preg_match_all(',(]*adaptive[^>]*>(.*)),Ums', $html, $matches); + if (count($matches[2])) { + $html = str_replace($matches[1], '', $html); $ins_style .= "\n"; // in case of this was only including \n"; // JS that evaluate connection speed and add a aislow class on if slow connection // and onload JS that adds CSS to finish rendering - $async_style = "picture.adapt-img-wrapper{background-size:0;}"; + $async_style = 'picture.adapt-img-wrapper{background-size:0;}'; $length = 1500; // ~1500 bytes for CSS and minified JS we add here // minified version of AdaptiveImages-light.js (using https://closure-compiler.appspot.com/home) $js = <<\n"; // JS that evaluate connection speed and add a aislow class on if slow connection // and onload JS that adds CSS to finish rendering - $async_style = "html img.adapt-img{opacity:0.01}html .adapt-img-wrapper::after{display:none;}"; + $async_style = 'html img.adapt-img{opacity:0.01}html .adapt-img-wrapper::after{display:none;}'; $length = 2000; // ~2000 bytes for CSS and minified JS we add here // minified version of AdaptiveImages.js (using https://closure-compiler.appspot.com/home) $js = <<c;else{var f=navigator.connection||navigator.mozConnection||navigator.webkitConnection;"undefined"!==typeof f&&(c=3==f.type||4==f.type||/^[23]g$/.test(f.type))}c&&d("aislow");var h=function(){var a=document.createElement("style");a.type="text/css";a.innerHTML=adaptImgAsyncStyles;var b=document.getElementsByTagName("style")[0];b.parentNode.insertBefore(a,b);window.matchMedia||window.onbeforeprint||g()};"undefined"!==typeof jQuery?jQuery(function(){jQuery(window).load(h)}):e(h);var g=function(){for(var a=document.getElementsByClassName("adapt-img"),b=0;bnojsPngGifProgressiveRendering){ + if (! $this->nojsPngGifProgressiveRendering) { $noscript = ""; } break; } - $length += strlen($html)+strlen($ins_style); - $ins = "\n"; + $length += strlen($html) + strlen($ins_style); + $ins = "\n"; $ins .= $noscript; $ins .= $ins_style; // insert before first