2012-01-26 4 views
1

크기 조정 및 자르기 여러 파일에. 그것은 버그이거나 나는 그것을 보지 않고 있습니다. 누군가가 나를 도와 줄 수 있기를 바랍니다. 미리 감사드립니다! 스크립트에CodeIgniter의 이미지 처리 클래스 : 내가 제대로 작동 CodeIgniter의 이미지 조작을 얻기 위해 사투를 벌인거야

: 나는 (176w의 X의 132h)를 작은 이미지를 만들려고합니다. 입력 이미지의 크기와 비율이 다릅니다. 항상이 크기를 얻으려면 먼저 최대 너비 또는 높이 (이미지 방향에 따라 다름)에 맞게 크기를 조정 한 다음 가운데에서 자릅니다. 나는 1 개의 방법 전부에서 그것을하는 것을 시도했다. 그게 효과가 없어서, 나는 두 개의 분리 된 방법을 만들었다.

resize_img() 

crop_img(). 

내가 3 개 다른 파일에 resize_img()를 실행

, 그것을 작동합니다. 그 이후에 작곡 된 1 번째 방법에 대한 축소판에 crop_img()를 사용하면 작동합니다. 두 가지를 결합하거나 서로 뒤에서 사용하면 그렇지 않습니다. $ this-> image_lib-> clear();를 시도했지만 설정 파일을 설정 해제했습니다. 심지어 두 개의 설정 파일을 만들었습니다.

나는 GD2에서 오류가 모든 종류의 서로 다른 받고 있어요,하지만 문제는 미리보기를 생성() resize_img 후 crop_img()가 그것을 잘라하지 않습니다이다. 그 후에 그것은 모두 남쪽으로 가고 다음 이미지는 열리지 않습니다. 쓰기 전제 조건은 폴더와 파일 모두에서 검사됩니다. 이미지를 저장

수 없습니다. 이미지 및 파일 디렉토리에 쓸 수 있는지 확인하십시오. 이미지 경로가 올바르지 않습니다. 서버가이 유형의 이미지를 처리하는 데 필요한 GD 기능을 지원하지 않습니다.

전체 코드 :

<?PHP 

class Imagetest extends MY_Controller { 

function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('image_lib'); 
} 

function index() 
{ 
    $testimg1 = 'uploads/test/1.png'; 
    $testimg2 = 'uploads/test/2.png'; 
    $testimg3 = 'uploads/test/3.png'; 

    $this->resize_img($testimg1); 
    $this->crop_img($testimg1); 

    $this->resize_img($testimg2); 
    $this->crop_img($testimg2); 

    $this->resize_img($testimg3); 
    $this->crop_img($testimg3); 
} 


function image_thumb_name($img = null) 
{ 
    if(!empty($img)) { 
     $exploded = explode('.', $img); 
     return $exploded['0'] . '_thumb.' . $exploded['1']; 
    } 
} 

function get_axis($img) 
{ 
    // Default values 
    $output['x_axis'] = 0; 
    $output['y_axis'] = 0; 

    // Settings 
    $config['height'] = 132; 
    $config['width'] = 176; 

    if ($img_dim = getimagesize($img)) { 
     list($thumb_width, $thumb_height) = $img_dim; 
    } else { 
     echo '<h1> ERROR HERE TOO</h1>'; 
     return false; 
    } 

    if ($thumb_width > $config['width']) { 

     $output['x_axis'] = (($thumb_width - $config['width'])/2) ;    

    } else if ($thumb_height > $config['height']) { 

     $output['y_axis'] = (($thumb_height - $config['height'])/2); 
    }  
    return $output; 
} 

function resize_img($img) 
{ 
    $config = array(); 
    echo 'Processing: '. $img .'<br/>'; // Debug 

    if ($img_dim = getimagesize($img)) { 
     list($image_width, $image_height) = $img_dim; 
    } else { 
     echo '<h1> ERROR HERE </h1>'; 
    } 

    // create a thumbnail 
    $config['image_library'] = 'GD2'; 
    $config['source_image'] = $img; 
    $config['quality'] = 100; 
    $config['height'] = 132; 
    $config['width'] = 176; 
    $config['create_thumb'] = TRUE; 
    $config['maintain_ratio']= TRUE; 
    $config['master_dim'] = ($image_width > $image_height) ? 'height' : 'width'; 
    $this->image_lib->initialize($config); 

    if (!$this->image_lib->resize()) { 
     echo $this->image_lib->display_errors(); 
    } 

    echo '<img src="../'.$this->image_thumb_name($img).'" /> <br/>'; // Debug 

    $this->image_lib->clear(); 
    unset($config); 
} 




function crop_img($img) 
{ 
    $config2 = array(); 

    // Crop that thumbnail 
    $config2['image_library'] = 'GD2'; 
    $config2['quality'] = 100; 
    $config2['height'] = 132; 
    $config2['width'] = 176; 
    $config2['source_image'] = $this->image_thumb_name($img); 
    $axis = $this->get_axis($config2['source_image']); 
    $config2['x_axis'] = $axis['x_axis']; 
    $config2['y_axis'] = $axis['y_axis']; 
    $config2['maintain_ratio'] = FALSE; 
    $config2['create_thumb'] = FALSE; 
    $this->image_lib->initialize($config2); 

    if (!$this->image_lib->crop()) { 
     echo $this->image_lib->display_errors(); 
    } 

    echo '<img src="../'.$config2['source_image'].'" /> <br/>'; // Debug 

    $this->image_lib->clear(); 
    unset($config2); 
} 

} 
+0

난 여전히이 작업을 얻을 수 아니에요. 어떤 아이디어? – Robert

답변

1

알 수 있습니다. create_thumb 옵션을 FALSE로 설정하고 resize_img 메소드에서 new_image 매개 변수를 사용했습니다. 효과는 동일하지만 내장 create_tumb 함수가 사용되지 않습니다. 그것은 버그 이럴하지만 지금 일하고있어 :)

$config['create_thumb'] = FALSE; 
$config['new_image'] = $this->image_thumb_name($img); 
0

문제는 당신의 $의 설정과이다 '는 source_image']

var_dump($config['source_image']); 

당신이 무엇을 얻을 참조하십시오.

파일이 이미지 인 경우

루트 폴더에 폴더,은 $ 설정 [ '는 source_image] ='이미지 /'.$ IMG.

또한 올바른 파일 이름을 반환해야합니다 모든 폭발하는 기능을합니다. (리턴 및 에코 검사).

첫째, 파일을 찾을 수 없음 오류를 제거하십시오. 그 후에 오류가 무엇인지 살펴보십시오.

+0

itachi, 조사해 주셔서 감사합니다. 파일 이름을 두 번 확인했는데 정확합니다. 파일을 찾을 수 없습니다 오류가 발생했습니다. 두 번째 방법은 1 위가 생성해야하는 축소판을 자르려고 시도하지만 실제로는 생성하지 않았기 때문에 존재하지 않습니다. ps. 다음과 같이 실행하면 $ this-> resize_img ($ testimg1); $ this-> resize_img ($ testimg2); $ this-> resize_img ($ testimg3); 작동하지만이 부분을 주석 처리해야합니다. (또한 작동). $ this-> crop_img ($ testimg1); $ this-> crop_img ($ testimg2); $ this-> crop_img ($ testimg3); 문제는 함께 작동하지 않는다는 것입니다. – Robert