Updated the proostekrant to work properly again.
This commit is contained in:
Executable
+128
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
namespace App\Helpers;
|
||||
|
||||
class ImageHelper {
|
||||
|
||||
public static function resizeImage($source, $target, $maxWidth, $maxHeight, $quality = 90) {
|
||||
if (!file_exists($source)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$imageInfo = getimagesize($source);
|
||||
if ($imageInfo === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
list($width, $height, $type) = $imageInfo;
|
||||
|
||||
// If image is smaller than max dimensions, just copy it
|
||||
if ($width <= $maxWidth && $height <= $maxHeight) {
|
||||
return copy($source, $target);
|
||||
}
|
||||
|
||||
// Calculate new dimensions maintaining aspect ratio
|
||||
$ratio = min($maxWidth / $width, $maxHeight / $height);
|
||||
$newWidth = (int)($width * $ratio);
|
||||
$newHeight = (int)($height * $ratio);
|
||||
|
||||
// Create source image resource
|
||||
$sourceImage = null;
|
||||
switch ($type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
$sourceImage = imagecreatefromjpeg($source);
|
||||
break;
|
||||
case IMAGETYPE_PNG:
|
||||
$sourceImage = imagecreatefrompng($source);
|
||||
break;
|
||||
case IMAGETYPE_GIF:
|
||||
$sourceImage = imagecreatefromgif($source);
|
||||
break;
|
||||
case IMAGETYPE_WEBP:
|
||||
$sourceImage = imagecreatefromwebp($source);
|
||||
break;
|
||||
default:
|
||||
return copy($source, $target);
|
||||
}
|
||||
|
||||
if (!$sourceImage) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create destination image
|
||||
$destinationImage = imagecreatetruecolor($newWidth, $newHeight);
|
||||
|
||||
// Preserve transparency for PNG and GIF
|
||||
if ($type === IMAGETYPE_PNG || $type === IMAGETYPE_GIF) {
|
||||
imagealphablending($destinationImage, false);
|
||||
imagesavealpha($destinationImage, true);
|
||||
$transparent = imagecolorallocatealpha($destinationImage, 255, 255, 255, 127);
|
||||
imagefilledrectangle($destinationImage, 0, 0, $newWidth, $newHeight, $transparent);
|
||||
}
|
||||
|
||||
// Resample image
|
||||
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
|
||||
|
||||
// Save image
|
||||
switch ($type) {
|
||||
case IMAGETYPE_JPEG:
|
||||
return imagejpeg($destinationImage, $target, $quality);
|
||||
case IMAGETYPE_PNG:
|
||||
return imagepng($destinationImage, $target, 9);
|
||||
case IMAGETYPE_GIF:
|
||||
return imagegif($destinationImage, $target);
|
||||
case IMAGETYPE_WEBP:
|
||||
return imagewebp($destinationImage, $target, $quality);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function isImagePortrait($imagePath) {
|
||||
if (!file_exists($imagePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$imageInfo = getimagesize($imagePath);
|
||||
if ($imageInfo === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
list($width, $height) = $imageInfo;
|
||||
return $height > $width;
|
||||
}
|
||||
|
||||
public static function isImageLandscape($imagePath) {
|
||||
if (!file_exists($imagePath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$imageInfo = getimagesize($imagePath);
|
||||
if ($imageInfo === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
list($width, $height) = $imageInfo;
|
||||
return $width > $height;
|
||||
}
|
||||
|
||||
public static function getImageOrientation($imagePath) {
|
||||
if (!file_exists($imagePath)) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
$imageInfo = getimagesize($imagePath);
|
||||
if ($imageInfo === false) {
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
list($width, $height) = $imageInfo;
|
||||
|
||||
if ($width > $height) {
|
||||
return 'landscape';
|
||||
} elseif ($height > $width) {
|
||||
return 'portrait';
|
||||
} else {
|
||||
return 'square';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user