PHP GD:画像データをバイナリ文字列として取得する方法は?

StackOverflow https://stackoverflow.com/questions/1206884

  •  05-07-2019
  •  | 
  •  

質問

画像ファイルをzipにアセンブルし、ブラウザ/ Flexアプリケーションにストリーミングするためのソリューションを使用しています。 (ZipStream by Paul Duncan、 http://pablotron.org/software/zipstream-php/ )。

画像ファイルをロードして圧縮するだけでうまくいきます。ファイルを圧縮するためのコアは次のとおりです。

// 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 );

手がかりはありますか

役に立ちましたか?

解決

1つの方法は、GDに画像を出力するように指示し、PHPバッファリングを使用して文字列にキャプチャすることです。

$imagedata = imagecreatefrompng($imagefile);
ob_start();
imagepng($imagedata);
$stringdata = ob_get_contents(); // read from buffer
ob_end_clean(); // delete buffer
$zdata = gzdeflate($stringdata);

他のヒント

// ob_clean(); // optional
ob_start();
imagepng($imagedata);
$image = ob_get_clean();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top