如何使用imagecreatetruecolor函数调整图像大小

时间:2016-11-14 12:34:45

标签: php image

我正在尝试向上移动全宽和全高图像,这是脚本 但不知道哪里出错了?

我想要上传完整的宽度和高度(原始宽度和高度)图像.. 检查下面的代码一次。此代码返回600乘400图像,但我想要原始的宽度和高度图像。

<?php
function create_image($file) {
    // Content type
    header('Content-Type: image/jpeg');
    header('Content-Type: text/html');

    // Get new sizes
    list($width, $height) = getimagesize($file['tmp_name']);

    // Load
    $thumb = imagecreatetruecolor(600, 400);

    $wmext = explode(".",$file['name']);
    $wmext = end($wmext);

    switch ($wmext) {
        case 'jpg' :
            $source = imagecreatefromjpeg($file['tmp_name']);
            break;
        case 'png' :
            $source = imagecreatefrompng($file['tmp_name']);
            break;
        case 'gif' :
            $source = imagecreatefromgif($file['tmp_name']);
            break;
        default :
//          progress_log("Resize error for image : $file");
    }

    // Resize
    imagecopyresized($thumb, $source, 0, 0, 0, 0, 600, 400, $width, $height);

    $final_file = "Thumb_".time()."_".$file['name'];
    imagejpeg($thumb,"uploads/".$final_file);

    //echo '<img src="uploads/'.$final_file.'">';
    imagedestroy($thumb);
    return $final_file;
}
?>

1 个答案:

答案 0 :(得分:1)

imagecopyresized($thumb, $source, 0, 0, 0, 0, 600, 400, $width, $height);

你的$ width和$ height应该替换为图像的原始宽度和高度(宽度和高度,你想要调整大小的图像的方式)。

虽然我不确定你为什么想要调整图像到原始大小。这是有道理的。