Update find_image_size.php
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
@@ -7,19 +8,43 @@
|
|||||||
* based on the proportions you provide.
|
* based on the proportions you provide.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Include the image folders configuration
|
// Main script - choose configuration file
|
||||||
require_once __DIR__ . '/sites/default/settings/image_folders.php';
|
echo "\n";
|
||||||
|
echo "===========================================\n";
|
||||||
|
echo " Image Size Finder Tool\n";
|
||||||
|
echo "===========================================\n\n";
|
||||||
|
|
||||||
|
echo "Choose which configuration to load:\n";
|
||||||
|
echo " 1) Whole site (sites/default/settings/image_folders.php)\n";
|
||||||
|
echo " 2) Shop/Categories (shop/includes/image_folders.php)\n";
|
||||||
|
echo "\nYour choice (1-2): ";
|
||||||
|
|
||||||
|
$configChoice = trim(fgets(STDIN));
|
||||||
|
|
||||||
|
switch ($configChoice) {
|
||||||
|
case "1":
|
||||||
|
require_once __DIR__ . "/sites/default/settings/image_folders.php";
|
||||||
|
echo "✅ Loaded: Whole site configuration\n\n";
|
||||||
|
break;
|
||||||
|
case "2":
|
||||||
|
require_once __DIR__ . "/shop/includes/image_folders.php";
|
||||||
|
echo "✅ Loaded: Shop/Categories configuration\n\n";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
echo "\n❌ Invalid choice. Exiting.\n\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// Function to parse dimension input like "565x369px"
|
// Function to parse dimension input like "565x369px"
|
||||||
function parseDimensions($input)
|
function parseDimensions($input)
|
||||||
{
|
{
|
||||||
$input = strtolower(trim($input));
|
$input = strtolower(trim($input));
|
||||||
$input = str_replace('px', '', $input);
|
$input = str_replace("px", "", $input);
|
||||||
|
|
||||||
if (preg_match('/^(\d+)\s*x\s*(\d+)$/i', $input, $matches)) {
|
if (preg_match('/^(\d+)\s*x\s*(\d+)$/i', $input, $matches)) {
|
||||||
return [
|
return [
|
||||||
'width' => (int) $matches[1],
|
"width" => (int) $matches[1],
|
||||||
'height' => (int) $matches[2],
|
"height" => (int) $matches[2],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,8 +59,8 @@ function getAspectRatio($width, $height)
|
|||||||
};
|
};
|
||||||
$divisor = $gcd($width, $height);
|
$divisor = $gcd($width, $height);
|
||||||
return [
|
return [
|
||||||
'ratio' => $width / $height,
|
"ratio" => $width / $height,
|
||||||
'simplified' => $width / $divisor . ':' . $height / $divisor,
|
"simplified" => $width / $divisor . ":" . $height / $divisor,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,19 +68,30 @@ function getAspectRatio($width, $height)
|
|||||||
function calculateSimilarity($target, $existing)
|
function calculateSimilarity($target, $existing)
|
||||||
{
|
{
|
||||||
// Calculate aspect ratio difference (0-1, where 0 is perfect match)
|
// Calculate aspect ratio difference (0-1, where 0 is perfect match)
|
||||||
$ratioScore = abs($target['ratio'] - $existing['ratio']) / max($target['ratio'], $existing['ratio']);
|
$ratioScore =
|
||||||
|
abs($target["ratio"] - $existing["ratio"]) /
|
||||||
|
max($target["ratio"], $existing["ratio"]);
|
||||||
|
|
||||||
// Calculate size difference (0-1, where 0 is perfect match)
|
// Calculate size difference (0-1, where 0 is perfect match)
|
||||||
$targetSize = $target['width'] * $target['height'];
|
$targetSize = $target["width"] * $target["height"];
|
||||||
$existingSize = $existing['width'] * $existing['height'];
|
$existingSize = $existing["width"] * $existing["height"];
|
||||||
$sizeScore = abs($targetSize - $existingSize) / max($targetSize, $existingSize);
|
$sizeScore =
|
||||||
|
abs($targetSize - $existingSize) / max($targetSize, $existingSize);
|
||||||
|
|
||||||
// Calculate dimension differences
|
// Calculate dimension differences
|
||||||
$widthScore = abs($target['width'] - $existing['width']) / max($target['width'], $existing['width']);
|
$widthScore =
|
||||||
$heightScore = abs($target['height'] - $existing['height']) / max($target['height'], $existing['height']);
|
abs($target["width"] - $existing["width"]) /
|
||||||
|
max($target["width"], $existing["width"]);
|
||||||
|
$heightScore =
|
||||||
|
abs($target["height"] - $existing["height"]) /
|
||||||
|
max($target["height"], $existing["height"]);
|
||||||
|
|
||||||
// Weighted score (aspect ratio is most important)
|
// Weighted score (aspect ratio is most important)
|
||||||
$totalScore = $ratioScore * 0.5 + $sizeScore * 0.2 + $widthScore * 0.15 + $heightScore * 0.15;
|
$totalScore =
|
||||||
|
$ratioScore * 0.5 +
|
||||||
|
$sizeScore * 0.2 +
|
||||||
|
$widthScore * 0.15 +
|
||||||
|
$heightScore * 0.15;
|
||||||
|
|
||||||
return 1 - $totalScore; // Convert to similarity (higher is better)
|
return 1 - $totalScore; // Convert to similarity (higher is better)
|
||||||
}
|
}
|
||||||
@@ -66,20 +102,20 @@ function collectAllImageSizes($image_folders)
|
|||||||
$allSizes = [];
|
$allSizes = [];
|
||||||
|
|
||||||
foreach ($image_folders as $category => $sizes) {
|
foreach ($image_folders as $category => $sizes) {
|
||||||
if ($category === 'cms_images') {
|
if ($category === "cms_images") {
|
||||||
continue; // Skip cms_images as it's a merged array
|
continue; // Skip cms_images as it's a merged array
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($sizes as $sizeName => $config) {
|
foreach ($sizes as $sizeName => $config) {
|
||||||
if (isset($config['width'])) {
|
if (isset($config["width"])) {
|
||||||
$allSizes[] = [
|
$allSizes[] = [
|
||||||
'name' => $sizeName,
|
"name" => $sizeName,
|
||||||
'category' => $category,
|
"category" => $category,
|
||||||
'width' => $config['width'],
|
"width" => $config["width"],
|
||||||
'height' => $config['height'] ?? null,
|
"height" => $config["height"] ?? null,
|
||||||
'crop' => $config['crop'] ?? false,
|
"crop" => $config["crop"] ?? false,
|
||||||
'forced' => $config['forced'] ?? false,
|
"forced" => $config["forced"] ?? false,
|
||||||
'path' => $config['path'] ?? '',
|
"path" => $config["path"] ?? "",
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,13 +124,7 @@ function collectAllImageSizes($image_folders)
|
|||||||
return $allSizes;
|
return $allSizes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main script
|
echo "Enter image dimensions (e.g., 565x369px or 1920x1080): ";
|
||||||
echo "\n";
|
|
||||||
echo "===========================================\n";
|
|
||||||
echo " Image Size Finder Tool\n";
|
|
||||||
echo "===========================================\n\n";
|
|
||||||
|
|
||||||
echo 'Enter image dimensions (e.g., 565x369px or 1920x1080): ';
|
|
||||||
$input = trim(fgets(STDIN));
|
$input = trim(fgets(STDIN));
|
||||||
|
|
||||||
$dimensions = parseDimensions($input);
|
$dimensions = parseDimensions($input);
|
||||||
@@ -104,10 +134,10 @@ if (!$dimensions) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "\n📐 Analyzing dimensions: {$dimensions['width']}x{$dimensions['height']}px\n";
|
echo "\n📐 Analyzing dimensions: {$dimensions["width"]}x{$dimensions["height"]}px\n";
|
||||||
|
|
||||||
$targetRatio = getAspectRatio($dimensions['width'], $dimensions['height']);
|
$targetRatio = getAspectRatio($dimensions["width"], $dimensions["height"]);
|
||||||
echo " Aspect ratio: {$targetRatio['simplified']} ({$targetRatio['ratio']})\n\n";
|
echo " Aspect ratio: {$targetRatio["simplified"]} ({$targetRatio["ratio"]})\n\n";
|
||||||
|
|
||||||
// Collect all image sizes
|
// Collect all image sizes
|
||||||
$allSizes = collectAllImageSizes($image_folders);
|
$allSizes = collectAllImageSizes($image_folders);
|
||||||
@@ -115,27 +145,27 @@ $allSizes = collectAllImageSizes($image_folders);
|
|||||||
// Calculate similarity for each size
|
// Calculate similarity for each size
|
||||||
$matches = [];
|
$matches = [];
|
||||||
foreach ($allSizes as $size) {
|
foreach ($allSizes as $size) {
|
||||||
if ($size['height'] === null) {
|
if ($size["height"] === null) {
|
||||||
// Skip sizes without height (proportional scaling only)
|
// Skip sizes without height (proportional scaling only)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$existingRatio = getAspectRatio($size['width'], $size['height']);
|
$existingRatio = getAspectRatio($size["width"], $size["height"]);
|
||||||
$similarity = calculateSimilarity(
|
$similarity = calculateSimilarity(
|
||||||
array_merge($dimensions, ['ratio' => $targetRatio['ratio']]),
|
array_merge($dimensions, ["ratio" => $targetRatio["ratio"]]),
|
||||||
array_merge($size, ['ratio' => $existingRatio['ratio']]),
|
array_merge($size, ["ratio" => $existingRatio["ratio"]]),
|
||||||
);
|
);
|
||||||
|
|
||||||
$matches[] = array_merge($size, [
|
$matches[] = array_merge($size, [
|
||||||
'aspect_ratio' => $existingRatio['simplified'],
|
"aspect_ratio" => $existingRatio["simplified"],
|
||||||
'aspect_ratio_value' => $existingRatio['ratio'],
|
"aspect_ratio_value" => $existingRatio["ratio"],
|
||||||
'similarity' => $similarity,
|
"similarity" => $similarity,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort by similarity (best matches first)
|
// Sort by similarity (best matches first)
|
||||||
usort($matches, function ($a, $b) {
|
usort($matches, function ($a, $b) {
|
||||||
return $b['similarity'] <=> $a['similarity'];
|
return $b["similarity"] <=> $a["similarity"];
|
||||||
});
|
});
|
||||||
|
|
||||||
// Display results
|
// Display results
|
||||||
@@ -145,21 +175,21 @@ echo "===========================================\n\n";
|
|||||||
$topMatches = array_slice($matches, 0, 10);
|
$topMatches = array_slice($matches, 0, 10);
|
||||||
|
|
||||||
foreach ($topMatches as $index => $match) {
|
foreach ($topMatches as $index => $match) {
|
||||||
$matchPercent = round($match['similarity'] * 100, 1);
|
$matchPercent = round($match["similarity"] * 100, 1);
|
||||||
$matchBar = str_repeat('█', (int) ($matchPercent / 5));
|
$matchBar = str_repeat("█", (int) ($matchPercent / 5));
|
||||||
|
|
||||||
echo $index + 1 . '. ';
|
echo $index + 1 . ". ";
|
||||||
echo "\033[1m{$match['name']}\033[0m";
|
echo "\033[1m{$match["name"]}\033[0m";
|
||||||
echo " (Category: {$match['category']})\n";
|
echo " (Category: {$match["category"]})\n";
|
||||||
echo " Size: {$match['width']}x{$match['height']}px\n";
|
echo " Size: {$match["width"]}x{$match["height"]}px\n";
|
||||||
echo " Aspect Ratio: {$match['aspect_ratio']}\n";
|
echo " Aspect Ratio: {$match["aspect_ratio"]}\n";
|
||||||
echo " Match: {$matchBar} {$matchPercent}%\n";
|
echo " Match: {$matchBar} {$matchPercent}%\n";
|
||||||
echo " Path: {$match['path']}\n";
|
echo " Path: {$match["path"]}\n";
|
||||||
|
|
||||||
if ($match['crop']) {
|
if ($match["crop"]) {
|
||||||
echo ' ✂️ Cropped: Yes';
|
echo " ✂️ Cropped: Yes";
|
||||||
if ($match['forced']) {
|
if ($match["forced"]) {
|
||||||
echo ' (Forced)';
|
echo " (Forced)";
|
||||||
}
|
}
|
||||||
echo "\n";
|
echo "\n";
|
||||||
}
|
}
|
||||||
@@ -169,14 +199,15 @@ foreach ($topMatches as $index => $match) {
|
|||||||
|
|
||||||
// Check for exact matches
|
// Check for exact matches
|
||||||
$exactMatches = array_filter($matches, function ($m) use ($dimensions) {
|
$exactMatches = array_filter($matches, function ($m) use ($dimensions) {
|
||||||
return $m['width'] === $dimensions['width'] && $m['height'] === $dimensions['height'];
|
return $m["width"] === $dimensions["width"] &&
|
||||||
|
$m["height"] === $dimensions["height"];
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!empty($exactMatches)) {
|
if (!empty($exactMatches)) {
|
||||||
echo "\n✅ EXACT MATCH FOUND!\n";
|
echo "\n✅ EXACT MATCH FOUND!\n";
|
||||||
echo "===========================================\n";
|
echo "===========================================\n";
|
||||||
foreach ($exactMatches as $match) {
|
foreach ($exactMatches as $match) {
|
||||||
echo " • {$match['name']} ({$match['category']})\n";
|
echo " • {$match["name"]} ({$match["category"]})\n";
|
||||||
}
|
}
|
||||||
echo "\n";
|
echo "\n";
|
||||||
}
|
}
|
||||||
@@ -186,13 +217,13 @@ echo "\n💡 Recommendations:\n";
|
|||||||
echo "===========================================\n";
|
echo "===========================================\n";
|
||||||
|
|
||||||
$bestMatch = $topMatches[0];
|
$bestMatch = $topMatches[0];
|
||||||
$matchPercent = round($bestMatch['similarity'] * 100, 1);
|
$matchPercent = round($bestMatch["similarity"] * 100, 1);
|
||||||
|
|
||||||
if ($matchPercent >= 95) {
|
if ($matchPercent >= 95) {
|
||||||
echo "✅ USE EXISTING: '{$bestMatch['name']}' is very similar ({$matchPercent}% match)\n";
|
echo "✅ USE EXISTING: '{$bestMatch["name"]}' is very similar ({$matchPercent}% match)\n";
|
||||||
echo " You can probably use this size instead.\n";
|
echo " You can probably use this size instead.\n";
|
||||||
} elseif ($matchPercent >= 80) {
|
} elseif ($matchPercent >= 80) {
|
||||||
echo "⚠️ CONSIDER: '{$bestMatch['name']}' is fairly similar ({$matchPercent}% match)\n";
|
echo "⚠️ CONSIDER: '{$bestMatch["name"]}' is fairly similar ({$matchPercent}% match)\n";
|
||||||
echo " Evaluate if this size meets your needs or create a new one.\n";
|
echo " Evaluate if this size meets your needs or create a new one.\n";
|
||||||
} else {
|
} else {
|
||||||
echo "🆕 CREATE NEW: No close matches found (best match: {$matchPercent}%)\n";
|
echo "🆕 CREATE NEW: No close matches found (best match: {$matchPercent}%)\n";
|
||||||
@@ -209,21 +240,21 @@ echo "\nYour choice (1-3): ";
|
|||||||
$choice = trim(fgets(STDIN));
|
$choice = trim(fgets(STDIN));
|
||||||
|
|
||||||
switch ($choice) {
|
switch ($choice) {
|
||||||
case '1':
|
case "1":
|
||||||
echo "\n✅ Great! Use the size name from the list above in your image configuration.\n";
|
echo "\n✅ Great! Use the size name from the list above in your image configuration.\n";
|
||||||
break;
|
break;
|
||||||
case '2':
|
case "2":
|
||||||
echo "\n📝 To create a new size, add it to: sites/default/settings/image_folders.php\n";
|
echo "\n📝 To create a new size, add it to: sites/default/settings/image_folders.php\n";
|
||||||
echo " Use this template:\n\n";
|
echo " Use this template:\n\n";
|
||||||
echo " 'your_size_name' => [\n";
|
echo " 'your_size_name' => [\n";
|
||||||
echo " 'path' => 'uploads/images/your_path',\n";
|
echo " 'path' => 'uploads/images/your_path',\n";
|
||||||
echo " 'width' => {$dimensions['width']},\n";
|
echo " 'width' => {$dimensions["width"]},\n";
|
||||||
echo " 'height' => {$dimensions['height']},\n";
|
echo " 'height' => {$dimensions["height"]},\n";
|
||||||
echo " 'forced' => true,\n";
|
echo " 'forced' => true,\n";
|
||||||
echo " 'crop' => true,\n";
|
echo " 'crop' => true,\n";
|
||||||
echo " ],\n\n";
|
echo " ],\n\n";
|
||||||
break;
|
break;
|
||||||
case '3':
|
case "3":
|
||||||
echo "\n👋 Goodbye!\n";
|
echo "\n👋 Goodbye!\n";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -232,5 +263,3 @@ switch ($choice) {
|
|||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user