I have a php script im currently using that creates thumbnails based on a max width and height. However, I\'d like it to always create square images and crop the images whe
This modified function worked great for me
public function igImagePrepare($img,$name){
$dir = 'my-images/';
$img_name = $name.'-'.uniqid().'.jpg';
//Your Image
$imgSrc = $img;
//getting the image dimensions
list($width, $height) = getimagesize($imgSrc);
//saving the image into memory (for manipulation with GD Library)
$myImage = imagecreatefromjpeg($imgSrc);
$square_size = 400;
$width = imagesx( $myImage );
$height = imagesy( $myImage );
//set dimensions
if($width> $height) {
$width_t=$square_size;
//respect the ratio
$height_t=round($height/$width*$square_size);
//set the offset
$off_y=ceil(($width_t-$height_t)/2);
$off_x=0;
} elseif($height> $width) {
$height_t=$square_size;
$width_t=round($width/$height*$square_size);
$off_x=ceil(($height_t-$width_t)/2);
$off_y=0;
}
else {
$width_t=$height_t=$square_size;
$off_x=$off_y=0;
}
/* Create the New Image */
$new = imagecreatetruecolor( $square_size , $square_size );
/* Transcribe the Source Image into the New (Square) Image */
$bg = imagecolorallocate ( $new, 255, 255, 255 );
imagefill ( $new, 0, 0, $bg );
imagecopyresampled( $new , $myImage , $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height );
//final output
imagejpeg($new, $dir.$img_name);
return $dir.$img_name;
}