首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用shell_exec()保存映像--使用imagejpeg & jpegoptim,使用stdin / stdinout

使用shell_exec()保存映像--使用imagejpeg & jpegoptim,使用stdin / stdinout
EN

Stack Overflow用户
提问于 2015-06-21 19:36:20
回答 1查看 558关注 0票数 1

我保存了两次图像,一次是用imagejpeg创建的,然后用jpegoptim压缩和覆盖。我怎么能一举做到这一点,所以我不会保存图像两次?

代码语言:javascript
复制
$im = imagecreatefromstring($imageString);
imagejpeg($im, 'img/test.jpg', 100);
shell_exec("jpegoptim img/test.jpg");

Jpegoptim有stdin和stdout,但我很难理解如何使用它们。

我想用shell保存图像,所以我设想如下所示:

代码语言:javascript
复制
imagejpeg($im);
shell_exec("jpegoptim --stdin > img/test.jpg");

但唉,这不像我想象的那样有效。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-21 19:46:02

虽然这可能没有更好的效果,但这是一个只将最终结果写入磁盘的解决方案:

代码语言:javascript
复制
// I'm not sure about that, as I don't have jpegoptim installed 
$cmd = "jpegoptim --stdin > img/test.jpg";
// Use output buffer to save the output of imagejpeg
ob_start(); 
imagejpeg($img, NULL, 100); 
imagedestroy($img); 
$img = ob_get_clean();
// $img now contains the binary data of the jpeg image
// start jpegoptim and get a handle to stdin 
$handle = popen($cmd, 'w');
// write the image to stdin
fwrite($handle, $img."\n");

如果脚本继续运行,以后不要忘记关闭所有句柄。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30968548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档