jpegtran optimize without changing filename
how about:
jpegtran -copy none -optimize -outfile image.jpg image.jpg
I'm not a shell expert by any means, but I think that with your original command, as soon as it is executed, the redirect is set up which overwrites your image.jpg. Then when jpegtran tries to read it in it finds an empty file.
jpegtran whole directory
Answer:
find /img/path -name "*.jpg" -type f -exec jpegtran -copy none -optimize -outfile {} {} \;
jpg won't optimize (jpegtran, jpegoptim)
I downloaded your image from imgur, but the size is 189,056 bytes. Is it possible that imgur did something to your image?
Anyway, I managed to optimize it to 165,920 bytes using Leanify (I'm the author) and it's lossless.
How to avoid Optimizing images that are already optimized with PHP?
Meta data
You could put a flag in the meta info of each image after it is optimized. First check for that flag and only proceed if it's not there. You can use exif_read_data() to read the data. Writing it maybe like this.
The above is for JPGs. Metdata for PNGs is also possible take a look at this question, and this one.
I'm not sure about GIFs, but you could definitely convert them to PNGs and then add metadata... although I'm pretty sure they have their own meta info, since meta data extraction tools allow GIFs.
Database Support
Another solution would be to store information about the images in a MySQL database. This way, as you tweak your optimizations you could keep track of when and which optimization was tried on which image. You could pick which images to optimize according to any parameters of your choosing. You could build an admin panel for this. This method would allow easy experimentation.
You could also combine the above two methods.
Maximum File Size
Since this is for saving space, you could have the program only work on images that are larger than a certain file size. Ideally, after running the compressor once, all the images would be below this file size, and after that only newly added images that are too big would be touched. I don't know how practical this is in terms of implementation, since it would require that the compressor gets any image below some arbitrary files size. You could make the maximum file size dependent on image size.....
How does Google's Page Speed lossless image compression work?
If you're really interested in the technical details, check out the source code:
- png_optimizer.cc
- jpeg_optimizer.cc
- webp_optimizer.cc
For PNG files, they use OptiPNG with some trial-and-error approach
// we use these four combinations because different images seem to benefit from
// different parameters and this combination of 4 seems to work best for a large
// set of PNGs from the web.
const PngCompressParams kPngCompressionParams[] = {
PngCompressParams(PNG_ALL_FILTERS, Z_DEFAULT_STRATEGY),
PngCompressParams(PNG_ALL_FILTERS, Z_FILTERED),
PngCompressParams(PNG_FILTER_NONE, Z_DEFAULT_STRATEGY),
PngCompressParams(PNG_FILTER_NONE, Z_FILTERED)
};
When all four combinations are applied, the smallest result is kept. Simple as that.
(N.B.: The optipng
command line tool does that too if you provide -o 2
through -o 7
)
For JPEG files, they use jpeglib with the following options:
JpegCompressionOptions()
: progressive(false), retain_color_profile(false),
retain_exif_data(false), lossy(false) {}
Similarly, WEBP is compressed using libwebp with these options:
WebpConfiguration()
: lossless(true), quality(100), method(3), target_size(0),
alpha_compression(0), alpha_filtering(1), alpha_quality(100) {}
There is also image_converter.cc which is used to losslessly convert to the smallest format.
recursive .bat file to apply jpegtran and pngout to all images in subfolders
This should work when launched in the main folder of the image tree.
Test it on some sample folders first.
@echo none
for /d /r %%a in (*) do (
pushd "%%a"
echo processing "%%a"
md "OptimizedJPEGS"
for %%i in (*.jpg) do "C:\imageoptimization\jpegtran.exe" -optimize -progressive -copy none "%%i" "OptimizedJPEGS\%%i"
move /Y "OptimizedJPEGS\*.*" .
rd "OptimizedJPEGS"
for %%i in (*.png) do "C:\imageoptimization\pngout.exe" "%%i"
popd
)
Is there a way to set some data into an image
If you were to include a flag in the image itself then that would be served to the clients. It would add to the filesize of your images thus negating some of your optimisation.
Suggestions
Keep a reference of the status
Keep a catalog in a file in the same directory - much like the Windows Thumbs.db
file.
Another option would be to keep the record in a database or datastore such as Redis or Memcached.
Move after processing
You could move the files to a different directory once they are processed (as @Jordan mentions).
Change the filename to indicate it is processed
Another option would be to append an extra "extension" onto the file name for example:
my_image.processed.jpg
Embedding data in images
Steganography
Usually this is used for attempting to hide covert data in an image and it is called Steganography. It is not really suited to this use case however.
EXIF data
You could write it into the EXIF data of an image, but this would be JPEG and TIFF only as far as I am aware. There is a PHP library available called PEL for writing and reading EXIF data.
Related Topics
What Should I Choose: Gtk+ or Qt
Linux Why Can't I Pipe Find Result to Rm
How to Change All Occurrences of a Word in All Files in a Directory
Sh: 0: Getcwd() Failed: No Such File or Directory on Cited Drive
How to Figure Out What Is Using a Linux Kernel Module
How to File Split at a Line Number
Where Are All My Inodes Being Used
How to Remove Duplicate Words from a Plain Text File Using Linux Command
What's an Alternative for Dtrace on Linux
Portable Way to Get File Size (In Bytes) in the Shell
How to Call Matlab Functions from the Linux Command Line
What Does the 2> Mean on the Unix Command-Line
Using Awk to Align Columns in Text File
Maximum Number of Files/Directories on Linux
What Do the .Eh_Frame and .Eh_Frame_Hdr Sections Store, Exactly