图像复制重采样自动裁剪图像

发布于 2024-09-11 02:17:57 字数 1763 浏览 2 评论 0原文

我正在使用 imagecopyresampled 根据特定的宽度和高度创建缩略图。我遇到的问题是他们的身高被压扁了。我想要的是所有缩略图均为 140x84,如果它们的纵横比不匹配,则图像的顶部和底部额外部分将被裁剪到中心。

这是我到目前为止所拥有的,任何想法将不胜感激。

// Create Thumbnail

        $imgsize = getimagesize($targetFile);
    switch(strtolower(substr($targetFile, -3))){
        case "jpg":
            $image = imagecreatefromjpeg($targetFile);    
        break;
        case "png":
            $image = imagecreatefrompng($targetFile);
        break;
        case "gif":
            $image = imagecreatefromgif($targetFile);
        break;
        default:
            exit;
        break;
    }

    $width = 140; //New width of image    
    $height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions

    $x_mid = $width/2;  //horizontal middle
    $y_mid = $height/2; //vertical middle

    $src_w = $imgsize[0];
    $src_h = $imgsize[1];


    $picture = imagecreatetruecolor($width, $height);
    imagealphablending($picture, false);
    imagesavealpha($picture, true);
    $bool = imagecopyresampled($picture, $image, 0, 0, 0, ($y_mid-(84/2)), $width, $height, $src_w, $src_h); 

    if($bool){
        switch(strtolower(substr($targetFile, -3))){
            case "jpg":
                header("Content-Type: image/jpeg");
                $bool2 = imagejpeg($picture,$file_dir."/thumbs/".$imageName,85);
            break;
            case "png":
                header("Content-Type: image/png");
                imagepng($picture,$file_dir."/thumbs/".$imageName);
            break;
            case "gif":
                header("Content-Type: image/gif");
                imagegif($picture,$file_dir."/thumbs/".$imageName);
            break;
        }
    }


    imagedestroy($picture);
    imagedestroy($image);

I'm using imagecopyresampled to create thumbnails based on a certain width and height. The problem I'm having is that their height is being squished. What I want is for all thumbnail images to be 140x84 and if their aspect ratio does not match that the top and bottom extra portions of the image are cropped to center.

Here is what I have so far, any ideas would be greatly appreciated.

// Create Thumbnail

        $imgsize = getimagesize($targetFile);
    switch(strtolower(substr($targetFile, -3))){
        case "jpg":
            $image = imagecreatefromjpeg($targetFile);    
        break;
        case "png":
            $image = imagecreatefrompng($targetFile);
        break;
        case "gif":
            $image = imagecreatefromgif($targetFile);
        break;
        default:
            exit;
        break;
    }

    $width = 140; //New width of image    
    $height = $imgsize[1]/$imgsize[0]*$width; //This maintains proportions

    $x_mid = $width/2;  //horizontal middle
    $y_mid = $height/2; //vertical middle

    $src_w = $imgsize[0];
    $src_h = $imgsize[1];


    $picture = imagecreatetruecolor($width, $height);
    imagealphablending($picture, false);
    imagesavealpha($picture, true);
    $bool = imagecopyresampled($picture, $image, 0, 0, 0, ($y_mid-(84/2)), $width, $height, $src_w, $src_h); 

    if($bool){
        switch(strtolower(substr($targetFile, -3))){
            case "jpg":
                header("Content-Type: image/jpeg");
                $bool2 = imagejpeg($picture,$file_dir."/thumbs/".$imageName,85);
            break;
            case "png":
                header("Content-Type: image/png");
                imagepng($picture,$file_dir."/thumbs/".$imageName);
            break;
            case "gif":
                header("Content-Type: image/gif");
                imagegif($picture,$file_dir."/thumbs/".$imageName);
            break;
        }
    }


    imagedestroy($picture);
    imagedestroy($image);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2024-09-18 02:17:57

您需要首先确定是否要根据图像的高度或宽度重新调整图像的大小。您通常会根据原始图像是纵向还是横向以及所需图像尺寸的方向来解决此问题。然后,您需要根据您的选择以编程方式计算出另一条边。

一旦完成,您可以简单地在 (0,0) 处重新采样原始图像,并且悬垂的图像数据将被截断。

You need to first work out if you're going to re-size your image based on it's height or width. You'd usually work this out depending if your original image is portrait or landscape, and the orientation of your desired image size. You then need to work out the other edge programmatically from your choice.

Once you have, you can simple resample your original image at (0,0) and the over-hanging image data will be truncated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文