Bug #19751
closedStrip uploaded images headers
0%
Description
Images can hold miscellaneous information (profiles, comments) in its headers.
The idea is to introduce config option:
$TYPO3_CONF_VARS['GFX']['im_strip_uploaded_images'] = 1;
which would strip such headers from images when uploading them to Typo3.
Since ImageMagick cannot strip headers without recompressing the image (http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=10895&start=0&st=0&sk=t&sd=a), we should use 'jpegtran' command (part of libjpeg package for lossless transformation) for jpegs and im 'convert' for other formats:
- Command for jpegs: jpegtran optimize -copy none -outfile out.jpg in.jpg Command for other formats: convert -strip in.png out.png
And then replace the original image if the resulting is smaller.
Please find attached shell script for stripping existing images. The same logic should be implemented in Typo3 itself, triggered on every image upload.
You can try the script using 'test' switch:
./strip_image_headers.sh test /typo3/typo3temp
I would recommend running the following commands on the existing Typo3 installation (backup everything first of course):
./strip_image_headers.sh typo3temp
./strip_image_headers.sh typo3temp/pics
./strip_image_headers.sh uploads
./strip_image_headers.sh uploads/pics
./strip_image_headers.sh uploads/media
and for all other directories where you have uploaded your own images.
You can later fix ownership of files if necessary.
Statistics for 96187 images:
- Before: 2176 MB
- After running the script: 1243 MB
- Gain: 43% smaller images!
Related to #19748.
(issue imported from #M10032)
Files
Updated by Fabien Udriot almost 16 years ago
I tried to run the script but I faced some compatibility problems on NetBSD / FreeBSD system. This is due to some little nuance betwen Linux and *BSD command.
For example this line does not work and makes the script crash.
size1=$(stat -c%s "$i")
I changed to:
size1=$(ls -al "$i" | awk '{ print $5 }')
It happens the same with: `expr index "$i" ' '` (makes the script die!)
I converted to: `/bin/expr "$i" : ".* "` I must admit I am not sure it will pass on Linux. It needs to be tested.
Btw, I have improved a little bit the script by adding a recusive handling of the images.
Updated by Steffen Gebert almost 16 years ago
I prefer recursive work, too. But you should avoid `find $DIR/*` as this passes a long (often too long) argument list to find command. Use `find $DIR/` instead!
Unfortunately for me the script didn't help that much: 5% savings with 29.000 image files in typo3temp/.
Updated by John Angel almost 16 years ago
Additional squizing:
- By Niels Frohling:
http://groups.google.com/group/comp.compression/browse_thread/thread/837b8aa980c3090f/e8d8cbaa213d3d68?lnk=gst&q=jpegtran#e8d8cbaa213d3d68
http://paradice-insight.us/cdb/scripts/jpeg/
- pngcrush: http://pmt.sourceforge.net/pngcrush/
- Try converting GIFs to PNGs
More info: http://developer.yahoo.com/performance/rules.html#opt_images
Updated by John Angel almost 16 years ago
Here is the code...
We are adding three configuration vars to localconf.php:
$TYPO3_CONF_VARS['GFX']['jpegtran_path'] = '/usr/bin/'; // Path to jpegtran
$TYPO3_CONF_VARS['GFX']['strip_uploaded_images'] = 1; // Strip image headers from uploaded images
$TYPO3_CONF_VARS['GFX']['strip_generated_images'] = 1; // Strip image headers from Typo3 generated images (usually thumbs), ref bug #10025
In function file t3lib/class.t3lib_div.php, before function upload_copy_move:
INSERT:
public static function optimize_image($filename) {
$gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
$newfile = t3lib_div::tempnam('optimized_image_');
$path_parts = pathinfo($filename);
$extension = strtolower($path_parts['extension']);
if(in_array($extension,array('jpg','jpeg')))
$cmd = $gfxConf['jpegtran_path'] . 'jpegtran -optimize -copy none -outfile "'.$newfile.'" "'.$filename.'"';
else
$cmd = t3lib_div::imageMagickCommand('convert', '-strip "'.$filename.'" "'.$newfile.'"', $gfxConf['im_path']);
exec($cmd);
if (@is_file($newfile) && filesize($newfile)>0 && filesize($newfile)<filesize($filename))
copy($newfile,$filename);
t3lib_div::unlink_tempfile($newfile);
}
The same file, in function upload_copy_move:
BEFORE:
t3lib_div::fixPermissions($destination);
INSERT:
if($GLOBALS['TYPO3_CONF_VARS']['GFX']['strip_uploaded_images'])
t3lib_div::optimize_image($destination);
The idea is to use the same function optimize_image to process images on existing Typo3 installations within Installation Tool.
We can improve this function with pngcrush/gif2png and other stuff mentioned in this bug.
Updated by Michael Stucki over 15 years ago
Are there any differences between this issue and #10025? Otherwise I would close this as duplicate of #10025.
Updated by John Angel over 15 years ago
#0010025 is related to
$TYPO3_CONF_VARS['GFX']['strip_generated_images'] = 1; // Strip image headers from Typo3 generated images (usually thumbs)
and this one is related to:
$TYPO3_CONF_VARS['GFX']['strip_uploaded_images'] = 1; // Strip image headers from uploaded images
so they are different things.
Updated by Dmitry Dulepov over 13 years ago
- Status changed from Needs Feedback to Closed
- Target version deleted (
0) - TYPO3 Version set to 4.2
fixed already