emmanuelroecker /
php-lazyload-img
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Lazy loading images |
||
| 5 | * |
||
| 6 | * PHP version 5.4 |
||
| 7 | * |
||
| 8 | * @category GLICER |
||
| 9 | * @package Contact |
||
| 10 | * @author Emmanuel ROECKER <[email protected]> |
||
| 11 | * @author Rym BOUCHAGOUR <[email protected]> |
||
| 12 | * @copyright 2012-2013 GLICER |
||
| 13 | * @license Proprietary property of GLICER |
||
| 14 | * @link http://www.glicer.com |
||
| 15 | * |
||
| 16 | * Created : 8/26/16 |
||
| 17 | * File : GlLazyLoadImg.php |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | namespace GlLazyLoadImg; |
||
| 22 | |||
| 23 | use GlHtml\GlHtml; |
||
| 24 | |||
| 25 | class GlLazyLoadImg |
||
| 26 | { |
||
| 27 | const BLANK = 0; |
||
| 28 | const LOSSY = 1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string rootpath |
||
| 32 | */ |
||
| 33 | private $rootpath; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $moveToAttribute; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $excludeAttributesList; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * constructor - set root directory to relative url |
||
| 47 | * |
||
| 48 | * @param string $rootpath |
||
| 49 | * @param int $type |
||
| 50 | * @param string $moveToAttribute |
||
| 51 | * @param array $excludeAttributesList |
||
| 52 | */ |
||
| 53 | public function __construct( |
||
| 54 | $rootpath, |
||
| 55 | $type = self::BLANK, |
||
| 56 | $moveToAttribute = 'data-original', |
||
| 57 | array $excludeAttributesList = [] |
||
| 58 | ) { |
||
| 59 | $this->rootpath = $rootpath; |
||
| 60 | $this->type = $type; |
||
|
0 ignored issues
–
show
|
|||
| 61 | $this->moveToAttribute = $moveToAttribute; |
||
| 62 | $this->excludeAttributesList = $excludeAttributesList; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | private function gcd($a,$b) { |
||
| 67 | return ($a % $b) ? $this->gcd($b,$a % $b) : $b; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * create lossy gif image and encode to data uri format |
||
| 72 | * minimal size is jpeg |
||
| 73 | * |
||
| 74 | * @param $src resource GD library |
||
| 75 | * @param int $minwidth min width in pixels (height autocalculte) |
||
| 76 | * |
||
| 77 | * @return string data uri format |
||
| 78 | */ |
||
| 79 | public function getLossyGifDataURI($src, $minwidth = 75) |
||
| 80 | { |
||
| 81 | $src = imagescale($src, $minwidth, -1, IMG_NEAREST_NEIGHBOUR); |
||
| 82 | |||
| 83 | ob_start(); |
||
| 84 | imagegif($src); |
||
| 85 | $data = ob_get_contents(); |
||
| 86 | ob_end_clean(); |
||
| 87 | |||
| 88 | imagedestroy($src); |
||
| 89 | |||
| 90 | $base64 = base64_encode($data); |
||
| 91 | |||
| 92 | $mime = 'image/gif'; |
||
| 93 | |||
| 94 | return ('data:' . $mime . ';base64,' . $base64); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * create blank image with same size in data uri format |
||
| 99 | * minimal size is gif |
||
| 100 | * |
||
| 101 | * @param $src |
||
| 102 | * @param int $red red component background color (default 255) |
||
| 103 | * @param int $green green component background color (default 255) |
||
| 104 | * @param int $blue blue component background color (default 255) |
||
| 105 | * |
||
| 106 | * @return string data uri format |
||
| 107 | */ |
||
| 108 | public function getMinGifDataURI($src, $red = 255, $green = 255, $blue = 255) |
||
| 109 | { |
||
| 110 | $width = imagesx($src); |
||
| 111 | $height = imagesy($src); |
||
| 112 | |||
| 113 | $gcd = $this->gcd($width, $height); |
||
| 114 | $width = $width / $gcd; |
||
| 115 | $height = $height / $gcd; |
||
| 116 | |||
| 117 | $img = imagecreatetruecolor($width, $height); |
||
| 118 | $bgcol = imagecolorallocate($img, $red, $green, $blue); |
||
| 119 | imageFill($img, 0, 0, $bgcol); |
||
| 120 | |||
| 121 | ob_start(); |
||
| 122 | imagegif($img); |
||
| 123 | $data = ob_get_contents(); |
||
| 124 | ob_end_clean(); |
||
| 125 | |||
| 126 | $base64 = base64_encode($data); |
||
| 127 | |||
| 128 | imagedestroy($img); |
||
| 129 | |||
| 130 | $mime = 'image/gif'; |
||
| 131 | |||
| 132 | return ('data:' . $mime . ';base64,' . $base64); |
||
| 133 | } |
||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * find type of image and open it |
||
| 138 | * |
||
| 139 | * @param string $file |
||
| 140 | * |
||
| 141 | * @return bool|resource |
||
| 142 | */ |
||
| 143 | private function openImage($file) |
||
| 144 | { |
||
| 145 | if (!file_exists($file)) { |
||
| 146 | return false; |
||
| 147 | } |
||
| 148 | |||
| 149 | $size = getimagesize($file); |
||
| 150 | switch ($size["mime"]) { |
||
| 151 | case "image/jpeg": |
||
| 152 | $im = imagecreatefromjpeg($file); |
||
| 153 | break; |
||
| 154 | case "image/gif": |
||
| 155 | $im = imagecreatefromgif($file); |
||
| 156 | break; |
||
| 157 | case "image/png": |
||
| 158 | $im = imagecreatefrompng($file); |
||
| 159 | break; |
||
| 160 | default: |
||
| 161 | $im = false; |
||
| 162 | break; |
||
| 163 | } |
||
| 164 | |||
| 165 | return $im; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * replace all src attributes from img tags with datauri and set another attribute with old src value |
||
| 170 | * support jpeg, png or gif file format |
||
| 171 | * |
||
| 172 | * @param string $html |
||
| 173 | * |
||
| 174 | * @throws \Exception |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function autoDataURI($html) |
||
| 178 | { |
||
| 179 | $html = new GlHtml($html); |
||
| 180 | $imgs = $html->get('img'); |
||
| 181 | foreach ($imgs as $img) { |
||
| 182 | $src = $img->getAttribute('src'); |
||
| 183 | $pathimagesrc = $this->rootpath . '/' . $src; |
||
| 184 | |||
| 185 | $imgbin = $this->openImage($pathimagesrc); |
||
| 186 | if ($imgbin) { |
||
| 187 | switch ($this->type) { |
||
| 188 | case self::BLANK: |
||
| 189 | $datauri = $this->getMinGifDataURI($imgbin); |
||
| 190 | break; |
||
| 191 | case self::LOSSY: |
||
| 192 | $datauri = $this->getLossyGifDataURI($imgbin); |
||
| 193 | break; |
||
| 194 | default: |
||
| 195 | throw new \Exception("Type unknown (only self::BLANK=0 or self::LOSSY=1 accepted) : " . $this->type); |
||
| 196 | } |
||
| 197 | |||
| 198 | $width = imagesx($imgbin); |
||
| 199 | $height = imagesy($imgbin); |
||
| 200 | $img->setAttributes(['width' => $width, 'height' => $height]); |
||
| 201 | |||
| 202 | if (!$img->hasAttributes($this->excludeAttributesList)) { |
||
| 203 | $img->setAttributes([$this->moveToAttribute => $src, 'src' => $datauri]); |
||
| 204 | } |
||
| 205 | imagedestroy($imgbin); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | return $html->html(); |
||
| 210 | } |
||
| 211 | } |
||
| 212 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: