ShortPixel Command Line Tool

ShortPixel Command Line Tool can optimize image folders from the command line, so it can be configured as a Cron (or other task scheduler) job. You need to have PHP installed in order for it to run.

Installation

Download the latest stable release: ShortPixel PHP SDK v. 1.1.3 and unpack it. You should find a shortpixel-php-master subfolder that was extracted from the archive. Inside shortpixel-php-master/lib you find cmdShortpixelOptimize.php which is the main script of the ShortPixel Command Line Tool.

Optimizing folders

Manually: Run the following commands, while in the site's root folder:


 cd shortpixel-php-master/lib

 php cmdShortpixelOptimize.php --apiKey=<<YOUR_API_KEY>> --folder=<<FOLDER_PATH>> 

The Command Line Tool accepts the following parameters:

Parameter Meaning
apiKey Required. Your API Key. If you don't have one yet, you can request one for free in our Sign-up page.
folder Required. The path of the folder, either full or relative to the current folder. You need to have write rights to all files and subfolders in it.
compression Optional. The compression level, 1 for lossy, 2 for glossy and 0 for lossless. Defaults to 1 - Lossy.
Specify as --compression=1
targetFolder Optional. The absolute path of the destination folder where the optimized files will be saved. If specified, cannot be either a subfolder or a parent folder of the source folder. If not specified, the original images will be overwritten by the optimized images.
Specify as --backupBase=/path/to/backup/folder/
webPath Optional. Map the folder to a web URL and have the ShortPixel servers download the images instead of posting them (less heavy on memory for large files).
Specify as --webPath=http://yoursites.address/img/folder/
backupBase Optional. The full path of the base directory where to back up the optimized images. If the backupBase is outside the folder, the name of the source folder will be created as subdirectory. If it's the parent of the source folder, a folder with the _SP_BKP suffix will be created alongside the source folder. If it's inside the source folder, a ShortPixelBackups subfolder will be created and backups stored there. If backupBase is not specified, no backup copies are made.
Specify as --backupBase=<<BACKUPS_FOLDER_PATH>>
speed Optional. Set between 1 and 10 - default is 10 but if you have large images it will eat up a lot of memory when creating the post messages so sometimes you might need to lower it. Not needed when using the webPath mapping.
Specify as --speed=X
verbose Optional. Display info about the files optimized. Specify as --verbose
clearLock Optional. Clear a lock that's already placed on the folder. BE SURE you know what you're doing, files might get corrupted if the previous script is still running. The locks expire in 6 minutes. Specify as --clearLock

The Command Line Tool will fallback to the .sp-options file for additional options; if an option is specified in the command line but is present also in the .sp-options file, the command-line value will prevail (but it will not update it in the .sp-options file). The .sp-options file should be present in the source folder and has the following structure:

[SHORTPIXEL]
lossy=1
keep_exif=0
cmyk2rgb=1
resize=1
resize_width=1024
resize_height=1024
convertto=
backup_path=
exclude=
base_url=http://your.domain/web/path/to/files

Cron job: add the same command to your scheduler, optionally appending  2>&1 1>/your/log/file. The ShortPixel Command Line Tool has a locking mechanism in place, so if another instance is already running on the same folder, it exits.