이미지 라이브러리는 나에게 많은 골칫거리를주었습니다. 좋지 않아서가 아니라, 새로운 것을 시도 할 때마다 어떻게 작동하는지 다시 배워야하기 때문입니다. 나에게 적어도 설명서는 약간 이해하기 어렵다.
처음에는 자르기 전에 크기를 조정하는 것이 더 바람직하다고 생각했습니다. 그 이유를 정확히 기억하지 못하지만 나중에 반대되는 것이 낫다는 것을 발견했습니다. 나는 그걸 착각 할 수도 있지만, 지금은 코드가 잘 작동하기 때문에 그 전략을 고수 할 것입니다.
내가 중요하다고 생각하는 또 다른 사항은 'maintain_ratio'를 FALSE로 설정하고 계산을 직접 수행하는 것입니다.
최근 크기 조정을위한 함수를 다시 작성했으며, 변수 $ top_crop을 제외하고는 대부분 설명이 필요하다고 생각합니다. 그건 내 "이발사"변수인데,이 변수는 얼마나 많은 돈을 차지할 것인지를 가정합니다. 내 구성 파일 '설정'에서 20으로 설정했습니다. 즉, 잘라내는 총량 중 20 %가 상단에서 벗어납니다. 즉, 100 픽셀을 자르는 경우 상단에서 20, 하단에서 80을 가져옵니다.
어쨌든 여기 자르기위한 나의 코드가 있습니다. 당신은 그것을 사용할 수 있으며 귀하의 요구에 적응할 수 있습니다 :
function resize_img($data){
if ($data['width'] == 0 || $data['height'] == 0){
return FALSE;
}
$this->config->load('settings');
$ratio = $data['height']/$data['width'];
$targ_ratio = $data['max_ht']/$data['max_wd'];
$top_crop = $this->config->item('crop_top');
if ($targ_ratio >= $ratio){
//too wide
$crop_width = floor($data['height']/$targ_ratio);
$crop_height = $data['height'];
} else {
//too tall
$crop_width = $data['width'];
$crop_height = floor($data['width'] * $targ_ratio);
}
$img_data = array( 'source_image' => $data['full_path'],
'maintain_ratio' => FALSE,
'x_axis' => round(($data['width'] - $crop_width)/2),
'y_axis' => round(($data['height'] - $crop_height)*$top_crop/100),
'width' => $crop_width,
'height' => $crop_height);
//thumbs have a target path
if ($data['target_path']){
$img_data['new_image'] = $data['target_path'];
//set source for the crop, because for thumbs it will be the thumb folder
$source = $data['target_path'].$data['file_name'];
} else {
$source = $data['full_path'];
}
$this->image_lib->clear();
$this->image_lib->initialize($img_data);
if ($this->image_lib->crop()){
$img_data = array( 'source_image' => $source,
'maintain_ratio' => FALSE,
'width' => $data['max_wd'],
'height' => $data['max_ht']);
$this->image_lib->clear();
$this->image_lib->initialize($img_data);
if($this->image_lib->resize()){
return array('height' => $data['max_ht'], 'width' => $data['max_wd']);
}
}
return $this->image_lib->display_errors();
}