resizer.php
320 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * VikAppointments image resizer helper class. |
| 16 | * |
| 17 | * @since 1.6 |
| 18 | */ |
| 19 | abstract class VAPImageResizer |
| 20 | { |
| 21 | /** |
| 22 | * Resizes an image proportionally. |
| 23 | * |
| 24 | * @param string $fileimg The image source path. |
| 25 | * @param string $dest The destination path of the resized image. |
| 26 | * @param integer $towidth The final width. |
| 27 | * @param integer $toheight The final height. |
| 28 | * |
| 29 | * @return boolean True on success, false otherwise. |
| 30 | */ |
| 31 | public static function proportionalImage($fileimg, $dest, $towidth, $toheight) |
| 32 | { |
| 33 | if (!file_exists($fileimg)) |
| 34 | { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | if (empty($towidth) && empty($toheight)) |
| 39 | { |
| 40 | copy($fileimg, $dest); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | list($owid, $ohei, $type) = getimagesize($fileimg); |
| 45 | |
| 46 | if ($owid > $towidth || $ohei > $toheight) |
| 47 | { |
| 48 | $xscale = $owid / $towidth; |
| 49 | $yscale = $ohei / $toheight; |
| 50 | |
| 51 | if ($yscale > $xscale) |
| 52 | { |
| 53 | $new_width = round($owid * (1 / $yscale)); |
| 54 | $new_height = round($ohei * (1 / $yscale)); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | $new_width = round($owid * (1 / $xscale)); |
| 59 | $new_height = round($ohei * (1 / $xscale)); |
| 60 | } |
| 61 | |
| 62 | switch ($type) |
| 63 | { |
| 64 | case '1' : |
| 65 | $imagetmp = imagecreatefromgif($fileimg); |
| 66 | break; |
| 67 | |
| 68 | case '2' : |
| 69 | $imagetmp = imagecreatefromjpeg($fileimg); |
| 70 | break; |
| 71 | |
| 72 | default : |
| 73 | $imagetmp = imagecreatefrompng($fileimg); |
| 74 | } |
| 75 | |
| 76 | $imageresized = imagecreatetruecolor($new_width, $new_height); |
| 77 | |
| 78 | if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_PNG) |
| 79 | { |
| 80 | imagealphablending($imageresized, false); |
| 81 | imagesavealpha($imageresized ,true); |
| 82 | $transparent = imagecolorallocatealpha($imageresized, 255, 255, 255, 127); |
| 83 | imagefilledrectangle($imageresized, 0, 0, $new_width, $new_height, $transparent); |
| 84 | } |
| 85 | |
| 86 | imagecopyresampled($imageresized, $imagetmp, 0, 0, 0, 0, $new_width, $new_height, $owid, $ohei); |
| 87 | |
| 88 | switch ($type) |
| 89 | { |
| 90 | case '1' : |
| 91 | imagegif($imageresized, $dest); |
| 92 | break; |
| 93 | |
| 94 | case '2' : |
| 95 | imagejpeg($imageresized, $dest); |
| 96 | break; |
| 97 | |
| 98 | default : |
| 99 | imagealphablending($imageresized, false); |
| 100 | imagesavealpha($imageresized, true); |
| 101 | imagepng($imageresized, $dest); |
| 102 | } |
| 103 | |
| 104 | imagedestroy($imageresized); |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | copy($fileimg, $dest); |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Resizes an image with banded effect (1 rect on top and 1 rect on bottom). |
| 118 | * |
| 119 | * @param string $fileimg The image source path. |
| 120 | * @param string $dest The destination path of the resized image. |
| 121 | * @param integer $towidth The final width. |
| 122 | * @param integer $toheight The final height. |
| 123 | * @param string $rgb The RGB values (separated by a comma) of the bands. |
| 124 | * |
| 125 | * @return boolean True on success, false otherwise. |
| 126 | */ |
| 127 | public static function bandedImage($fileimg, $dest, $towidth, $toheight, $rgb = '') |
| 128 | { |
| 129 | if (!file_exists($fileimg)) |
| 130 | { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | if (empty($towidth) && empty($toheight)) |
| 135 | { |
| 136 | copy($fileimg, $dest); |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | $exp = explode(",", $rgb); |
| 141 | |
| 142 | if (count($exp) == 3) |
| 143 | { |
| 144 | $r = trim($exp[0]); |
| 145 | $g = trim($exp[1]); |
| 146 | $b = trim($exp[2]); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | $r = 0; |
| 151 | $g = 0; |
| 152 | $b = 0; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | list($owid, $ohei, $type) = getimagesize($fileimg); |
| 157 | |
| 158 | if ($owid > $towidth || $ohei > $toheight) |
| 159 | { |
| 160 | $xscale = $owid / $towidth; |
| 161 | $yscale = $ohei / $toheight; |
| 162 | |
| 163 | if ($yscale > $xscale) |
| 164 | { |
| 165 | $new_width = round($owid * (1 / $yscale)); |
| 166 | $new_height = round($ohei * (1 / $yscale)); |
| 167 | |
| 168 | $ydest = 0; |
| 169 | $diff = $towidth - $new_width; |
| 170 | $xdest = ($diff > 0 ? round($diff / 2) : 0); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | $new_width = round($owid * (1 / $xscale)); |
| 175 | $new_height = round($ohei * (1 / $xscale)); |
| 176 | |
| 177 | $xdest = 0; |
| 178 | $diff = $toheight - $new_height; |
| 179 | $ydest = ($diff > 0 ? round($diff / 2) : 0); |
| 180 | } |
| 181 | |
| 182 | $imageresized = imagecreatetruecolor($towidth, $toheight); |
| 183 | |
| 184 | $bgColor = imagecolorallocate($imageresized, (int)$r, (int)$g, (int)$b); |
| 185 | imagefill($imageresized, 0, 0, $bgColor); |
| 186 | |
| 187 | switch ($type) |
| 188 | { |
| 189 | case '1' : |
| 190 | $imagetmp = imagecreatefromgif($fileimg); |
| 191 | break; |
| 192 | |
| 193 | case '2' : |
| 194 | $imagetmp = imagecreatefromjpeg($fileimg); |
| 195 | break; |
| 196 | |
| 197 | default : |
| 198 | $imagetmp = imagecreatefrompng($fileimg); |
| 199 | } |
| 200 | |
| 201 | imagecopyresampled($imageresized, $imagetmp, $xdest, $ydest, 0, 0, $new_width, $new_height, $owid, $ohei); |
| 202 | |
| 203 | switch ($type) |
| 204 | { |
| 205 | case '1' : |
| 206 | imagegif($imageresized, $dest); |
| 207 | break; |
| 208 | |
| 209 | case '2' : |
| 210 | imagejpeg($imageresized, $dest); |
| 211 | break; |
| 212 | |
| 213 | default : |
| 214 | imagepng($imageresized, $dest); |
| 215 | } |
| 216 | |
| 217 | imagedestroy($imageresized); |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | copy($fileimg, $dest); |
| 224 | } |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Crops an image. |
| 231 | * |
| 232 | * @param string $fileimg The image source path. |
| 233 | * @param string $dest The destination path of the resized image. |
| 234 | * @param integer $towidth The final width. |
| 235 | * @param integer $toheight The final height. |
| 236 | * |
| 237 | * @return boolean True on success, false otherwise. |
| 238 | */ |
| 239 | public static function croppedImage($fileimg, $dest, $towidth, $toheight) |
| 240 | { |
| 241 | if (!file_exists($fileimg)) |
| 242 | { |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | if (empty($towidth) && empty($toheight)) |
| 247 | { |
| 248 | copy($fileimg, $dest); |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | list($owid, $ohei, $type) = getimagesize($fileimg); |
| 253 | |
| 254 | if ($owid <= $ohei) |
| 255 | { |
| 256 | $new_width = $towidth; |
| 257 | $new_height = ($towidth / $owid) * $ohei; |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | $new_height = $toheight; |
| 262 | $new_width = ($new_height / $ohei) * $owid; |
| 263 | } |
| 264 | |
| 265 | switch ($type) |
| 266 | { |
| 267 | case '1': |
| 268 | $img_src = imagecreatefromgif($fileimg); |
| 269 | $img_dest = imagecreate($new_width, $new_height); |
| 270 | break; |
| 271 | |
| 272 | case '2': |
| 273 | $img_src = imagecreatefromjpeg($fileimg); |
| 274 | $img_dest = imagecreatetruecolor($new_width, $new_height); |
| 275 | break; |
| 276 | |
| 277 | default: |
| 278 | $img_src = imagecreatefrompng($fileimg); |
| 279 | $img_dest = imagecreatetruecolor($new_width, $new_height); |
| 280 | } |
| 281 | |
| 282 | imagecopyresampled($img_dest, $img_src, 0, 0, 0, 0, $new_width, $new_height, $owid, $ohei); |
| 283 | |
| 284 | switch ($type) |
| 285 | { |
| 286 | case '1': |
| 287 | $cropped = imagecreate($towidth, $toheight); |
| 288 | break; |
| 289 | |
| 290 | case '2': |
| 291 | $cropped = imagecreatetruecolor($towidth, $toheight); |
| 292 | break; |
| 293 | |
| 294 | default: |
| 295 | $cropped = imagecreatetruecolor($towidth, $toheight); |
| 296 | } |
| 297 | |
| 298 | imagecopy($cropped, $img_dest, 0, 0, 0, 0, $owid, $ohei); |
| 299 | |
| 300 | switch ($type) |
| 301 | { |
| 302 | case '1' : |
| 303 | imagegif($cropped, $dest); |
| 304 | break; |
| 305 | |
| 306 | case '2' : |
| 307 | imagejpeg($cropped, $dest); |
| 308 | break; |
| 309 | |
| 310 | default : |
| 311 | imagepng($cropped, $dest); |
| 312 | } |
| 313 | |
| 314 | imagedestroy($img_dest); |
| 315 | imagedestroy($cropped); |
| 316 | |
| 317 | return true; |
| 318 | } |
| 319 | } |
| 320 |