在保持比例的同时调整图像大小

时间:2015-10-11 08:00:25

标签: php

我在这里阅读http://php.net/manual/en/function.imagecopyresized.php,我应该使用以下代码来调整图像大小,同时保持比例:

function resizeImage($filename, $max_width, $max_height)
{
    list($orig_width, $orig_height) = getimagesize($filename);

    $width = $orig_width;
    $height = $orig_height;

    # taller
    if ($height > $max_height) {
        $width = ($max_height / $height) * $width;
        $height = $max_height;
    }

    # wider
    if ($width > $max_width) {
        $height = ($max_width / $width) * $height;
        $width = $max_width;
    }

    $image_p = imagecreatetruecolor($width, $height);

    $image = imagecreatefromjpeg($filename);

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

    return $image_p;
}

但是,我不确定我应该为参数$filename添加什么。我有这段代码$target_file = ($_FILES["fileToUpload"]["name"]);。是否可以将$target_file变量放入参数$filename进行调整大小?例如,

    $image = addslashes(file_get_contents($_FILES['fileToUpload']['tmp_name']));
    $max_width = 100; 
    $max_height = 100;
    $image = resizeImage($image, $max_width, $max_height);

这会将图片大小调整为100x100吗?

0 个答案:

没有答案