diff --git a/src/Files.php b/src/Files.php index 264aea8..85a5a95 100644 --- a/src/Files.php +++ b/src/Files.php @@ -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; + } + } } ?> diff --git a/src/Image.php b/src/Image.php new file mode 100644 index 0000000..f0d1dc2 --- /dev/null +++ b/src/Image.php @@ -0,0 +1,84 @@ + +* @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; + } +} +?> \ No newline at end of file