douqie6454 2012-05-01 20:29
浏览 79
已采纳

PHP图像调整大小会切断我的图像

I have this function here.....

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

And what this does it takes an image and resizes, but its not resizing the way I expected to, I was expecting it would take a wide image or a tall image or a normal size image and cut it off so it fits to that size, it does the resizing but it cuts off most of my images...I have been struggling with this for days and I cant seem to find a way to resize my images without cutting them off....I hope I can find some help and it would greatly appreciated...so, so tired....

For an example I have this image....

enter image description here

and when I ran that function for that image it returns this...

enter image description here

What I am expecting is that same image, just smaller.

I changed this part of my code...

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

changed the $x and $y to '0' and '0' and this is what came up...

enter image description here

I close to what I am looking for but the full image is not there...it still gets cut off.

  • 写回答

2条回答 默认 最新

  • dswu26846 2012-05-01 21:33
    关注

    For what you want you can use this function:

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

    However if you want to preserve the image ratio, you could use something like this:

    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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵