I'm saving an image twice, once when I create it with imagejpeg and then I compress and overwrite with jpegoptim. How may I do this in one swoop, so I'm not saving the image twice?
$im = imagecreatefromstring($imageString);
imagejpeg($im, 'img/test.jpg', 100);
shell_exec("jpegoptim img/test.jpg");
Jpegoptim have stdin and stdout, but I'm struggling to understand how to use them.
I want to save the image with the shell, so I imagine something like this:
imagejpeg($im);
shell_exec("jpegoptim --stdin > img/test.jpg");
But alas, it doesn't work how I imagined.
Although that might not perform better, this is a solution that writes nothing but the final result to disk:
Don't forget to close all handles afterwards if your script keeps running.