User Guide: ServBay Built-in Command-Line Image Processing Tools
As a local web development environment tailored for developers, ServBay not only offers a rich variety of language runtimes and databases, but also comes packed with powerful command-line image processing utilities. These tools are essential for common web development tasks such as image format conversion, compression and optimization, resizing, watermarking, and more. This article provides a detailed overview of the main image processing tools bundled with ServBay, along with practical command-line usage tips to help you efficiently manage and process image files during local development.
Prerequisites
Ensure you have successfully installed and are running ServBay. All the tools covered in this document come pre-installed with ServBay—there is no need for any additional installation or configuration.
Popular Image Processing Tools
ServBay comes with the following widely used command-line image processing tools:
ImageMagick
ImageMagick is a comprehensive, long-standing suite and library for image processing, supporting nearly all major image formats. Developers frequently use it for format conversion, resizing, applying complex effects, and more.
Basic Usage
Convert Image Format Convert a JPEG image to PNG:
bashmagick convert input.jpg output.png
1Resize Image Resize an image to 300x300 pixels:
bashmagick convert input.jpg -resize 300x300 output.jpg
1Add a Watermark Add a "ServBay" text watermark to the bottom right corner of an image:
bashmagick convert input.jpg -gravity southeast -draw "text 10,10 'ServBay'" output.jpg
1
cwebp
WebP is a modern image format developed by Google, designed to provide superior lossless and lossy compression compared to JPEG and PNG. cwebp
is the official command-line tool for converting images into WebP format.
Basic Usage
Convert JPEG to WebP
bashcwebp input.jpg -o output.webp
1Convert PNG to WebP
bashcwebp input.png -o output.webp
1Set Compression Quality Use the
-q
parameter to set the output WebP quality (0–100), e.g., set to 80:bashcwebp -q 80 input.jpg -o output.webp
1
jpegtran
jpegtran
is a utility provided by the libjpeg library, specialized for lossless transformations of JPEG images such as rotation, flipping, cropping, etc.—ideal for optimizing or adjusting orientation without sacrificing image quality.
Basic Usage
Rotate JPEG Image Losslessly rotate a JPEG image by 90 degrees:
bashjpegtran -rotate 90 input.jpg > output.jpg
1Note: Here, redirection
>
is used to write the output to a file.Flip JPEG Image Horizontally
bashjpegtran -flip horizontal input.jpg > output.jpg
1
djpeg
djpeg
is another tool from the libjpeg library. It decodes (decompresses) JPEG files into raw pixel data formats such as PPM (Portable Pixmap), making it easy to perform pixel-level processing or convert to other non-JPEG formats.
Basic Usage
- Decompress JPEG to PPM Formatbash
djpeg input.jpg > output.ppm
1
cjpeg
Conversely, cjpeg
encodes (compresses) raw pixel data (such as PPM) into JPEG files. It's commonly used together with djpeg
, or for converting raw images generated by other tools into JPEG format.
Basic Usage
- Compress PPM to JPEG Formatbash
cjpeg input.ppm > output.jpg
1
img2webp
img2webp
is part of the WebP tools suite, designed to create animated WebP files by combining multiple static images (such as PNG, JPEG), similar to creating GIF animations.
Basic Usage
- Merge Multiple Images into Animated WebP Merge
frame1.png
,frame2.png
, andframe3.png
into a single animated WebP fileoutput.webp
:bashimg2webp -o output.webp frame1.png frame2.png frame3.png
1
Practical Examples & Advanced Tips
With these command-line tools, you can automate even complex image processing tasks. Here are some useful examples:
Batch Resize Images
By combining ImageMagick with a shell script, you can easily batch resize all JPEG images in the current directory. For example, resize them all to a width of 300 pixels (height scaled proportionally):
# Enter your image directory, for example: cd /Applications/ServBay/www/your-project/images
for file in *.jpg; do
# Use magick convert to resize; -resize 300x scales height proportionally
# Save the result as a new file, or to a new directory to avoid overwriting
magick convert "$file" -resize 300x "${file%.*}-resized.jpg"
echo "Processed: $file -> ${file%.*}-resized.jpg"
done
2
3
4
5
6
7
8
Tip: ${file%.*}
is shell parameter expansion that removes the filename extension.
Batch Convert and Optimize Image Formats
Use cwebp
to batch convert all PNG or JPEG images in the current directory with names starting with servbay
to WebP format, and set the compression quality to 45:
# Navigate to your image directory
for file in servbay*.jpg servbay*.png; do
# Construct the output filename by changing the extension to .webp
output_file="${file%.*}.webp"
# Use cwebp for conversion; -q sets the quality
cwebp -q 45 "$file" -o "$output_file"
echo "Converted: $file -> $output_file (Quality 45)"
done
2
3
4
5
6
7
8
Batch Add Watermarks
Combine ImageMagick with a shell script to batch add a "ServBay" text watermark to all JPEG images in the current directory:
# Navigate to your image directory
for file in *.jpg; do
# Add 'ServBay Demo' text watermark to the bottom right corner
# Save the result as a new file
magick convert "$file" -gravity southeast -pointsize 20 -fill white -annotate +10+10 'ServBay Demo' "${file%.*}-watermarked.jpg"
echo "Watermarked: $file -> ${file%.*}-watermarked.jpg"
done
2
3
4
5
6
7
Optimize JPEG Image Size
Use jpegtran
to losslessly optimize JPEG images, remove unnecessary data, and convert to progressive JPEG (which helps improve perceived webpage loading speed):
jpegtran -optimize -progressive input.jpg > output.jpg
Note: jpegtran
writes output to standard output, so you need to redirect it to a file.
Notes
- PATH Environment Variable: ServBay adds its built-in tools to the system PATH, allowing you to run commands such as
magick
,cwebp
,jpegtran
, etc., directly from the terminal. If you encounter "command not found" errors, please check whether ServBay is running properly and if your system PATH is correctly configured. - Error Handling: Command-line tools typically output error messages when something goes wrong. When writing batch scripts, it’s advisable to add error-checking mechanisms.
- File Overwriting: Many command-line tools will overwrite output files by default. When processing files in batches, operate with caution: consider saving outputs to a separate directory or use different file names, and only replace the original files once you're sure everything is correct.
Frequently Asked Questions (FAQ)
Q: What should I do if I get 'command not found' when typing magick
in the terminal?
A: Please ensure that the environment variables for ServBay are correctly set up. If you’re still experiencing issues, try opening ServBay’s "Settings" > "Command-Line Tools" and configure for both zsh
and bash
, then reopen the terminal.
Q: What image formats do these tools support?
A: ImageMagick supports hundreds of formats, including JPEG, PNG, GIF, TIFF, BMP, SVG, and more. cwebp
is specialized for WebP conversions. jpegtran
, djpeg
, and cjpeg
mainly handle JPEG and related raw formats. For exact supported formats, please refer to each tool’s official documentation.
Q: Can I use these tools in PHP, Python, or other scripts?
A: Yes! You can invoke these command-line tools from scripts running in ServBay’s environment using PHP (exec()
, shell_exec()
, proc_open()
), Python (the subprocess
module), Node.js (the child_process
module), and more, to process uploaded images or handle various tasks.
Summary
As an all-in-one local development solution, ServBay greatly simplifies a developer’s workflow when handling image assets by providing these powerful built-in command-line image processing tools (such as ImageMagick, cwebp, jpegtran, and others). No extra installation or configuration is needed—simply use these tools directly for format conversion, compression, optimization, and automation of batch image processing tasks. Mastering these tools will notably improve your productivity and flexibility when working with images in web development projects. Start exploring the full capabilities of these tools in the ServBay environment today!