HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/innodrive/migrations/optimise_images.php
<?php

if( php_sapi_name() !== 'cli' ) {
    die("Meant to be run from command line");
}

if (!empty($argv[1])) {
    define('APPLICATION_ENV', $argv[1]);
} else {
    define('APPLICATION_ENV', 'production');
}

define( 'BASE_PATH', '../public/' );
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header, $wpdb;
require(BASE_PATH . 'wp-load.php');

$brands = ['constar/images', 'eds/images', 'elmo_images', 'fulling/images',
    'langyi/images', 'maxon', 'leaderdrive/images', 'maxon_images', 'npoat/images',
    'images/harmonic', 'img', 'iblock/d44', 'iblock/c04'];

$path = BASE_PATH . 'wp-content/uploads/';
$toOptimise = $total = 0;
foreach($brands as $brand) {
    echo $brand . PHP_EOL;
    echo PHP_EOL;

    $images = scandir($path . $brand);

    foreach($images as $image) {
        if (in_array($image, ['.', '..'])) continue;

        $imagePath = $path . $brand . '/' . $image;
        $imageSize = filesize($imagePath);

        if ($imageSize > 200000) {
            echo ' - ' . $imagePath . ' (' . ceil($imageSize / 1000) .'Kb)';
            $toOptimise++;

            $editor = wp_get_image_editor( $imagePath);

            if (is_wp_error( $editor )) {
                echo ' - image is broken' . PHP_EOL;
                continue;
            }

            $editor->resize( 450, 450);
            $editor->set_quality(70);
            $editor->save($imagePath);

            $imageSize = filesize($imagePath);
            echo ' => ' . ceil($imageSize / 1000) .'Kb' . PHP_EOL;
        }

        $total++;
    }

    echo PHP_EOL;

}

echo $toOptimise . ' / ' . $total . PHP_EOL;

echo 'DONE' . PHP_EOL . PHP_EOL;