Install Jpegoptim
If you haven’t jpegoptim installed on your environment execute the following in your terminal:
1 |
$ apt-get install jpegoptim |
And if you are running on Mac, make sure you have Homebrew install (https://brew.sh/index_es.html)
1 2 3 4 5 |
$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null $ brew link jpeg $ brew install jpegoptim |
Optimize jpg
In your terminal, go to your project directory and execute the following command:
1 |
$ find ./ -name '*.jpg' -type f -print0 | xargs -0 jpegoptim -o --strip-all |
This will look and optimize each jpg image in your current directory and sub directories.
Jpegtran
Most of the servers already has installed jpegtran. If that is your case, you can execute the following command
1 |
find ./ -name "*.jpg" -exec jpegtran -copy none -optimize -outfile "{}" "{}" \; |
That will scan each file with the .jpg extension and optimize it by saving the changes with the same filename.
ImageMagick
Install ImageMagick on your server.
If you are using OSX
1 |
brew install imagemagick |
Ubuntu
1 |
apt-get install ImageMagick |
CentOS
1 |
yum install ImageMagick |
If you want to resize a single image, you can do something like this
1 |
convert image-to-convert.jpg -resize 350x350 image-converted.jpg # You can use the same filename if you don't want a new image file |
Resize only if size if current bigger than new size
1 2 |
convert small.png -resize 800x600\> a.png # 300x200 convert large.png -resize 800x600\> b.png # 800x400 # This will be resized |
NOTE: You may need a caret (^
) rather than a backslash on Windows.
Convert to another image type
1 |
convert source.png target.pg |
Change image quality
1 |
convert image.jpg -quality 80 image.jpg # the quality value must be between 1 and 100 |
If you want to perform a bulk task on a directory, you can create a bash script like the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/bin/bash DIR=/home/youruser/magento/html echo "($(date '+%d/%m/%Y %H:%M:%S')): Resize Images process started" IMAGES=$(eval "find ${DIR}/pub/media/catalog \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' -o -name '*.gif' \) -type f -size +750k") for f in $IMAGES; do echo "Resizing $f... " convert $f -quality 70 -resize 800x800\> $f done echo "($(date '+%d/%m/%Y %H:%M:%S')): Resize Images process finished!" |
This will resize all of the images inside pub/media/attribute and force the width to be 350, respecting the aspect ratio.