imagecopyresampled()
将从 position 的 width 和 height 的矩形区域中取出一个矩形区域,$src_image
并将其放置在 position 的 width$src_w
和height的$src_h
矩形区域中。($src_x, $src_y)
$dst_image
$dst_w
$dst_h
($dst_x, $dst_y)
如果源和目标坐标以及宽度和高度不同,则会对图像片段进行适当的拉伸或收缩。坐标指的是左上角。
此功能可用于复制同一图像内的区域。但是如果区域重叠,结果将是不可预测的。
- 编辑 -
如果$src_w
和$src_h
分别小于$dst_w
和$dst_h
,拇指图像将被放大。否则,它将被缩小。
<?php
$dst_x = 0; // X-coordinate of destination point
$dst_y = 0; // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // Crop end X position in original image
$src_h = 220; // Crop end Y position in original image
// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/source.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');
?>