imagecopy在PHP中重新采样,有人可以解释吗? [英] imagecopyresampled in PHP, can someone explain it?

查看:104
本文介绍了imagecopy在PHP中重新采样,有人可以解释吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我以为我了解此功能,但是我对此功能有一个完整的认识.

OK i thought i understood this function but i have a complete mental block on this one.

我想从800x536的照片中创建尺寸为75x75的裁剪缩略图.

I wanted to create cropped thumbnails of size 75x75 from photos that are 800x536.

imagecopyresampled函数具有10个可能的参数.我首先尝试了这个:

the imagecopyresampled function has 10 possible parameters. i first tried this:

// Starting point of crop
        $tlx = floor(($width / 2) - ($new_width / 2)); //finds halfway point of big image and subtracts half of thumb.
        $tly = floor(($height / 2) - ($new_height / 2)); //gets centre of image to be cropped.

imagecopyresampled($tmp_img,$img,0,0,$tlx,$tly,$new_width,$new_height,$orig_width,$orig_height);

这会在大图像上找到中途标记的任一侧并将其裁剪掉.还是我想.但实际上会裁切一些图片,并在右侧和底部留出黑色(大概是从imagecreatetruecolor开始的.

this finds either side of the halfway mark on the large image and crops it out. or so i thought. but it actuall crops a bit of the picture and leaves the right hand side and bottom in black (presumably from the imagecreatetruecolor earlier.

所以我找到了一种方法来做我想做的事,但我想让你解释一下它是如何工作的.

so i found a way to do what i want but i want you to explain how it is working.

我现在有:

//Create thumbnails.
            $new_width = 75; //pixels.
            $new_height = 75;

            if($width > $height) $biggest_side = $width;   
            else $biggest_side = $height;   

            //The crop size will be half that of the largest side   
            $crop_percent = .5;   
            $crop_width   = $biggest_side*$crop_percent;   
            $crop_height  = $biggest_side*$crop_percent;

            $c1 = array("x"=>($width-$crop_width)/2, "y"=>($height-$crop_height)/2);

        //Create new image with new dimensions to hold thumb
        $tmp_img = imagecreatetruecolor($new_width,$new_height);

        //Copy and resample original image into new image.
            imagecopyresampled($tmp_img,$img,0,0,$c1['x'],$c1['y'],$new_width,$new_height,$crop_width,$crop_height);

它做得很完美,缩小了图像,然后裁剪出中间部分,但是我的数学不是很敏锐,而且我想当然是我不完全了解imagecopyresampled函数.

it's doing it perfectly, shrinking the image and then cropping out the middle, but my maths isn't very sharp and also i think it's definitely that i don't fully understand the imagecopyresampled function.

有人可以引导我通过它吗?逐个参数.特别是最后两个.最初,我输入了原始图像的宽度和高度,但这输入了400和400(最长边的一半).对不起,咆哮.希望我的头脑很快理解这一点:)

can someone walk me through it? parameter by parameter. especially the last two. originally i entered the width and height of the original image, but this enters 400 and 400 (half of the longest side). sorry for the rant. hope my mind understands this soon :)

亚历克斯

推荐答案

此处

参数:

1)$ dst_image,一个有效的GD句柄,代表要复制到INTO的图像
2)$ src_image,有效的GD句柄,用于重新呈现要复制的图像

1) $dst_image, a valid GD handle representing the image you want to copy INTO
2) $src_image, a valid GD Handle represending the image you're copying FROM

3)$ dst_x-您要将重采样后的图像放入目标图像中的X偏移量
4)$ dst_y-Y偏移量,同上

3) $dst_x - X offset in the destination image you want to place the resampled image into
4) $dst_y - Y offset, ditto

5)$ src_x-您要开始复制的源图像中的X偏移量
6)$ src_y-Y偏移量,同上

5) $src_x - X offset in the source image you want to start copying from
6) $src_y - Y offset, ditto

7)$ dst_x-$ dst_image中新重新采样的图像的X宽度
8)$ dst_y-Y宽度,同上

7) $dst_x - X width of the newly resampled image in $dst_image
8) $dst_y - Y width, ditto

9)$ src_x-要复制到$ src_image中的区域的X宽度
10)$ src_y-Y宽度,同上

9) $src_x - X width of the area to copy out of the $src_image
10) $src_y - Y width, ditto

所以...

您有一个800x536的$ src_image和一个75x75的$ dst_image

You've got a $src_image that's 800x536, and a $dst_image that's 75x75

       $width = 800                                $new_width = 75
+-----------------------+                        +----+
|                       |                        |    |
|                       |                        |    | $new_height = 75
|                       | $height = 536          +----+
|                       |
|                       |
+-----------------------+

听起来像是要获取源图像的​​中间部分并以此为缩略图,对吗?这个中间的块应该代表高度的一半.原始图像的宽度,因此需要:

Sounds like you want to take the middle chunk of the source image and make a thumbnail from that, right? This middle chunk should represent half the height & width of the original image, so you want:

$start_X = floor($width / 4); //  200
$width_Y = floor($height / 4); // 134

  200     400      200       
+-----------------------+
|     |          |      | 134
|-----+----------+------|
|     | This part|      | 268
|-----+----------+------|
|     |          |      | 134
+-----------------------+

$end_x = $start_X + (2 * $start_x) // 3 * $start_x = 600
$end_y = $start_Y + (2 * $start_y) // 3 * $start_y = 402

imagecopyresampled($src, $dst, 0, 0, $startX, $start_y, 75, 75, $end_x, $end_y);
                               a  b  c        d         e   f   g       h

a,b-开始将新图像粘贴到目标图像的左上方
c,d-从200,134开始从原始图像中吸取像素
e,f-将尺寸调整为75x75的图片(填满缩略图)
g,h-停止复制原始图像中600x402的像素

a,b - start pasting the new image into the top-left of the destination image
c,d - start sucking pixels out of the original image at 200,134
e,f - make the resized image 75x75 (fill up the thumbnail)
g,h - stop copying pixels at 600x402 in the original image

现在,这是假设您希望缩略图完全填满.如果要按比例缩小源图像(因此它具有与原始图像相同的高/宽比,则必须做一些数学运算来调整a,be,f参数.

Now, this is assuming that you want the thumbnail to be completely filled up. If you want the source image to be shrunk proportionally (so it has the same ration of height/width as the original, then you'll have to do some math to adjust the a,b and e,f parameters.

这篇关于imagecopy在PHP中重新采样,有人可以解释吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆