2010-08-30 78 views
1

好吧,我以为我明白这个功能,但我对这一个完整的心理障碍。imagecopy在PHP中进行采样,有人可以解释它吗?

我想从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); 

这发现在大图像的中途标记的任何一方,并裁剪出来。或者我想。但它实际上是作物的一部分图片,并将右侧和底部留在黑色(大概是从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); 

这是完全这样做,缩小图像,然后裁剪出来的中间,但我的数学是不是很锋利,还我认为这绝对是我不充分了解imagecopyresampled功能。

有人可以通过它走过我吗?参数按参数。特别是最后两个。最初我输入了原始图像的宽度和高度,但是这会进入400和400(最长边的一半)。对咆哮抱歉。希望我的心中明白这一点很快:)

亚历

回答

16

这是相当简单的,记录here

参数:

1)$ dst_image和,表示要复制图像的有效GD手柄INTO
2)$ src_image,一个有效的GD句柄,表示要复制的图像从

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

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

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

9)$从src_x - 的区域的X宽度复制出的$和src_image
10)$ src_y - Y宽度,同上

所以......

你已经有了一个$ src_image分别这是800x536,和$ dst_image和那75x75

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

听起来像是你要取源图像的​​中间块,并作出从那个缩略图,对吧?这中间的大块应该代表原始图像的高度&宽度的一半,所以你想:

$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的 - 开始在200134
Ë吸吮像素出原始图像的,F - 使调整后的图像75x75(填满缩略图)
G,H - 原始图像现在

在停止复制像素为600x402,这是假设你想让缩略图完全填满。如果你想让源图像按比例缩小(因此它与原始图像具有相同的高度/宽度比率,那么你将不得不做一些数学来调整参数a,be,f

+0

哇非常感谢。因此,您使用end_x和end_y参数作为停止坐标,当手册(我已经看了很多)说明它是宽度和高度时现在,我现在使用的是,在end_x和end_y中工作的是400x400,所以这意味着它需要从中心切出400x400,然后缩小到75x75? – 2010-08-30 23:59:43

+0

,当它显示end_x/end_y的“源图像”时,它就是您想要工作的源图像的PIECE的结尾用。如果你想缩小整个图像,那么它就是原始图像的高度/宽度。否则,它只是您正在切出的块的高度/宽度。 – 2010-08-31 01:00:00

+0

对,我即将缩小整个图像的400片400片,然后取出中间75x75平方米的图像?我想我明白了。好哇! – 2010-08-31 01:41:29

相关问题