From 0e16b91cdabbf948423a4310691e103e6cb660ec Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Wed, 15 Feb 2023 15:19:07 +0100 Subject: [PATCH 1/5] fix: Undefined variable extension with small svg as background --- AdaptiveImages.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/AdaptiveImages.php b/AdaptiveImages.php index 70c254f..dbbadbc 100644 --- a/AdaptiveImages.php +++ b/AdaptiveImages.php @@ -12,7 +12,7 @@ class AdaptiveImages { /** - * @var Array + * @var array */ static protected $instances = array(); @@ -744,11 +744,10 @@ protected function processImgTag($img, $bkpt, $maxWidth1x, $sizes = null, $asBac } else { // Don't do anything if img filesize is to small $filesize = @filesize($src); + $extension = \pathinfo($src, \PATHINFO_EXTENSION); if ($filesize and $filesize<$this->minFileSize){ $adapt = false; } else { - $parts = pathinfo($src); - $extension = $parts['extension']; if (!in_array($extension, $this->acceptedFormats) // don't do anyting if it's an animated GIF or ($extension === "gif" and $this->isAnimatedGif($src))){ From 5b955bd229fab379f9d489f91db193ccd8477026 Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Wed, 15 Feb 2023 15:29:36 +0100 Subject: [PATCH 2/5] fix: Warning (null cast to string) --- AdaptiveImages.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/AdaptiveImages.php b/AdaptiveImages.php index dbbadbc..e67b261 100644 --- a/AdaptiveImages.php +++ b/AdaptiveImages.php @@ -717,11 +717,8 @@ protected function processImgTag($img, $bkpt, $maxWidth1x, $sizes = null, $asBac } list($w, $h) = $this->imgSize($img); - $src = $this->tagAttribute($img, 'src'); - if (!is_null($src)) { - $src = trim($src); - } - if (is_null($src) or strlen($src)<1){ + $src = trim($this->tagAttribute($img, 'src') ?? ''); + if (strlen($src)<1){ $src = $img; $img = ""; } @@ -731,7 +728,7 @@ protected function processImgTag($img, $bkpt, $maxWidth1x, $sizes = null, $asBac if (!$w or $w<=$this->minWidth1x){ $adapt = false; } else { - $srcMobile = $this->tagAttribute($img, 'data-src-mobile'); + $srcMobile = $this->tagAttribute($img, 'data-src-mobile') ?? ''; // don't do anything with data-URI images if (strncmp($src, "data:", 5)==0){ @@ -956,7 +953,7 @@ protected function lowsrcQualityOptimize($width_fallback, $lowsrcBaseQuality, $w * @return string */ protected function imgAdaptiveMarkup($img, $bkptImages, $width, $height, $extension, $maxWidth1x, $sizes = null, $asBackground = false){ - $class = $this->tagAttribute($img, "class"); + $class = $this->tagAttribute($img, "class") ?? ''; if (!$width or strpos($class, "adapt-img")!==false){ return $img; } @@ -1019,7 +1016,7 @@ protected function imgAdaptiveMarkup($img, $bkptImages, $width, $height, $extens * @return string */ protected function imgAdaptiveSrcsetMarkup($img, $fallback_file, $fallback_class, $bkptImages, $width, $height, $extension, $maxWidth1x, $maxWidthMobile = null, $sizes = null){ - $originalClass = $class = $this->tagAttribute($img, "class"); + $originalClass = $class = $this->tagAttribute($img, "class") ?? ''; $intrinsic = ""; if (strpos(" $class ", " intrinsic ")!==false){ $intrinsic = " intrinsic"; From 4481d338731ab7ed823baed5deb32f88832d4ace Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Wed, 15 Feb 2023 15:30:31 +0100 Subject: [PATCH 3/5] build: Version 4.0.0-dev with requirement on PHP >= 7.3 --- AdaptiveImages.php | 2 +- README.md | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/AdaptiveImages.php b/AdaptiveImages.php index e67b261..32b7e0f 100644 --- a/AdaptiveImages.php +++ b/AdaptiveImages.php @@ -2,7 +2,7 @@ /** * AdaptiveImages * - * @version 3.2.5 + * @version 4.0.0-dev * @copyright 2013-2022 * @author Nursit * @licence GNU/GPL3 diff --git a/README.md b/README.md index 3840ee6..b6d1d3b 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,7 @@ See for technical explanations and justifications ( ## Requirements -PHP>=5.1 with GD library -(if PHP<5.3.0 extending `AdaptiveImages` also needs to override method `getInstance()`) - +PHP >= 7.3 ## Using From 0c034c28c2cd7199dfb04196eadd898ffc1eca4f Mon Sep 17 00:00:00 2001 From: Matthieu Marcillaud Date: Wed, 15 Feb 2023 15:56:30 +0100 Subject: [PATCH 4/5] chore(cs-fix): Passe de ECS Config: ```php $ecsConfig->sets([SetList::PSR_12, SetList::SYMPLIFY, SetList::COMMON, SetList::CLEAN_CODE]); $ecsConfig->rule(NoExtraBlankLinesFixer::class); $ecsConfig->indentation('tab'); $ecsConfig->paths(['AdaptiveImages.php']); ``` --- AdaptiveImages.php | 2864 +++++++++++++++++++++++--------------------- 1 file changed, 1485 insertions(+), 1379 deletions(-) diff --git a/AdaptiveImages.php b/AdaptiveImages.php index 32b7e0f..61d9a6a 100644 --- a/AdaptiveImages.php +++ b/AdaptiveImages.php @@ -1,4 +1,5 @@ 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