Task #58110

File size of most images are not optimized

Added by Bas van Beek about 7 years ago. Updated over 2 years ago.

Status:
Closed
Priority:
Could have
Assignee:
Category:
Code Cleanup
Target version:
-
Start date:
2014-04-23
Due date:
% Done:

100%

Estimated time:
TYPO3 Version:
6.2
PHP Version:
Tags:
Complexity:
easy
Sprint Focus:

Description

A lot of images has a file size that can be further decreased without losing image quality.

#1

Updated by Markus Klein about 7 years ago

  • Status changed from New to Needs Feedback

That means??

What (setting?) do you suggest to change?
Example images?

#2

Updated by Bas van Beek about 7 years ago

The images file size can be further decreased. I'm trying to push the fix for this to review.typo3.org.

#3

Updated by Gerrit Code Review about 7 years ago

  • Status changed from Needs Feedback to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/29617

#4

Updated by Gerrit Code Review about 7 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/29617

#5

Updated by Gerrit Code Review about 7 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/29617

#6

Updated by Markus Klein about 7 years ago

Alright. Can you please document here what you did?
Maybe some settings you choose, in which tool, etc.
Thanks

#7

Updated by Bas van Beek about 7 years ago

I made a bash script that scans the TYPO3 core folder for PNG, JPEG and GIF files. The returned files are optimized by executing several image optimizer binaries.

Bash script:

folder=$1
if [ $folder ]
then
        folders=( $folder )
        for i in "${folders[@]}" 
        do
                # optimize png
                find $i -type f -iname '*.png' -exec sh -c "pngout {}; optipng -o7 {}; advpng -z1 {}; pngcrush -rem allb -brute -reduce {} {}.pngcrush && mv -f {}.pngcrush {}; chmod 777 {};" \;
                # optimize jpg
                find $i -type f -iname '*.jpg' -exec sh -c "/shopcvs/scripts/image_jpeg_optimizer_jpopt.sh {}; jpegoptim -p --strip-all -m88 {}; chmod 777 {};" \;
                # optimize jpeg
                find $i -type f -iname '*.jpeg' -exec sh -c "/shopcvs/scripts/image_jpeg_optimizer_jpopt.sh {}; jpegoptim -p --strip-all -m88 {}; chmod 777 {};" \;
                # optimize gif
                find $i -type f -iname '*.gif' -exec sh -c "gifsicle --batch -i {}; chmod 777 {};" \;
        done
else
        echo "/shopcvs/scripts/folder_img_optimizer.sh /folder" 
fi
#8

Updated by Bas van Beek about 7 years ago

And the contents of image_jpeg_optimizer_jpopt.sh:

siz() { ls -l "$1" | awk '{print $5}'; }

if type jhead >/dev/null 2>&1; then
    jhead=1 copy=none
else
    jhead=  copy=all
    echo >&2 WARNING: jhead not found, cannot optimize comments or EXIF data
fi

trap exit 1 2 3 13 15
oldsum=0  newsum=0

for f do
    [ -f "$f" ] || { echo >&2 "no file '$f'"; continue; }
    mydir=`dirname "$f"`
    [ "$mydir" -a -d "$mydir" ] || { echo >&2 "no dir '$mydir'"; continue; }
    a=$mydir/_${$}_a.jpg  b=$mydir/_${$}_b.jpg
    trap 'rm -f "$a" "$b"' 0
    rm -f "$a" "$b" 

    printf "'%s' ... " "$f" 
    jpegtran -copy "$copy" -opt  "$f" >"$a" 2>/dev/null && touch -r "$f" "$a" 
    jpegtran -copy "$copy" -prog "$f" >"$b" 2>/dev/null && touch -r "$f" "$b" 
    [ -f "$a" -a -s "$a" -a -f "$b" -a -s "$b" ] ||
        { rm -f "$a" "$b"; trap 0; echo FAILED; continue; }
    [ "$jhead" ] && jhead "$f" 2>/dev/null | grep '^Camera ' >/dev/null && {
        [ -f "$a" ] && jhead -te "$f" -dt -ft "$a" >/dev/null 2>&1 &&
        [ -f "$b" ] && jhead -te "$f" -dt -ft "$b" >/dev/null 2>&1 &&
        printf 'exif ' ||
            { rm -f "$a" "$b"; trap 0; echo EXIF_FAILED; continue; }
    }

    oldsize=`siz "$f"`   optsize=`siz "$a"`     progsize=`siz "$b"`
    if [ $progsize -lt $oldsize -a $progsize -lt $optsize ]; then
        mv -f "$b" "$f" 
        printf prog
    elif [ $optsize -lt $oldsize ]; then
        mv -f "$a" "$f" 
        printf opt
    fi
    rm -f "$a" "$b"; trap 0; echo
    oldsum=`expr $oldsum + $oldsize`
    oldsize=`siz "$f"`
    newsum=`expr $newsum + $oldsize`
done

[ $oldsum -gt 0 -a $newsum -gt 0 ] &&
    echo $oldsum bytes total, saved `expr $oldsum - $newsum` bytes \
    \(`expr 100 \* \( $oldsum - $newsum \) / $oldsum`%\)
#9

Updated by Gerrit Code Review about 7 years ago

Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/29617

#10

Updated by Christian Kuhn about 7 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100
#11

Updated by Gerrit Code Review about 7 years ago

  • Status changed from Resolved to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/29829

#12

Updated by Gerrit Code Review about 7 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/29829

#13

Updated by Stefan Neufeind about 7 years ago

Should we maybe put such scripts into some kind of "tool-package", for the case that new graphics are added to TYPO3?
There is afaik also the case that some SCSS are to be processed manually and then commited if they are updated. Do we maybe have some tools (and a doc on the process of updating) somewhere?

#14

Updated by Gerrit Code Review almost 7 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/29829

#15

Updated by Gerrit Code Review almost 7 years ago

Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/29829

#16

Updated by Gerrit Code Review almost 7 years ago

Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/29829

#17

Updated by Anja Leichsenring almost 7 years ago

  • Tracker changed from Bug to Task
  • Category set to Code Cleanup
  • Priority changed from Must have to Could have
  • Complexity set to easy

in a Unix System, you can create a file, that is included in your path like this:

cat ~/bin/git-file-size-diff 
#!/bin/sh
. git-sh-setup
args=$(git rev-parse --sq "$@")
eval "git diff-tree -r $args" | {
  total=0
  while read A B C D M P
  do
    case $M in
      M) bytes=$(( $(git cat-file -s $D) - $(git cat-file -s $C) )) ;;
      A) bytes=$(git cat-file -s $D) ;;
      D) bytes=-$(git cat-file -s $C) ;;
      *)
        echo >&2 warning: unhandled mode $M in \"$A $B $C $D $M $P\" 
        continue
        ;;
    esac
    total=$(( $total + $bytes ))
    printf '%d\t%s\n' $bytes "$P" 
  done
  echo total $total
}

Then apply the patch in an early patchset and run in the git root

git file-size-diff <commithash>

The output will show lots of files with a differenz of zero or positive numbers. Those grow larger compared to HEAD~1. When you apply the latest patchset, only negative numbers are displayed. Those files are smaller now.

#18

Updated by Gerrit Code Review almost 7 years ago

Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/29829

#19

Updated by Anja Leichsenring almost 7 years ago

  • Status changed from Under Review to Resolved
#20

Updated by Benni Mack over 2 years ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF