0

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);
MarcM
  • 1,750
  • 17
  • 27
  • `is it possible to make my program take any image and resize/rescale it to 16:9` yes. check out: https://stackoverflow.com/questions/29063094/php-fit-any-size-image-to-169-aspect-ratio that solution may be of help to you. `without making the image look weird?` Now this is the difficult question and yes it's possible but I assume you'd need to check the original pictures aspect ratio. – IsThisJavascript Apr 12 '18 at 10:44
  • Depends on how precious you are about automatically cropping the image I guess; if you don't mind people losing heads when you crop from 4:3 to 16:9 or something, then I'd imagine it's possible, yes. – CD001 Apr 12 '18 at 10:44
  • Possible duplicate of [Resize image in PHP](https://stackoverflow.com/questions/14649645/resize-image-in-php) –  Apr 12 '18 at 11:51

1 Answers1

1

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.

MarcM
  • 1,750
  • 17
  • 27