如何使用GD图像函数裁剪图像

发布于 2024-12-02 20:59:23 字数 1576 浏览 3 评论 0原文

我的代码中的所有内容都非常适合创建上传图片的缩略图。

现在我需要做的就是将图像中心的 $thumb 裁剪成正方形(50x50)

这是我的功能,到目前为止

    $ext = end(explode('.', $_FILES['profile_photo']['name']));

    if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif')
    {
        $tmp = $_FILES['profile_photo']['tmp_name'];

        if ($ext=='jpg' || $ext=='jpeg')
            $src = imagecreatefromjpeg($tmp);
        else if ($ext=='png')
            $src = imagecreatefrompng($tmp);
        else 
            $src = imagecreatefromgif($tmp);

        list($width,$height) = getimagesize($tmp);

        $thumb_width = 50;
        $thumb_height = ($height/$width) * $thumb_width;
        $thumb_tmp = imagecreatetruecolor($thumb_width, $thumb_height);

        $full_width = 200;
        $full_height = ($height/$width) * $full_width;
        $full_tmp = imagecreatetruecolor($full_width, $full_height);

        imagecopyresampled($thumb_tmp, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);         
        imagecopyresampled($full_tmp, $src, 0, 0, 0, 0, $full_width, $full_height, $width, $height);        

        imagejpeg($thumb_tmp, 'images/profile/'.$user['id'].'_'.time().'_thumb.'.$ext, 100);
        imagejpeg($full_tmp, 'images/profile/'.$user['id'].'_'.time().'_full.'.$ext, 100);

        imagedestroy($src);
        imagedestroy($thumb_tmp);
        imagedestroy($full_tmp);

        // delete old image from server if it is not none.png
    }

任何帮助将不胜感激!我知道它与 imagecopyresampled 有关,但我无法计算出从图像中心裁剪的数学公式。我希望这是我自己的功能,所以请不要推荐我使用其他人的课程。

everything in my code is working great for creating a thumbnail image of an uploaded picture.

now all i need to do is crop the $thumb from the center of the image into a square shape (50x50)

heres my function so far

    $ext = end(explode('.', $_FILES['profile_photo']['name']));

    if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif')
    {
        $tmp = $_FILES['profile_photo']['tmp_name'];

        if ($ext=='jpg' || $ext=='jpeg')
            $src = imagecreatefromjpeg($tmp);
        else if ($ext=='png')
            $src = imagecreatefrompng($tmp);
        else 
            $src = imagecreatefromgif($tmp);

        list($width,$height) = getimagesize($tmp);

        $thumb_width = 50;
        $thumb_height = ($height/$width) * $thumb_width;
        $thumb_tmp = imagecreatetruecolor($thumb_width, $thumb_height);

        $full_width = 200;
        $full_height = ($height/$width) * $full_width;
        $full_tmp = imagecreatetruecolor($full_width, $full_height);

        imagecopyresampled($thumb_tmp, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);         
        imagecopyresampled($full_tmp, $src, 0, 0, 0, 0, $full_width, $full_height, $width, $height);        

        imagejpeg($thumb_tmp, 'images/profile/'.$user['id'].'_'.time().'_thumb.'.$ext, 100);
        imagejpeg($full_tmp, 'images/profile/'.$user['id'].'_'.time().'_full.'.$ext, 100);

        imagedestroy($src);
        imagedestroy($thumb_tmp);
        imagedestroy($full_tmp);

        // delete old image from server if it is not none.png
    }

any help would be greatly appreciated! i know that it has something to do with imagecopyresampled but i can't figure out the math for the cropping from the center of the image. i want this to be my own function so please dont recommend me using other peoples classes.

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

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

发布评论

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

评论(2

一指流沙 2024-12-09 20:59:23

$full_tmp = imagecreatetruecolor($full_width, $full_height); 之后,添加...

if ($thumb_width > $thumb_height) {
    $thumb_offset = array('x' => ($thumb_width/2 - 25), 'y' => 0);
} else {
    $thumb_offset = array('x' => 0, 'y' => ($thumb_height/2 - 25));
}

$square_tmp = imagecreatetruecolor($thumb_width, $thumb_height);

imagecopyresampled($square_tmp, $src, 0, 0, $thumb_offset['x'], $thumb_offset['y'], 50, 50, $width, $height);

然后像其他两个图像一样保存并销毁温度。

Right after $full_tmp = imagecreatetruecolor($full_width, $full_height);, add...

if ($thumb_width > $thumb_height) {
    $thumb_offset = array('x' => ($thumb_width/2 - 25), 'y' => 0);
} else {
    $thumb_offset = array('x' => 0, 'y' => ($thumb_height/2 - 25));
}

$square_tmp = imagecreatetruecolor($thumb_width, $thumb_height);

imagecopyresampled($square_tmp, $src, 0, 0, $thumb_offset['x'], $thumb_offset['y'], 50, 50, $width, $height);

Then save and destroy the temp like the other two images.

无所谓啦 2024-12-09 20:59:23

根据 PHP 手册,看一下应传递给 imagecopyresampled 的参数:

imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

从第三个参数开始,您基本上定义了源图像上的矩形如何映射到目标图像上的矩形。

因此,您要做的第一件事是计算矩形(xywidthheight),其中定义原始图像的可见区域。这些将分别是函数的第 5 个、第 6 个、第 9 个和第 10 个参数。

对于目标矩形,使用 0,0 表示 x,y,使用 $thumb_width,$thumb_height 表示 w,h code>,就像您当前正在做的那样。

Take a look at the parameters that should be passed to imagecopyresampled, as per the PHP manual:

imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

From the third parameter on, you basically define how a rectangle on the source image maps to a rectangle on the destination image.

So the first thing you have to do is calculate the rectanle (x, y, width and height) which defines the visible area of your original image. These will be the 5th, 6th, 9th and 10th parameters to the function, respectively.

For the destination rectangle, use 0,0 for x,y, and $thumb_width,$thumb_height for w,h, just as you are currently doing.

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