Ok, basically I want all images to be 170x170px squares. Thus if an image is not a square i want it to be resized, and then cropped in the middle..
I have spent numerous hours playing with this and I am getting nowhere.. I have gotten it to crop a section of the bigger image etc, but i specifically need the image to be resized, then cropped..
Any help would be greatly appreciated.
// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
// image height
$sh = $x[1];
if($sw > $sh) // Horizontal Rectangle?
{
$newwidth = ($sw/$sh)*170;
$newheight=170;
$x_pos = ($sw - $sh) / 2;
$x_pos = ceil($x_pos);
$y_pos=0;
}
else if($sh > $sw) // Vertical Rectangle?
{
$newheight = ($sh/$sw)*170;
$newwidth=170;
$y_pos = ($sh - $sw) / 2;
$y_pos = ceil($y_pos);
$x_pos=0;
}
else //Already Square
{
$newheight=170;
$newwidth=170;
}
$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
$im = @ImageCreateFromPNG ($img) or // or PNG Image
$im = @ImageCreateFromGIF ($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF
if (!$im) {
// We get errors from PHP's ImageCreate functions...
// So let's echo back the contents of the actual image.
readfile ($img);
} else {
// Create the resized image destination
$thumb = @ImageCreateTrueColor (170, 170);
// Copy from image source, resize it, and paste to image destination
imagecopyresampled($thumb, $im, 0, 0, 180, $y_pos, 170, 170, $newwidth,
$newheight);
}