codeigniter image_lib проблема? - PullRequest
       20

codeigniter image_lib проблема?

0 голосов
/ 08 августа 2011
$this->load->library('image_lib');
if($imgWidth > $imgHeight){
    $img['image_library'] = 'gd2';
    $img['source_image'] = 'uploads/img/gallery/small/'.$photo;
    $img['maintain_ratio'] = FALSE;
    $img['x_axis'] = $imgWidth - $imgHeight;
    $img['y_axis'] = 0;
    $this->image_lib->initialize($img);
    $this->image_lib->crop();
    echo $this->image_lib->display_errors();
}
if($imgWidth < $imgHeight){
    $img['image_library'] = 'gd2';
    $img['source_image'] = 'uploads/img/gallery/small/'.$photo;
    $img['create_thumb'] = TRUE;
    $img['dinamic_output'] = TRUE;
    $img['maintain_ratio'] = FALSE;

    $img['x_axis'] = 0;
    $img['y_axis'] = $imgHeight-$imgWidth;
    $this->image_lib->initialize($img);
    $this->image_lib->crop();

    echo $this->image_lib->display_errors();
}
$img['image_library'] = 'gd2';
$img['source_image'] = 'uploads/img/gallery/small/'.$photo;
$img['maintain_ratio'] = FALSE;
$img['width'] = 80;
$img['height'] = 80;
$this->image_lib->initialize($img);
$this->image_lib->resize();
echo $this->image_lib->display_errors();

почему до сих пор не обрезка, а только изменение размера? (

также не возвращает никаких ошибок: /

1 Ответ

1 голос
/ 08 августа 2011

Хорошо, я сделал рефакторинг, чтобы дать вам лучшее представление о том, что происходит.Я добавил комментарии, чтобы объяснить мои изменения.

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();
}
...