Skip to content

Commit

Permalink
Image tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisnikoy committed Apr 22, 2024
1 parent 317ee64 commit 1362059
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,39 @@ public static function getFilesInDirectory(string $directory, string $extension

return $files;
}

public static function getFileFromBucket(?string $bucket, string $fileId, ?int $width, ?int $height): mixed {
global $db, $config;

if(isset($bucket)) {
$db->query("SELECT * FROM exm_storage_bucket WHERE id = ? AND bucket_id=?", $fileId, $bucket);
} else {
$db->query("SELECT * FROM exm_storage_bucket WHERE id = ?", $fileId);
}

if($db->hasRows()) {
$row = $db->getRow();

$mime = explode('/', $row->mime_type);
$extension = $mime[1];
$extension = str_replace('jpeg', 'jpg', $extension);

$filePath = $config->get('storage_dir') . $row->id . '.' . $extension;

if(file_exists($filePath)) {
header('Content-Type: ' . $row->mime_type);
if(Image::isImage($filePath) && (isset($width) || isset($height))) {
Image::resizeImage($filePath, $width ?? null, $height ?? null);
} else {
echo file_get_contents($filePath);
}
return $row;
} else {
return false;
}
} else {
return false;
}
}
}
?>
84 changes: 84 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Athos\Foundation;

/**
* Image
* Basic Image resizing
*
* @package athos-foundation
* @author Jannis Nikoy <[email protected]>
* @license MIT
* @link https://github.com/jannisnikoy/athos-foundation
*/

class Image {
public static function isImage($fileName): bool {
$type = exif_imagetype($fileName);
$supportedTypes = [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG];

return in_array($type, $supportedTypes);
}

public static function resizeImage($originalFile, $newWidth = null, $newHeight = null): bool {
$type = exif_imagetype($originalFile);

switch ($type) {
case IMAGETYPE_GIF:
$srcImg = imagecreatefromgif($originalFile);
break;
case IMAGETYPE_JPEG:
$srcImg = imagecreatefromjpeg($originalFile);
break;
case IMAGETYPE_PNG:
$srcImg = imagecreatefrompng($originalFile);
break;
}

$width = imagesx($srcImg);
$height = imagesy($srcImg);

if (isset($newWidth) && $newWidth > 0) {
$ratio = $width / $newWidth;
$newHeight = $height / $ratio;
} elseif (isset($newHeight) && $newHeight > 0) {
$ratio = $height / $newHeight;
$newWidth = $width / $ratio;
} else {
return false;
}

$newImg = imagecreatetruecolor($newWidth, $newHeight);

if ($type == IMAGETYPE_GIF || $type == IMAGETYPE_PNG) {
imagecolortransparent(
$newImg,
imagecolorallocatealpha($newImg, 0, 0, 0, 127)
);

imagealphablending($newImg, false);
imagesavealpha($newImg, true);
}

imagecopyresampled($newImg, $srcImg, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);


switch ($type) {
case IMAGETYPE_GIF:
imagegif($newImg);
break;
case IMAGETYPE_JPEG:
imagejpeg($newImg, null, 90);
break;
case IMAGETYPE_PNG:
imagepng($newImg);
break;
}

imagedestroy($srcImg);
imagedestroy($newImg);

return true;
}
}
?>

0 comments on commit 1362059

Please sign in to comment.