2009-07-30 21 views
18

イメージファイルをzipにアセンブルし、ブラウザ/ Flexアプリケーションにストリーミングするソリューションを使用しています。 (ZipStream Paul Duncan著、http://pablotron.org/software/zipstream-php/)。PHP GD:imagedataをバイナリ文字列として取得するには?

イメージファイルを読み込んで圧縮するだけで正常に動作します。ここでは、ファイルを圧縮するためのコアがあります:

// Reading the file and converting to string data 
$stringdata = file_get_contents($imagefile); 

// Compressing the string data 
$zdata = gzdeflate($stringdata); 

私の問題は、私はそれを圧縮する前にGDを使用して画像を処理したいということです。したがって、イメージデータ(imagecreatefrompng)を文字列データ形式に変換するソリューションが必要です。

// Reading the file as GD image data 
$imagedata = imagecreatefrompng($imagefile); 
// Do some GD processing: Adding watermarks etc. No problem here... 

// HOW TO DO THIS??? 
// convert the $imagedata to $stringdata - PROBLEM! 

// Compressing the string data 
$zdata = gzdeflate($stringdata);

手がかりはありますか?

答えて

39

一つの方法は、文字列にそれを捕捉するためにPHPのバッファリングを使用し、その後、出力する画像GDを伝えることである。

$imagedata = imagecreatefrompng($imagefile); 
ob_start(); 
imagepng($imagedata); 
$stringdata = ob_get_contents(); // read from buffer 
ob_end_clean(); // delete buffer 
$zdata = gzdeflate($stringdata); 
8
// ob_clean(); // optional 
ob_start(); 
imagepng($imagedata); 
$image = ob_get_clean(); 
+0

ob_get_clean()は、本質的にので、)(ob_get_contents()およびob_end_cleanの両方を実行しますこの解決方法は、上記の答えが少し上品です。 –

関連する問題