2011-01-23 7 views
2

私はimagemagickで作成されたイメージをPHPに保存しようとしています。これは私のコードです:イメージを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); 
関連する問題