1

我这里有这个功能.....

function create_thumbnail($source,$destination, $thumb_width) {
        $size = getimagesize($source);
        $width = $size[0];
        $height = $size[1];
        $x = 0;
        $y = 0;
        if($width> $height) {
            $x = ceil(($width - $height) / 2 );
            $width = $height;
        } elseif($height> $width) {
            $y = ceil(($height - $width) / 2);
            $height = $width;
        }
        $new_image = imagecreatetruecolor($thumb_width,$thumb_width)or die('Cannot Initialize new GD image stream');
        $extension = get_image_extension($source);
         if($extension=='jpg' || $extension=='jpeg') 
            $image = imagecreatefromjpeg($source); 
        if($extension=='gif') 
            $image = imagecreatefromgif($source); 
        if($extension=='png') 
            $image = imagecreatefrompng($source);   

        imagecopyresampled($new_image,$image,0,0,$x,$y,$thumb_width,$thumb_width,$width,$height);
        if($extension=='jpg' || $extension=='jpeg') 
           imagejpeg($new_image,$destination); 
        if($extension=='gif') 
            imagegif($new_image,$destination); 
        if($extension=='png') 
            imagepng($new_image,$destination); 
    }

这是做什么的,它需要一张图像并调整大小,但它并没有像我预期的那样调整大小,我期待它会拍摄一张宽幅图像或一张高大图像或一个正常尺寸的图像并将其剪掉,使其适合该尺寸,它可以调整大小,但它会切断我的大部分图像......我已经为此苦苦挣扎了好几天,我似乎无法找到一种方法来调整我的图像大小而不切断它们......我希望我能找到一些帮助它会非常感激......所以,很累......

例如,我有这张图片....

在此处输入图像描述

当我为该图像运行该函数时,它返回此...

在此处输入图像描述

我期待的是相同的图像,只是更小。

我更改了这部分代码...

imagecopyresampled($new_image,$image,0,0,0,0,$thumb_width,$thumb_width,$width,$height);

$xand更改$y为“0”和“0”,这就是出现的...

在此处输入图像描述

我接近我正在寻找的东西,但完整的图像不存在......它仍然被切断。

4

2 回答 2

3

对于您想要的,您可以使用此功能:

function create_thumbnail($source, $destination, $thumbWidth)
{
    $extension = get_image_extension($source);
    $size = getimagesize($source);
    $imageWidth  = $newWidth  = $size[0];
    $imageHeight = $newheight = $size[1];

    if ($imageWidth > $thumbWidth || $imageHeight > $thumbWidth)
    {
        $newWidth  = $newHeight = $thumbWidth;
    }

    $newImage = imagecreatetruecolor($newWidth, $newHeight);

    switch ($extension)
    {
        case 'jpeg':
        case 'jpg':
            $imageCreateFrom = 'imagecreatefromjpeg';
            $store = 'imagejpeg';
            break;

        case 'png':
            $imageCreateFrom = 'imagecreatefrompng';
            $store = 'imagepng';
            break;

        case 'gif':
            $imageCreateFrom = 'imagecreatefromgif';
            $store = 'imagegif';
            break;

        default:
            return false;
    }

    $container = $imageCreateFrom($source);
    imagecopyresampled($newImage, $container, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
    return $store($newImage, $destination);
}
var_dump(create_thumbnail('sample.jpg', 'sample_thumb_no_ratio.jpg', '255'));

但是,如果你想保留图像比例,你可以使用这样的东西:

function create_thumbnail_preserve_ratio($source, $destination, $thumbWidth)
{
    $extension = get_image_extension($source);
    $size = getimagesize($source);
    $imageWidth  = $newWidth  = $size[0];
    $imageHeight = $newheight = $size[1];

    if ($imageWidth > $thumbWidth || $imageHeight > $thumbWidth)
    {
        // Calculate the ratio
        $xscale = ($imageWidth/$thumbWidth);
        $yscale = ($imageHeight/$thumbWidth);
        $newWidth  = ($yscale > $xscale) ? round($imageWidth * (1/$yscale)) : round($imageWidth * (1/$xscale));
        $newHeight = ($yscale > $xscale) ? round($imageHeight * (1/$yscale)) : round($imageHeight * (1/$xscale));
    }

    $newImage = imagecreatetruecolor($newWidth, $newHeight);

    switch ($extension)
    {
        case 'jpeg':
        case 'jpg':
            $imageCreateFrom = 'imagecreatefromjpeg';
            $store = 'imagejpeg';
            break;

        case 'png':
            $imageCreateFrom = 'imagecreatefrompng';
            $store = 'imagepng';
            break;

        case 'gif':
            $imageCreateFrom = 'imagecreatefromgif';
            $store = 'imagegif';
            break;

        default:
            return false;
    }

    $container = $imageCreateFrom($source);
    imagecopyresampled($newImage, $container, 0, 0, 0, 0, $newWidth, $newHeight, $imageWidth, $imageHeight);
    return $store($newImage, $destination);
}
var_dump(create_thumbnail_preserve_ratio('sample.jpg', 'sample_thumb_with_ratio.jpg', '255'));
于 2012-05-01T21:33:38.167 回答
1

其他解决方案:

/**
 * Create a jpg thumbnail from a source
 **/
function create_thumbnail($source, $destination, $thumbWidth) {
    if (($info = getimagesize($source)) === FALSE)
        return null;
    switch ($info[2]) {
        case IMAGETYPE_GIF  : $src = imagecreatefromgif($source);  break;
        case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($source); break;
        case IMAGETYPE_PNG  : $src = imagecreatefrompng($source);  break;
        default : null;
    }
    $width = $info[0];
    $height = $info[1];
    $widthDst = $thumbWidth;
    $heightDst = intval( $height * $thumbWidth / $width);
    $tmp = imagecreatetruecolor($widthDst, $heightDst);
    imagecopyresampled($tmp, $src, 0, 0, 0, 0, $widthDst, $heightDst, $width, $height);
    imagejpeg($tmp, $destination);
}
于 2018-03-01T08:09:11.103 回答