2

压缩博客图片尺寸的 Bash 脚本(Linux)

 3 years ago
source link: https://iphyer.github.io/blog/2017/05/13/shrinkingimages/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

压缩博客图片尺寸的 Bash 脚本(Linux)

之前一直没管博客图片的大小因为感觉其实不太重要,毕竟是 Github 的免费空间,没有那么大的动力优化图片。

但是最近发现有的博客确实图片太多了,所以加载起来很慢,所以才开始了优化。

现在把最后的成品写成这个博客。

原理非常简单,列出当前文件夹全部的图片文件(JPG,PNG),然后处理所有的大于 1M 的图片。

然后循环运行脚本,知道不在出现大于 1M 的图片即可。


#! /bin/bash 

#获取脚本所在文件目录
#大于1M图片缩小一半




    for file in  `find ./ -name "*.[jJ][pP][gG]"`;
    do      
        if [ `stat --printf="%s" $file` -gt 1000000 ]
        then
            echo $file
            mogrify -resize 50% $file
        fi

    done

    for file in  `find ./ -name "*.[pP][nN][gG]"`;
    do 
        if [ `stat --printf="%s" $file` -gt 1000000 ]
        then
            echo $file
            mogrify -resize 50% $file
        fi
    done


echo "Done"



最后配图文件夹减少了 2/3 的体积还是很不错的。

Written on May 13, 2017

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK