PHP GD : How to get imagedata as binary string?

标签 php zip gd

我正在使用一个解决方案将图像文件组装成一个 zip 并将其流式传输到浏览器/Flex 应用程序。 (Paul Duncan 的 ZipStream,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 );

有什么线索吗?

最佳答案

一种方法是告诉 GD 输出图像,然后使用 PHP 缓冲将其捕获到字符串:

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

关于PHP GD : How to get imagedata as binary string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1206884/

相关文章:

php - 安装SSL证书后,WordPress网站仍然不安全。如何更新Google字体网址

javascript - 如何在服务器端调用分页时停止页面重新加载?php

java(安卓): try compressed I/O and failed

PHP imagecopy去除png的颜色

php - 未在php:5.6-cli docker中安装libgd

php - 在 GD 中显示从右到左连接的语言(如波斯语和阿拉伯语)——可能是 PHP 错误

php - 两列 Ajax Livesearch 搜索

php - 小于运算符在 PHP 5.3.1 中无法正常工作

c# - 如何压缩文件夹中的所有文件

macos - 如何创建与 Finder 的 "Compress"菜单项格式相同的 zip 文件?