Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP图像调整将切断我的图像_Php_Resize - Fatal编程技术网

PHP图像调整将切断我的图像

PHP图像调整将切断我的图像,php,resize,Php,Resize,我这里有这个函数 function create_thumbnail($source,$destination, $thumb_width) { $size = getimagesize($source); $width = $size[0]; $height = $size[1]; $x = 0; $y = 0; if($width> $height) { $x = c

我这里有这个函数

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);
将$x和$y更改为“0”和“0”,这就是出现的结果


我接近我要查找的内容,但完整图像不在那里…它仍然会被切断。

要了解您想要了解的内容,您可以使用此功能:

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'));

为了满足您的需要,您可以使用此功能:

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'));
其他解决方案:

/** *从源创建jpg缩略图 **/ 函数create_缩略图$source、$destination、$thumbWidth{ 如果$info=getimagesize$source==FALSE 返回null; 切换$info[2]{ case IMAGETYPE_GIF:$src=imagecreatefromfromgif$source;break; case IMAGETYPE_JPEG:$src=imagecreatefromjpeg$source;break; case IMAGETYPE_PNG:$src=imagecreatefrompng$source;break; 默认为空; } $width=$info[0]; $height=$info[1]; $widthDst=$thumbWidth; $heightDst=intval$height*$thumbWidth/$width; $tmp=ImageCreateTureColor$widthDst、$heightDst; imagecopyresampled$tmp、$src、0、0、0、$widthDst、$heightDst、$width、$height; 图像jpeg$tmp$destination; } 其他解决方案:

/** *从源创建jpg缩略图 **/ 函数create_缩略图$source、$destination、$thumbWidth{ 如果$info=getimagesize$source==FALSE 返回null; 切换$info[2]{ case IMAGETYPE_GIF:$src=imagecreatefromfromgif$source;break; case IMAGETYPE_JPEG:$src=imagecreatefromjpeg$source;break; case IMAGETYPE_PNG:$src=imagecreatefrompng$source;break; 默认为空; } $width=$info[0]; $height=$info[1]; $widthDst=$thumbWidth; $heightDst=intval$height*$thumbWidth/$width; $tmp=ImageCreateTureColor$widthDst、$heightDst; imagecopyresampled$tmp、$src、0、0、0、$widthDst、$heightDst、$width、$height; 图像jpeg$tmp$destination; }
请给出一些示例,说明调整大小/剪切应如何准确地工作ImageCopyResampled$new_image,$image,0,0,0…imagecopysampled$new_image,$image,0,0,0,0想法不起作用:您正在尝试重新调整图像大小,但目标比率与原始图像不一样。这是一个问题还是一个陈述?请给出一些示例,说明调整大小/剪切应如何工作ImageCopyResampled$new_image,$image,0,0,0,0…imagecopysampled$new_image,$image,0,0,0,0想法不起作用:您正在尝试重新调整图像大小,但目标比率与原始图像不同。这是一个问题还是一个陈述?