PHP GD:如何获取图像数据作为二进制字符串? [英] PHP GD: How to get imagedata as binary string?
问题描述
我正在使用一种将图像文件组装为zip并将其流式传输到浏览器/Flex应用程序的解决方案. (Paul Duncan的ZipStream, http://pablotron.org/software/zipstream-php/ ).
I'm using a solution for assembling image files to a zip and streaming it to browser/Flex application. (ZipStream by Paul Duncan, http://pablotron.org/software/zipstream-php/).
只需加载图像文件并将其压缩即可正常工作.这是压缩文件的核心:
Just loading the image files and compressing them works fine. Here's the core for compressing a file:
// Reading the file and converting to string data
$stringdata = file_get_contents($imagefile);
// Compressing the string data
$zdata = gzdeflate($stringdata );
我的问题是我想在压缩之前使用GD处理图像.因此,我需要一种将图像数据(imagecreatefrompng)转换为字符串数据格式的解决方案:
My problem is that I want to process the image using GD before compressing it. Therefore I need a solution for converting the image data (imagecreatefrompng) to string data format:
// 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 );
有任何线索吗?
推荐答案
一种方法是告诉GD输出图像,然后使用PHP缓冲将其捕获为字符串:
One way is to tell GD to output the image, then use PHP buffering to capture it to a string:
$imagedata = imagecreatefrompng($imagefile);
ob_start();
imagepng($imagedata);
$stringdata = ob_get_contents(); // read from buffer
ob_end_clean(); // delete buffer
$zdata = gzdeflate($stringdata);
这篇关于PHP GD:如何获取图像数据作为二进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!