$info = getimagesize($path . 'tb_' . $file); if ( $info[0] / $info[1] != 0 ) //if the source image is NOT a square { $config['source_image'] = $path . 'tb_' . $file; if ( $info[0] > $info[1] ) //the image is horizontal { $x = $info[0] - $info[1]; $config['x_axis'] = $info[0] - $x; $config['y_axis'] = $info[1]; } else //the image is vertical { $x = $info[1] - $info[0]; $config['x_axis'] = $info[0]; $config['y_axis'] = $info[1] - $x; } //cropping $this->image_lib->initialize($config); $this->image_lib->crop(); }