我保存了两次图像,一次是用imagejpeg创建的,然后用jpegoptim压缩和覆盖。我怎么能一举做到这一点,所以我不会保存图像两次?
$im = imagecreatefromstring($imageString);
imagejpeg($im, 'img/test.jpg', 100);
shell_exec("jpegoptim img/test.jpg");Jpegoptim有stdin和stdout,但我很难理解如何使用它们。
我想用shell保存图像,所以我设想如下所示:
imagejpeg($im);
shell_exec("jpegoptim --stdin > img/test.jpg");但唉,这不像我想象的那样有效。
发布于 2015-06-21 19:46:02
虽然这可能没有更好的效果,但这是一个只将最终结果写入磁盘的解决方案:
// 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");如果脚本继续运行,以后不要忘记关闭所有句柄。
https://stackoverflow.com/questions/30968548
复制相似问题