Socket
Book a DemoInstallSign in
Socket

wp-sharp-image

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wp-sharp-image

High-performance image processing for WordPress using Sharp and Bun

1.0.0
latest
npmnpm
Version published
Maintainers
1
Created
Source

WordPress Sharp Image Processing

High-performance image processing for WordPress using Sharp and Bun. This module monitors the WordPress uploads directory and automatically processes new images according to WordPress image size configurations, providing a significant performance improvement over PHP-based image processing.

Features

  • πŸš€ High Performance: Uses Sharp (libvips) for extremely fast image processing
  • πŸ“ Real-time Monitoring: Watches WordPress uploads directory for new images
  • πŸ”„ WordPress Integration: Reads image sizes directly from WordPress database
  • 🎨 Modern Formats: Optional WebP and AVIF generation
  • πŸ“Š Monitoring & Logging: Comprehensive logging and statistics
  • πŸ›‘οΈ Graceful Shutdown: Proper cleanup and error handling
  • βš™οΈ Configurable: Extensive configuration options

Requirements

  • Bun: >= 1.0.0
  • Node.js: 18.17.0+ (for dependencies)
  • WordPress: 5.0+
  • WP-CLI: Latest version (for WordPress integration)

Installation

  • Navigate to the module directory:

    cd wp-sharp-image
    
  • Install dependencies:

    bun install
    
  • Run the interactive setup wizard:

    bun run configure
    

    This will:

    • Check system requirements
    • Detect available process managers (supervisor, systemd, PM2)
    • Create configuration file from template
    • Guide you through service setup
    • Configure the selected process manager automatically

Manual Setup

  • Install dependencies:

    cd wp-sharp-image
    bun install
    
  • Check system requirements:

    bun run check
    
  • Configure the application:

    cp config.example.js config.js
    

    Edit config.js with your WordPress installation paths.

  • Test the configuration:

    bun run start
    
  • Check service status:

    bun run status
    

Configuration

Copy config.example.js to config.js and modify according to your setup:

WordPress Integration

The system uses wp-cli to interact with WordPress, so no database credentials are needed. WP-CLI uses WordPress's own database configuration from wp-config.php.

WP-CLI Configuration

You can pass additional arguments to all wp-cli commands via the wpCliArgs array:

wordpress: {
    // ... other settings
    wpCliArgs: [
        '--url=example.com',        // Required for multisite
        '--user=admin',             // Run commands as specific user
        '--skip-plugins',           // Skip plugin loading for performance
        '--skip-themes',            // Skip theme loading for performance
        '--debug',                  // Enable wp-cli debug output
        '--quiet',                  // Suppress informational messages
        '--allow-root'              // Allow running as root user
    ]
}

Common Use Cases:

  • Multisite: Add --url=your-site.com to target specific site
  • Performance: Add --skip-plugins and --skip-themes to speed up commands
  • Debugging: Add --debug for detailed wp-cli output
  • Production: Add --quiet to reduce log verbosity

WordPress Paths

wordpress: {
    rootPath: '/path/to/wordpress',
    uploadsPath: '/path/to/wordpress/wp-content/uploads',
    contentPath: '/path/to/wordpress/wp-content',
    
    // Additional wp-cli arguments (optional)
    wpCliArgs: [
        '--url=example.com',     // For multisite
        '--user=admin',          // Run as specific user
        '--skip-plugins',        // Skip plugin loading for performance
        '--quiet'                // Suppress informational messages
    ]
}

Image Processing Settings

images: {
    quality: {
        jpeg: 90,
        webp: 80,
        png: 100,
        avif: 75
    },
    progressive: true,
    optimize: true,
    modernFormats: {
        webp: true,
        avif: false
    },
    concurrency: 4,
    backupOriginals: true
}

Usage

Development Mode

bun run dev

Production Mode

bun run start

Background Service

Using PM2

npm install -g pm2
cp supervisor/pm2.config.cjs ecosystem.config.cjs
# Edit ecosystem.config.cjs to update paths
pm2 start ecosystem.config.cjs --env production
pm2 save
pm2 startup

Using Supervisor

sudo cp supervisor/supervisor.conf /etc/supervisor/conf.d/wp-sharp-image.conf
# Edit the configuration file to update paths and user
sudo nano /etc/supervisor/conf.d/wp-sharp-image.conf

Then reload supervisor:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start wp-sharp-image

Using systemd

sudo cp supervisor/systemd.service /etc/systemd/system/wp-sharp-image.service
# Edit the service file to update paths and user
sudo nano /etc/systemd/system/wp-sharp-image.service

Enable and start the service:

sudo systemctl daemon-reload
sudo systemctl enable wp-sharp-image
sudo systemctl start wp-sharp-image

WordPress Integration

Disable PHP Image Processing

Add this to your WordPress functions.php or a plugin:

// Disable WordPress image resizing
add_filter('intermediate_image_sizes_advanced', '__return_empty_array');

// Optional: Remove default image sizes
add_filter('intermediate_image_sizes', function($sizes) {
    return [];
});

// Prevent WordPress from generating thumbnails on upload
add_filter('wp_generate_attachment_metadata', function($metadata, $attachment_id) {
    // Let Sharp handle the processing
    return $metadata;
}, 10, 2);

Monitor Processing

Check the logs to ensure images are being processed:

tail -f logs/wp-sharp-image.log

File Structure

dev/wp-sharp-image/
β”œβ”€β”€ index.js                 # Main application entry point
β”œβ”€β”€ setup.js                 # Interactive setup wizard
β”œβ”€β”€ package.json             # Dependencies and scripts
β”œβ”€β”€ config.example.js        # Example configuration
β”œβ”€β”€ config.js                # Your configuration (gitignored)
β”œβ”€β”€ README.md                # This file
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ Database.js          # WordPress data access via wp-cli
β”‚   β”œβ”€β”€ ImageProcessor.js    # Sharp image processing logic
β”‚   β”œβ”€β”€ FileWatcher.js       # File system monitoring
β”‚   β”œβ”€β”€ Logger.js            # Logging utilities
β”‚   └── Prompt.js            # User interaction utilities
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ check-requirements.js # System requirements checker
β”‚   └── service-status.js    # Service status checker
β”œβ”€β”€ logs/                    # Log files (auto-created)
β”‚   β”œβ”€β”€ wp-sharp-image.log   # Main log file
β”‚   β”œβ”€β”€ exceptions.log       # Uncaught exceptions
β”‚   └── rejections.log       # Unhandled rejections
└── supervisor/              # Service configuration examples
    β”œβ”€β”€ supervisor.conf      # Supervisor configuration
    β”œβ”€β”€ systemd.service      # systemd service file
    └── pm2.config.js        # PM2 configuration

Monitoring

The service provides comprehensive monitoring and statistics:

Log Levels

  • error: Critical errors that require attention
  • warn: Warnings that should be monitored
  • info: General information and status updates
  • debug: Detailed debugging information

Statistics Reporting

When monitoring is enabled, the service reports statistics including:

  • Total images processed
  • Processing errors
  • Memory usage
  • File watcher statistics
  • Processing performance metrics

Health Checks

Monitor the service health by checking:

  • Log files for errors
  • Process status (ps aux | grep bun)
  • Database connectivity
  • File system permissions

Troubleshooting

Common Issues

Permission Errors

# Fix file permissions
sudo chown -R www-data:www-data /path/to/wordpress/wp-content/uploads
sudo chmod -R 755 /path/to/wordpress/wp-content/uploads

Database Connection Issues

  • Verify database credentials in config.js
  • Ensure MySQL/MariaDB is running
  • Check firewall settings
  • Verify user permissions

File Watcher Issues

  • Check if the uploads directory exists and is writable
  • Ensure sufficient inotify watches: echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf
  • Restart the service after making changes

Memory Issues

  • Adjust Sharp concurrency in configuration
  • Monitor memory usage with htop or similar
  • Consider increasing system memory for large images

Performance Tuning

Optimal Settings

images: {
    concurrency: Math.min(4, os.cpus().length), // Match CPU cores
    quality: {
        jpeg: 85,  // Balance quality/size
        webp: 80,  // WebP is more efficient
        png: 95,   // PNG quality less critical
        avif: 70   // AVIF very efficient
    }
}

System Optimization

  • Use SSD storage for better I/O performance
  • Ensure adequate RAM (4GB+ recommended)
  • Consider dedicated processing server for high-volume sites

Available Commands

Setup and Configuration

bun run configure        # Interactive setup wizard
bun run check           # Check system requirements
bun run status          # Check service status across all process managers

Development and Debugging

bun run start           # Start the service
bun run dev             # Start in development mode with auto-reload
bun test                # Run tests
bun run lint            # Check code style

Service Management

Once configured, you can manage the service with:

Supervisor:

sudo supervisorctl start wp-sharp-image
sudo supervisorctl stop wp-sharp-image
sudo supervisorctl restart wp-sharp-image
sudo supervisorctl status wp-sharp-image

systemd:

sudo systemctl start wp-sharp-image
sudo systemctl stop wp-sharp-image
sudo systemctl restart wp-sharp-image
sudo systemctl status wp-sharp-image

PM2:

pm2 start wp-sharp-image
pm2 stop wp-sharp-image
pm2 restart wp-sharp-image
pm2 status wp-sharp-image
pm2 logs wp-sharp-image

Development

Running Tests

bun test

Code Linting

bun run lint

Contributing

  • Fork the repository
  • Create a feature branch
  • Make your changes
  • Add tests if applicable
  • Submit a pull request

License

GPL-2.0+ - Same as WordPress

Support

For issues and questions:

  • Check the logs first
  • Review this README
  • Check WordPress and system requirements
  • Create an issue with detailed information

Note: This module is designed to replace WordPress's built-in image processing. Ensure you have backups and test thoroughly before deploying to production.

Keywords

wordpress

FAQs

Package last updated on 19 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.