PHP GD:如何获取二进制字符串形式的图像数据?
我正在使用一种解决方案将图像文件组装为 zip 并将其流式传输到浏览器/Flex 应用程序。 (ZipStream 作者: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 );
有任何线索吗?
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 );
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 );
Any clues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种方法是告诉 GD 输出图像,然后使用 PHP 缓冲将其捕获到字符串中:
One way is to tell GD to output the image, then use PHP buffering to capture it to a string:
当不需要输出缓冲区杂耍时,可以使用 php://memory 流。
https://www.php.net/manual/en/wrappers.php.php
The php://memory stream can be used when output-buffer juggling is unwanted.
https://www.php.net/manual/en/wrappers.php.php