mambax7 /
publisher
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.
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace XoopsModules\Publisher; |
||
| 4 | |||
| 5 | /* |
||
| 6 | You may not change or alter any portion of this comment or credits |
||
| 7 | of supporting developers from this source code or any supporting source code |
||
| 8 | which is considered copyrighted (c) material of the original comment or credit authors. |
||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful, |
||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Image resizer class for xoops |
||
| 17 | * |
||
| 18 | * @copyright module for xoops |
||
| 19 | * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) |
||
| 20 | * @since 1.0 |
||
| 21 | * @min_xoops 2.5.9 |
||
| 22 | * @author Goffy - Wedega - Email:<[email protected]> - Website:<https://wedega.com> |
||
| 23 | * @version $Id: 1.0 Resizer.php 1 Mon 2019-02-09 10:04:49Z XOOPS Project (www.xoops.org) $ |
||
| 24 | */ |
||
| 25 | class Resizer |
||
| 26 | { |
||
| 27 | public $sourceFile = ''; |
||
| 28 | public $endFile = ''; |
||
| 29 | public $maxWidth = 0; |
||
| 30 | public $maxHeight = 0; |
||
| 31 | public $imageMimetype = ''; |
||
| 32 | public $jpgQuality = 90; |
||
| 33 | public $mergeType = 0; |
||
| 34 | public $mergePos = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * resize image if size exceed given width/height |
||
| 38 | * @return string|bool |
||
| 39 | */ |
||
| 40 | public function resizeImage() |
||
| 41 | { |
||
| 42 | // check file extension |
||
| 43 | switch ($this->imageMimetype) { |
||
| 44 | case 'image/png': |
||
| 45 | $img = \imagecreatefrompng($this->sourceFile); |
||
| 46 | break; |
||
| 47 | case 'image/jpeg': |
||
| 48 | $img = \imagecreatefromjpeg($this->sourceFile); |
||
| 49 | break; |
||
| 50 | case 'image/gif': |
||
| 51 | $img = \imagecreatefromgif($this->sourceFile); |
||
| 52 | break; |
||
| 53 | default: |
||
| 54 | return 'Unsupported format'; |
||
| 55 | } |
||
| 56 | |||
| 57 | $width = \imagesx($img); |
||
| 58 | $height = \imagesy($img); |
||
| 59 | |||
| 60 | if ($width > $this->maxWidth || $height > $this->maxHeight) { |
||
| 61 | // recalc image size based on this->maxWidth/this->maxHeight |
||
| 62 | if ($width > $height) { |
||
| 63 | if ($width < $this->maxWidth) { |
||
| 64 | $newWidth = $width; |
||
| 65 | } else { |
||
| 66 | $newWidth = $this->maxWidth; |
||
| 67 | $divisor = $width / $newWidth; |
||
| 68 | $newHeight = \floor($height / $divisor); |
||
| 69 | } |
||
| 70 | } elseif ($height < $this->maxHeight) { |
||
| 71 | $newHeight = $height; |
||
| 72 | } else { |
||
| 73 | $newHeight = $this->maxHeight; |
||
| 74 | $divisor = $height / $newHeight; |
||
| 75 | $newWidth = \floor($width / $divisor); |
||
| 76 | } |
||
| 77 | |||
| 78 | // Create a new temporary image. |
||
| 79 | $tmpimg = \imagecreatetruecolor($newWidth, $newHeight); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
|
|||
| 80 | imagealphablending($tmpimg, false); |
||
| 81 | imagesavealpha($tmpimg, true); |
||
| 82 | |||
| 83 | // Copy and resize old image into new image. |
||
| 84 | \imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); |
||
| 85 | |||
| 86 | \unlink($this->endFile); |
||
| 87 | //compressing the file |
||
| 88 | switch ($this->imageMimetype) { |
||
| 89 | case 'image/png': |
||
| 90 | \imagepng($tmpimg, $this->endFile, 0); |
||
| 91 | break; |
||
| 92 | case 'image/jpeg': |
||
| 93 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
| 94 | break; |
||
| 95 | case 'image/gif': |
||
| 96 | \imagegif($tmpimg, $this->endFile); |
||
| 97 | break; |
||
| 98 | } |
||
| 99 | |||
| 100 | // release the memory |
||
| 101 | \imagedestroy($tmpimg); |
||
| 102 | } else { |
||
| 103 | return 'copy'; |
||
| 104 | } |
||
| 105 | \imagedestroy($img); |
||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | // public function resizeAndCrop($this->sourceFile, $this->imageMimetype, $this->endFile, $this->maxWidth, $this->maxHeight, $this->jpgQuality=90) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @return bool|string |
||
| 114 | */ |
||
| 115 | public function resizeAndCrop() |
||
| 116 | { |
||
| 117 | // check file extension |
||
| 118 | switch ($this->imageMimetype) { |
||
| 119 | case 'image/png': |
||
| 120 | $original = \imagecreatefrompng($this->sourceFile); |
||
| 121 | break; |
||
| 122 | case 'image/jpeg': |
||
| 123 | $original = \imagecreatefromjpeg($this->sourceFile); |
||
| 124 | break; |
||
| 125 | case 'image/gif': |
||
| 126 | $original = \imagecreatefromgif($this->sourceFile); |
||
| 127 | break; |
||
| 128 | default: |
||
| 129 | return 'Unsupported format'; |
||
| 130 | } |
||
| 131 | |||
| 132 | if (!$original) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | // GET ORIGINAL IMAGE DIMENSIONS |
||
| 136 | [$original_w, $original_h] = \getimagesize($this->sourceFile); |
||
| 137 | |||
| 138 | // RESIZE IMAGE AND PRESERVE PROPORTIONS |
||
| 139 | $max_width_resize = $this->maxWidth; |
||
| 140 | $maxHeightResize = $this->maxHeight; |
||
| 141 | if ($original_w > $original_h) { |
||
| 142 | $max_height_ratio = $this->maxHeight / $original_h; |
||
| 143 | $max_width_resize = (int)\round($original_w * $max_height_ratio); |
||
| 144 | } else { |
||
| 145 | $max_width_ratio = $this->maxWidth / $original_w; |
||
| 146 | $maxHeightResize = (int)\round($original_h * $max_width_ratio); |
||
| 147 | } |
||
| 148 | if ($max_width_resize < $this->maxWidth) { |
||
| 149 | $max_height_ratio = $this->maxWidth / $max_width_resize; |
||
| 150 | $maxHeightResize = (int)\round($this->maxHeight * $max_height_ratio); |
||
| 151 | $max_width_resize = $this->maxWidth; |
||
| 152 | } |
||
| 153 | |||
| 154 | // CREATE THE PROPORTIONAL IMAGE RESOURCE |
||
| 155 | $thumb = \imagecreatetruecolor($max_width_resize, $maxHeightResize); |
||
| 156 | if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $maxHeightResize, $original_w, $original_h)) { |
||
| 157 | return false; |
||
| 158 | } |
||
| 159 | // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS |
||
| 160 | $final = \imagecreatetruecolor($this->maxWidth, $this->maxHeight); |
||
| 161 | |||
| 162 | $max_width_offset = 0; |
||
| 163 | $max_height_offset = 0; |
||
| 164 | if ($this->maxWidth < $max_width_resize) { |
||
| 165 | $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2); |
||
| 166 | } else { |
||
| 167 | $max_height_offset = (int)\round(($maxHeightResize - $this->maxHeight) / 2); |
||
| 168 | } |
||
| 169 | |||
| 170 | if (!\imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $maxHeightResize)) { |
||
| 171 | return false; |
||
| 172 | } |
||
| 173 | // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile |
||
| 174 | if (!\imagejpeg($final, $this->endFile, $this->jpgQuality)) { |
||
| 175 | return false; |
||
| 176 | } |
||
| 177 | |||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 181 | // public function mergeImage($this->sourceFile, $this->endFile, $this->mergePos, $this->mergeType) |
||
| 182 | public function mergeImage(): void |
||
| 183 | { |
||
| 184 | $dest = \imagecreatefromjpeg($this->endFile); |
||
| 185 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
| 186 | if (4 == $this->mergeType) { |
||
| 187 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
| 188 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 189 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
| 190 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 191 | switch ($this->mergePos) { |
||
| 192 | case 1: |
||
| 193 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 194 | break; |
||
| 195 | case 2: |
||
| 196 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 197 | break; |
||
| 198 | case 3: |
||
| 199 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 200 | break; |
||
| 201 | case 4: |
||
| 202 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 203 | break; |
||
| 204 | } |
||
| 205 | } |
||
| 206 | if (6 == $this->mergeType) { |
||
| 207 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
| 208 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 209 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
| 210 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
| 211 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 212 | |||
| 213 | switch ($this->mergePos) { |
||
| 214 | case 1: |
||
| 215 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 216 | break; |
||
| 217 | case 2: |
||
| 218 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
| 219 | break; |
||
| 220 | case 3: |
||
| 221 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 222 | break; |
||
| 223 | case 4: |
||
| 224 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 225 | break; |
||
| 226 | case 5: |
||
| 227 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
| 228 | break; |
||
| 229 | case 6: |
||
| 230 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 231 | break; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | \imagejpeg($dest, $this->endFile); |
||
| 235 | |||
| 236 | \imagedestroy($src); |
||
| 237 | \imagedestroy($dest); |
||
| 238 | } |
||
| 239 | } |
||
| 240 |