File: /var/www/innodrive/migrations/migrate_thumbs.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');
require(BASE_PATH . 'wp-content/plugins/custom-search/custom-search.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Product.php');
$params = $wpdb->get_results(' SELECT * FROM params WHERE name = "Picture URL"');
$cache = [];
foreach($params as $param) {
$oldThumbImage = Product::getMeta($param->product_id, 'Thumbnail URL');
$bigImage = $param->value;
echo $param->product_id . ': ' . $oldThumbImage . PHP_EOL;
echo $bigImage . PHP_EOL;
if ($bigImage === $oldThumbImage) {
echo ' - same image skip' . PHP_EOL;
continue;
}
if (!empty($cache[$bigImage])) {
Product::updateMeta($param->product_id, 'Thumbnail URL', $cache[$bigImage]);
echo ' - saved from cache' . PHP_EOL;
} else {
$editor = wp_get_image_editor( BASE_PATH . $bigImage);
if (is_wp_error( $editor )) {
echo ' - image is broken' . PHP_EOL;
continue;
}
$editor->resize( 320, 320);
$editor->save(BASE_PATH . $oldThumbImage);
$cache[$bigImage] = $oldThumbImage;
echo ' - new thumb created' . PHP_EOL;
}
}
echo 'DONE' . PHP_EOL . PHP_EOL;