使用php将任何给定图像缩放/调整为16:9

I have restricted my program to accept only images with a 16:9 ratio by basically getting image size and seeing if width/height = 16:9.

Is it possible to make my program take any image and resize/rescale it to 16:9 without making the image look weird?

This is my code:

    global $config;

    $tempFile = $_FILES['file']['tmp_name'];
    $mimeType = mime_content_type($tempFile);

    $fileName = md5(uniqid());

    switch($mimeType){
        case "image/jpeg":
            $filePath = "images/";
            $extension = IMAGETYPE_JPEG;
            break;
        case "image/png":
            $filePath = "images/";
            $extension = IMAGETYPE_PNG;
            break;
        case "video/mp4":
            $filePath = "videos/";
            $extension = ".mp4";
            break;
    }


    $targetFile = $config['GENERAL']['UPLOAD_PATH'].$filePath.$fileName.$extension;


    move_uploaded_file($tempFile,$targetFile);

is it possible to make my program take any image and resize/rescale it to 16:9 without making the image look weird?

Yes, it is. I suggest you to use ImageMagik native PHP extension.

You can achive your goal by:

  1. Finding out current image size (Imagick::getSize())
  2. Do some basic maths to find out desired new size of the image.
  3. Crop image to new desired size (Imagick::cropImage())

Note about your concern:

...without making the image look weird?

...well, this depends on the image and the crop. Keep in mind that croping any image is a destructive operation since you are 'discarding' part of the image. If croping is not valid for you (yes, images might look weird) consider instead fiting the whole original image in a 16:9 bigger canvas (then, you will have blank margins on the left/right or top/bottom). Also achievable by using ImageMagik.