如果我不知道它的尺寸,我该如何显示较大的图像?

时间:2013-01-26 10:54:49

标签: php image scale image-resizing

我有以下问题。用户可以上传图像,我想将图像显示小约5倍,而不会导致图像失真。那是我想避免的。如何找到原始图像的宽度和高度并将其除以5?

我使用php,忘了提那个细节。

的问候,卓然

3 个答案:

答案 0 :(得分:2)

从你的评论的声音,你正在寻找比你得到的答案更简单的东西。你试过getimagesize吗? http://php.net/manual/en/function.getimagesize.php

您可以这样做:

$size = getimagesize($filename);
echo $size[0]/5; //width
echo $size[1]/5; //height

此方法还具有不必依赖GD或其他任何图像库的优点。

答案 1 :(得分:0)

http://php.net/manual/en/imagick.resizeimage.php

使用FILTER_GAUSSIAN

进行调用
<?php
    $image = new Imagick( $filename );
    $imageprops = $image->getImageGeometry();
    if ($imageprops['width'] <= 200 && $imageprops['height'] <= 200) {
        // don't upscale
    } else {
        $image->resizeImage(200,200, imagick::FILTER_GAUSSIAN, 0.9, true);
    }
?>

这个想法是通过使用高斯滤波器来模糊图像,而不是对其进行二次采样。

答案 2 :(得分:0)

完成图像上传后,使用以下功能:

<?php

function generate_image_thumbnail($source_image_path, $thumbnail_image_path){
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
    switch ($source_image_type) {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif($source_image_path);
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg($source_image_path);
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng($source_image_path);
            break;
    }

    if ($source_gd_image === false) {
        return false;
    }

    $thumbnail_image_width = $source_image_width/5;
    $thumbnail_image_height = $source_image_height/5;

    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
    imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
    imagedestroy($source_gd_image);
    imagedestroy($thumbnail_gd_image);
    return true;
}
?>

将右侧参数传递给该函数,它将完成这项工作。

并确保在您的php设置中启用了GD。它使用gd库。

相关问题