2011-01-23 3 views
2

PHP에서 imagemagick으로 만든 이미지를 저장하려고합니다. 이미지가 그런 식으로 요청pgp 스크립트에서 Imagemagick으로 올바른 형식으로 이미지 저장

 $source = str_replace(' ','%20',$_GET['src']); 
    $uri_components = parse_url($_GET['src']); 
    $width = isset($_GET['w']) ? abs(intval($_GET['w'])) : 0; 
    $height = isset($_GET['h']) ? abs(intval($_GET['h'])) : 0; 
    $zc = isset($_GET['zc']) ? intval($_GET['zc']) : 0; 
    $q = isset($_GET['q']) ? intval($_GET['q']) : 0; 

    if(isset($uri_components['host']) && !in_array($uri_components['host'], $allowed_domains)) { 
     $bad_domain = true; 

     if(ALLOW_SUBDOMAINS === true) 
      foreach($allowed_domains as $domain) 
       if(preg_match('/\.' . str_replace('.', '\.', $domain) . '$/i', $uri_components['host'])) { 
        $bad_domain = false; 
        break; 
       } 

     if($bad_domain) 
      trigger_error('Host not allowed: ' . $uri_components['host'], E_USER_ERROR); 
    } 

    if(isset($uri_components['host']) || file_exists($source)) 
     $image_handle = fopen($source, 'rb'); 
    else if(file_exists($_SERVER['DOCUMENT_ROOT'] . $source)) 
     $image_handle = fopen($_SERVER['DOCUMENT_ROOT'] .$source, 'rb'); 
    else 
     trigger_error('Image file (src) not found', E_USER_ERROR); 

    $image = new Imagick(); 
    $image->readimagefile($image_handle); 

    if($zc) 
     try { 
      $image->cropthumbnailimage($width, $height); 
     } catch (ImagickException $e) { 
      trigger_error($e->getMessage(), E_USER_ERROR); 
     } 
    else 
     try { 
      $image->thumbnailimage($width, $height, true); 
     } catch(ImagickException $e) { 
      trigger_error($e->getMessage(), E_USER_ERROR); 
     } 


    //$q = 100; 
    $image->setImageCompression($compression_type); 
    $image->setImageCompressionQuality($q); 
    $image->setResolution(300, 300); 


    $cache_time = 157680000; 
    header('Cache-Control: max-age=' . $cache_time . ',must-revalidate'); 
    header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $cache_time)); 

    header('Content-Type: image/' . $image->getImageFormat()); 
    echo $image; 


    $name = 'cache/'.$filename.'.'.$image->getImageFormat(); 
    file_put_contents($name, file_get_contents($image)); 

:

/script.php?src=pic.jpg&w=100&h=100&zc=1&q=90 

는 파일 헤더에 문제가 발생한 것 같습니다이 내 코드입니다. 이미지를 로컬에서 열면 헤더가 잘못되었다는 오류가 발생합니다. 내 기능의 실수는 어디에 있습니까?

감사합니다, $image 이미 당신이 필요로하는 내용이기 때문에 라스

+0

당신은 $ 이미지 (실제로 그것이 무엇인지) 정의됩니다 보여줍니다 코드의 섹션을 제공해야합니다. 순간적으로 getImageFormat 호출에 응답하지만 아직 디스크에 직접 기록 될 수있는 신비한 객체 형식 인 것처럼 보입니다. 불가능합니다. –

+0

오케이. 내 편집 좀 봐. – Lars

답변

3

는 사실이 코드에서, 당신은 file_get_contents($image)을 수행 할 필요가 없습니다.

나는 마지막 줄이되어야한다고 생각한다

file_put_contents($name, $image);