Хорошо, я сделал рефакторинг, чтобы дать вам лучшее представление о том, что происходит.Я добавил комментарии, чтобы объяснить мои изменения.
private function getBaseImgArray( $photo )
{
// you need this array three times. It should be extracted.
$ret = array(
'image_library' => 'gd2',
'source_image' => 'uploads/img/gallery/small/'.$photo,
'maintain_ratio' => FALSE
);
return $ret;
}
private function getCroppedImgArray( $photo, $imgWidth, $imgHeight )
{
// this summarizes everything in your first two code blocks.
$img = $this->getBaseImgArray( $photo );
$img[ 'x_axis' ] = ($imgWidth > $imgHeight)? $imgWidth - $imgHeight: 0;
$img[ 'y_axis' ] = ($imgWidth < $imgHeight)? $imgHeight - $imgWidth: 0;
}
public function yourFunctionNameHere()
{
$this->load->library('image_lib');
$img = $this->getCroppedImgArray( $photo, $imgWidth, $imgHeight );
// this is the only difference between the first and second img
// height/width blocks. Should it be there for both?
if($imgWidth < $imgHeight){
$img['create_thumb'] = TRUE;
}
$this->image_lib->initialize($img);
$this->image_lib->crop();
echo $this->image_lib->display_errors();
// right here you're overwriting the above code. If your image library
// works as I would expect, then your cropped images will be cropped
// overwritten, and then re-sized.
$img = $this->getBaseImgArray( $photo );
// on a technical note, you are using the same array when you resize,
// but since resize ignores the <x/y>_axis variables, it would not effect
// the final outcome.
$img['width'] = 80;
$img['height'] = 80;
$this->image_lib->initialize($img);
$this->image_lib->resize();
echo $this->image_lib->display_errors();
}