How the shell compresses the pictures in the specified directory and subdirectories in batches

Source: Internet
Author: User

The user uploads the picture, generally has not been compressed, causes the space to waste. So you need to write a program that looks up a picture file (jpg,gif,png) of directories and subdirectories, and compresses pictures that are larger than a certain value.

The code is as follows:

#!/bin/bash  
     
# Look up a picture file (jpg,gif,png) for a directory and subdirectories, compress a picture that is larger than a value in  
     
# Config  
     
folderpath= '/home/fdipzone/photo '   # Picture directory path  
     
maxsize= ' 1M '    # picture size allowable value  
maxwidth=1280   # picture max width  
maxheight=1280  # picture Max height  
QUALITY=85 #      image quality  
     
     
# compression processing  
# Param $folderPath picture directory  
function compress () {  
     
    folderpath=$1  
     
    If [-D "$folderPath"];  Then for  
     
        file in $ (find "$folderPath" (-name "*.jpg"-or-name "*.gif"-or-name "*.png")-type f-size + "$maxSize" ); Do  
     
            echo $file  
     
            # call ImageMagick resize picture  
            $ (convert-resize "$maxWidth" x "$maxHeight" "$file"-quality "$ Quality "$file")  
        performed else echo "$folderPath NOT exists"  
    fi  
}  
     
# Execute Compress  
Compress "$folderPath"  
     
exit 0

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.