在上传php上调整图像大小

时间:2011-01-06 06:14:43

标签: php image-processing file-upload

我有一个用于图片上传的php脚本,如下所示

<?php
$LibID = $_POST[name];
 define ("MAX_SIZE","10000");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
    $image=$_FILES['image']['name'];
    if ($image)
    {
        $filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
 if (($extension != "jpg") && ($extension != "jpeg"))
        {
            echo '<h1>Unknown extension!</h1>';
            $errors=1;
            exit();
        }
        else
        {
 $size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
    echo '<h1>You have exceeded the size limit!</h1>';
    $errors=1;
    exit();
}
$image_name=$LibID.'.'.$extension;
$newname="uimages/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
    echo '<h1>image upload unsuccessfull!</h1>';
    $errors=1;
    exit();
}}}
?>

将图像文件上传到根目录中的文件夹“uimages”。我通过定义“max-height”和“max-width”在html文件中对图像的紧凑显示进行了更改。但我想在上传时调整图像文件的大小。图像文件的最大宽度为100px,最大高度为150px。图像比例必须受到约束。也就是说,图像可能小于上述尺寸,但是,它不应超过限制。我怎样才能做到这一点?

提前致谢:)

blasteralfred ..

4 个答案:

答案 0 :(得分:1)

无法在上传时调整图片大小;它必须首先在服务器上。

为此,您可以使用ImageMagick查看Eric's answer。如果您使用的是GD库,则可以使用this

答案 1 :(得分:1)

您可以使用PHP本机GD库调整图像大小。这是我编写的用于将图像大小调整为任意大小的函数的链接。它有letterboxing或裁剪的选项,使其适合新的宽高比,并且有很好的解释。

http://www.spotlesswebdesign.com/blog.php?id=1

答案 2 :(得分:1)

上传后可以使用2个功能调整图像大小

<?php

function thumbnail($inputFileName, $maxSize = 100) {
    $info = getimagesize($inputFileName);
    $type = isset($info['type']) ? $info['type'] : $info[2];
    if (!(imagetypes() & $type)) {
        return false;
    }

    $width = isset($info['width']) ? $info['width'] : $info[0];
    $height = isset($info['height']) ? $info['height'] : $info[1];

    // Calculate aspect ratio
    $wRatio = $maxSize / $width;
    $hRatio = $maxSize / $height;

    // Using imagecreatefromstring will automatically detect the file type
    $sourceImage = imagecreatefromstring(file_get_contents($inputFileName));

    // Calculate a proportional width and height no larger than the max size.
    if (($width <= $maxSize) && ($height <= $maxSize)) {
        // Input is smaller than thumbnail, do nothing
        return $sourceImage;
    } elseif (($wRatio * $height) < $maxSize) {
        // Image is horizontal
        $tHeight = ceil($wRatio * $height);
        $tWidth = $maxSize;
    } else {
        // Image is vertical
        $tWidth = ceil($hRatio * $width);
        $tHeight = $maxSize;
    }

    $thumb = imagecreatetruecolor($tWidth, $tHeight);

    if ($sourceImage === false) {
        // Could not load image
        return false;
    }

    // Copy resampled makes a smooth thumbnail
    imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
    imagedestroy($sourceImage);

    return $thumb;
}


function imageToFile($im, $fileName, $quality = 80) {
    if (!$im || file_exists($fileName)) {
        return false;
    }

    $ext = strtolower(substr($fileName, strrpos($fileName, '.')));

    switch ($ext) {
        case '.gif':
            imagegif($im, $fileName);
            break;
        case '.jpg':
        case '.jpeg':
            imagejpeg($im, $fileName, $quality);
            break;
        case '.png':
            imagepng($im, $fileName);
            break;
        case '.bmp':
            imagewbmp($im, $fileName);
            break;
        default:
            return false;
    }
    return true;
}

?>

像这样打电话: $ image = thumbnail($ uploadedFile,300); imageToFile($ image,$ folder); //如果你想保存缩略图

答案 3 :(得分:0)

请参阅this answer,我喜欢自己使用imagemagick

相关问题