簡體   English   中英

使用shell_exec()保存圖像-使用imagejpeg和jpegoptim,以及stdin / stdinout

[英]Saving an image with shell_exec() - using imagejpeg & jpegoptim, with stdin / stdinout

我將圖像保存兩次,一次是使用imagejpeg創建圖像,然后使用jpegoptim壓縮並覆蓋。 一口氣怎么做,這樣就不會保存圖像兩次了?

$im = imagecreatefromstring($imageString);
imagejpeg($im, 'img/test.jpg', 100);
shell_exec("jpegoptim img/test.jpg");

Jpegoptim具有stdin和stdout ,但是我正在努力了解如何使用它們。

我想用外殼保存圖像,所以我想像這樣:

imagejpeg($im);
shell_exec("jpegoptim --stdin > img/test.jpg");

但可惜的是,這與我的想象不符。

盡管這可能不會更好,但是這是一種只將最終結果寫入磁盤的解決方案:

// 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");

如果腳本繼續運行,不要忘了之后關閉所有句柄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM