douqiu1604 2018-04-12 10:39
浏览 59

使用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);
  • 写回答

1条回答 默认 最新

  • drhwb470572 2018-04-12 10:49
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染