<?php
namespace EasyChat\Library;

class ImageHelper {
    public static function resize($source, $destination, $width, $height, $crop = true) {
        if (!file_exists($source)) {
            return false;
        }

        $info = getimagesize($source);
        if (!$info) {
            return false;
        }

        $mime = $info['mime'];
        switch ($mime) {
            case 'image/jpeg':
                $image = imagecreatefromjpeg($source);
                break;
            case 'image/png':
                $image = imagecreatefrompng($source);
                break;
            case 'image/gif':
                $image = imagecreatefromgif($source);
                break;
            case 'image/webp':
                if (function_exists('imagecreatefromwebp')) {
                    $image = imagecreatefromwebp($source);
                } else {
                    return false;
                }
                break;
            default:
                return false;
        }

        if (!$image) {
            return false;
        }

        $orig_width = imagesx($image);
        $orig_height = imagesy($image);

        if ($crop) {
             
            $new_image = imagecreatetruecolor($width, $height);
            
             
            if ($mime == 'image/png' || $mime == 'image/webp' || $mime == 'image/gif') {
                imagealphablending($new_image, false);
                imagesavealpha($new_image, true);
                $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
                imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
            }

            $aspect_source = $orig_width / $orig_height;
            $aspect_dest = $width / $height;

            if ($aspect_source > $aspect_dest) {
                 
                $src_h = $orig_height;
                $src_w = intval($orig_height * $aspect_dest);
                $src_x = intval(($orig_width - $src_w) / 2);
                $src_y = 0;
            } else {
                 
                $src_w = $orig_width;
                $src_h = intval($orig_width / $aspect_dest);
                $src_x = 0;
                $src_y = intval(($orig_height - $src_h) / 2);
            }

            imagecopyresampled($new_image, $image, 0, 0, $src_x, $src_y, $width, $height, $src_w, $src_h);
        } else {
             
            $aspect_source = $orig_width / $orig_height;
            if ($width / $height > $aspect_source) {
                $width = intval($height * $aspect_source);
            } else {
                $height = intval($width / $aspect_source);
            }

            $new_image = imagecreatetruecolor($width, $height);
            
             
            if ($mime == 'image/png' || $mime == 'image/webp' || $mime == 'image/gif') {
                imagealphablending($new_image, false);
                imagesavealpha($new_image, true);
                $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
                imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
            }

            imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
        }

         
        switch ($mime) {
            case 'image/jpeg':
                $result = imagejpeg($new_image, $destination, 85);
                break;
            case 'image/png':
                $result = imagepng($new_image, $destination, 6);
                break;
            case 'image/gif':
                $result = imagegif($new_image, $destination);
                break;
            case 'image/webp':
                $result = imagewebp($new_image, $destination, 80);
                break;
            default:
                $result = false;
        }

        self::destroyImage($image);
        self::destroyImage($new_image);

        return $result;
    }

    private static function destroyImage($image): void {
         
        if (function_exists('imagedestroy') && defined('PHP_VERSION_ID') && PHP_VERSION_ID < 80500) {
            @imagedestroy($image);
        }
    }
}
