imagecopyresampled在PHP中,有人可以解释一下吗?

Ale*_*x B 3 php gd image-manipulation

好吧,我以为我理解了这个功能,但我对这个有一个完整的心理障碍.

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

imagecopyresampled函数有10个可能的参数.我第一次尝试这个:

// 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);
Run Code Online (Sandbox Code Playgroud)

这会在大图像上找到中间标记的任一侧并将其裁剪掉.或者我想.但是它确实会使作物产生一些图像,并将右侧和底部留下黑色(大概来自之前的imagecreatetruecolor).

所以我找到了一种方法来做我想要的但我希望你解释它是如何工作的.

我现在有:

//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);
Run Code Online (Sandbox Code Playgroud)

它完美地完成它,缩小图像然后裁剪出中间,但我的数学不是很尖锐,我认为这绝对是我不完全理解imagecopyresampled函数.

有人可以带我走过吗?参数参数.特别是最后两个.最初我输入了原始图像的宽度和高度,但这进入了400和400(最长边的一半).抱歉咆哮.希望我的思绪能很快理解:)

亚历克斯

Mar*_*c B 18

这很简单,记录在这里

参数:

1)$ dst_image,一个有效的GD句柄,表示你要复制的图像INTO
2)$ src_image,一个有效的GD句柄代表你正在复制的图像

3)$ dst_x - X要放置重新采样的图像到目标图像偏移
4)$ dst_y - Y轴偏移,同上

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

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

9)$从src_x -复制出$和src_image的面积的X宽度
10)$ src_y - ÿ宽度,同上

所以...

你有$ src_image是800x536,$ dst_image是75x75

       $width = 800                                $new_width = 75
+-----------------------+                        +----+
|                       |                        |    |
|                       |                        |    | $new_height = 75
|                       | $height = 536          +----+
|                       |
|                       |
+-----------------------+
Run Code Online (Sandbox Code Playgroud)

听起来你想要拍摄源图像的中间部分并从中制作缩略图,对吧?这个中间块应该代表原始图像的一半高度和宽度,因此您需要:

$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
Run Code Online (Sandbox Code Playgroud)

a,b - 开始将新图像粘贴到目标图像
c 的左上角,d - 开始从原始图像中吸取像素200,134
e,f - 使调整大小的图像75x75(填充缩略图)
g,h - 停止在原始图像中以600x402复制像素

现在,假设您希望缩略图完全填满.如果你想在源图像按比例缩小(所以它具有高度/宽度与原相同口粮,那么你就必须做一些数学来调整a,be,f参数.