通过 GD 裁剪图像并粘贴顶部

Posted

技术标签:

【中文标题】通过 GD 裁剪图像并粘贴顶部【英文标题】:Cropping an image via GD and sticking with the top part 【发布时间】:2013-03-31 10:11:53 【问题描述】:

我有这个脚本并且一直在试图改变它而不是裁剪图像的中心部分,而是从顶部裁剪到发送到函数的 $height,任何帮助都会非常棒:

function img_resizer($src,$quality,$w,$h,$saveas) 
    /* v2.5 with auto crop */
    $r=1;
    $e=strtolower(substr($src,strrpos($src,".")+1,3));
    if (($e == "jpg") || ($e == "peg")) 
        $OldImage=ImageCreateFromJpeg($src) or $r=0;
     elseif ($e == "gif") 
        $OldImage=ImageCreateFromGif($src) or $r=0;
     elseif ($e == "bmp") 
        $OldImage=ImageCreateFromwbmp($src) or $r=0;
     elseif ($e == "png") 
        $OldImage=ImageCreateFromPng($src) or $r=0;
     else 
        _o("Not a Valid Image! (".$e.") -- ".$src);$r=0;
    
    if ($r) 
        list($width,$height)=getimagesize($src);
        // check if ratios match
        $_ratio=array($width/$height,$w/$h);
        if ($_ratio[0] != $_ratio[1])  // crop image

            // find the right scale to use
            $_scale=min((float)($width/$w),(float)($height/$h));

            // coords to crop
            $cropX=(float)($width-($_scale*$w));
            $cropY=(float)($height-($_scale*$h));

            // cropped image size
            $cropW=(float)($width-$cropX);
            $cropH=(float)($height-$cropY);

            $crop=ImageCreateTrueColor($cropW,$cropH);
            // crop the middle part of the image to fit proportions
            ImageCopy(
                $crop,
                $OldImage,
                0,
                0,
                (int)($cropX/2),
                (int)($cropY/2),
                $cropW,
                $cropH
            );
        

        // do the thumbnail
        $NewThumb=ImageCreateTrueColor($w,$h);
        if (isset($crop))  // been cropped
            ImageCopyResampled(
                $NewThumb,
                $crop,
                0,
                0,
                0,
                0,
                $w,
                $h,
                $cropW,
                $cropH
            );
            ImageDestroy($crop);
         else  // ratio match, regular resize
            ImageCopyResampled(
                $NewThumb,
                $OldImage,
                0,
                0,
                0,
                0,
                $w,
                $h,
                $width,
                $height
            );
        
        _ckdir($saveas);
        ImageJpeg($NewThumb,$saveas);
        ImageDestroy($NewThumb);
        ImageDestroy($OldImage);
    
    return $r;

img_resizer("profile.jpg","",114,89,"profile_small.jpg");

【问题讨论】:

【参考方案1】:

好的,换一行就解决了:

        // crop from top of the image to fit proportions
        ImageCopy(
            $crop,
            $OldImage,
            0,
            0,
            (int)($cropX/2),
            (int)(0),
            $cropW,
            $cropH
        );

【讨论】:

以上是关于通过 GD 裁剪图像并粘贴顶部的主要内容,如果未能解决你的问题,请参考以下文章

UIImagePickerController 允许编辑错误地裁剪图像,在顶部留下黑条

GD图像叠加透明png

为啥我粘贴的图像在 LibreOffice Writer 中看起来被裁剪了?

ImageView 裁剪顶部和底部并固定纵横比

图像调整大小和裁剪

如何从 jpeg 的顶部和底部裁剪 45px