imagecopyresampled()
bool 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 )
I've never done this before, so hopefully my logic is sound...
Create the $dst_image as being square. $dst_x and $dst_y would be the top left of the image (0,0). $dst_w and $dst_h would be the same as the height and width of $dst_image, since we're filling the entire thing.
$src_x and $src_y would be the top left of the crop. $src_w and $src_h would be the distance from the top left to the bottom right of the crop.
That leaves calculating where the crop will be.
If you have an image that is 18x6 pixels, the crop would be 6x6. We know this, but
how do we know it? We first know that the crop should be in the middle, so:
$middle = $width / 2;
We next know that the max height and width of the crop is the height (the smallest dimension), so:
$src_w = $src_h = $height;
After that, we need the top left corner of the crop. We know the top is at 0,
$src_y = 0;
and we know that the x value is half the width away from the middle of the crop, so:
$src_x = $middle - ($src_w / 2);
The only thing left is to account for vertical images, where I think we can simply invert x and y.
Code: Select all
//untested! I hope this is right... ^^;
//where $width and $height are the w & h of the original image:
if ($width > $height) {
$middle = $width / 2;
$src_w = $src_h = $height;
$src_y = 0;
$src_x = $middle - ($src_w / 2);
}
elseif ($height < $width) {
$middle = $height / 2;
$src_w = $src_h = $width;
$src_x = 0;
$src_y = $middle - ($src_h / 2);
}
else { //image is already square
$src_w = $src_h = $height; //or $width, it doesn't matter..
$src_x = $src_y = 0;
}
The rest you should be able to figure out yourself.
Edit:
damn. got beaten to it.