Update find_image_size.php

This commit is contained in:
2025-12-15 15:27:55 +02:00
parent ff7e2fef93
commit a167c6d788

View File

@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php
/**
@@ -7,19 +8,43 @@
* based on the proportions you provide.
*/
// Include the image folders configuration
require_once __DIR__ . '/sites/default/settings/image_folders.php';
// Main script - choose configuration file
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 parseDimensions($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)) {
return [
'width' => (int) $matches[1],
'height' => (int) $matches[2],
"width" => (int) $matches[1],
"height" => (int) $matches[2],
];
}
@@ -34,8 +59,8 @@ function getAspectRatio($width, $height)
};
$divisor = $gcd($width, $height);
return [
'ratio' => $width / $height,
'simplified' => $width / $divisor . ':' . $height / $divisor,
"ratio" => $width / $height,
"simplified" => $width / $divisor . ":" . $height / $divisor,
];
}
@@ -43,19 +68,30 @@ function getAspectRatio($width, $height)
function calculateSimilarity($target, $existing)
{
// 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)
$targetSize = $target['width'] * $target['height'];
$existingSize = $existing['width'] * $existing['height'];
$sizeScore = abs($targetSize - $existingSize) / max($targetSize, $existingSize);
$targetSize = $target["width"] * $target["height"];
$existingSize = $existing["width"] * $existing["height"];
$sizeScore =
abs($targetSize - $existingSize) / max($targetSize, $existingSize);
// Calculate dimension differences
$widthScore = abs($target['width'] - $existing['width']) / max($target['width'], $existing['width']);
$heightScore = abs($target['height'] - $existing['height']) / max($target['height'], $existing['height']);
$widthScore =
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)
$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)
}
@@ -66,20 +102,20 @@ function collectAllImageSizes($image_folders)
$allSizes = [];
foreach ($image_folders as $category => $sizes) {
if ($category === 'cms_images') {
if ($category === "cms_images") {
continue; // Skip cms_images as it's a merged array
}
foreach ($sizes as $sizeName => $config) {
if (isset($config['width'])) {
if (isset($config["width"])) {
$allSizes[] = [
'name' => $sizeName,
'category' => $category,
'width' => $config['width'],
'height' => $config['height'] ?? null,
'crop' => $config['crop'] ?? false,
'forced' => $config['forced'] ?? false,
'path' => $config['path'] ?? '',
"name" => $sizeName,
"category" => $category,
"width" => $config["width"],
"height" => $config["height"] ?? null,
"crop" => $config["crop"] ?? false,
"forced" => $config["forced"] ?? false,
"path" => $config["path"] ?? "",
];
}
}
@@ -88,13 +124,7 @@ function collectAllImageSizes($image_folders)
return $allSizes;
}
// Main script
echo "\n";
echo "===========================================\n";
echo " Image Size Finder Tool\n";
echo "===========================================\n\n";
echo 'Enter image dimensions (e.g., 565x369px or 1920x1080): ';
echo "Enter image dimensions (e.g., 565x369px or 1920x1080): ";
$input = trim(fgets(STDIN));
$dimensions = parseDimensions($input);
@@ -104,10 +134,10 @@ if (!$dimensions) {
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']);
echo " Aspect ratio: {$targetRatio['simplified']} ({$targetRatio['ratio']})\n\n";
$targetRatio = getAspectRatio($dimensions["width"], $dimensions["height"]);
echo " Aspect ratio: {$targetRatio["simplified"]} ({$targetRatio["ratio"]})\n\n";
// Collect all image sizes
$allSizes = collectAllImageSizes($image_folders);
@@ -115,27 +145,27 @@ $allSizes = collectAllImageSizes($image_folders);
// Calculate similarity for each size
$matches = [];
foreach ($allSizes as $size) {
if ($size['height'] === null) {
if ($size["height"] === null) {
// Skip sizes without height (proportional scaling only)
continue;
}
$existingRatio = getAspectRatio($size['width'], $size['height']);
$existingRatio = getAspectRatio($size["width"], $size["height"]);
$similarity = calculateSimilarity(
array_merge($dimensions, ['ratio' => $targetRatio['ratio']]),
array_merge($size, ['ratio' => $existingRatio['ratio']]),
array_merge($dimensions, ["ratio" => $targetRatio["ratio"]]),
array_merge($size, ["ratio" => $existingRatio["ratio"]]),
);
$matches[] = array_merge($size, [
'aspect_ratio' => $existingRatio['simplified'],
'aspect_ratio_value' => $existingRatio['ratio'],
'similarity' => $similarity,
"aspect_ratio" => $existingRatio["simplified"],
"aspect_ratio_value" => $existingRatio["ratio"],
"similarity" => $similarity,
]);
}
// Sort by similarity (best matches first)
usort($matches, function ($a, $b) {
return $b['similarity'] <=> $a['similarity'];
return $b["similarity"] <=> $a["similarity"];
});
// Display results
@@ -145,21 +175,21 @@ echo "===========================================\n\n";
$topMatches = array_slice($matches, 0, 10);
foreach ($topMatches as $index => $match) {
$matchPercent = round($match['similarity'] * 100, 1);
$matchBar = str_repeat('█', (int) ($matchPercent / 5));
$matchPercent = round($match["similarity"] * 100, 1);
$matchBar = str_repeat("", (int) ($matchPercent / 5));
echo $index + 1 . '. ';
echo "\033[1m{$match['name']}\033[0m";
echo " (Category: {$match['category']})\n";
echo " Size: {$match['width']}x{$match['height']}px\n";
echo " Aspect Ratio: {$match['aspect_ratio']}\n";
echo $index + 1 . ". ";
echo "\033[1m{$match["name"]}\033[0m";
echo " (Category: {$match["category"]})\n";
echo " Size: {$match["width"]}x{$match["height"]}px\n";
echo " Aspect Ratio: {$match["aspect_ratio"]}\n";
echo " Match: {$matchBar} {$matchPercent}%\n";
echo " Path: {$match['path']}\n";
echo " Path: {$match["path"]}\n";
if ($match['crop']) {
echo ' ✂️ Cropped: Yes';
if ($match['forced']) {
echo ' (Forced)';
if ($match["crop"]) {
echo " ✂️ Cropped: Yes";
if ($match["forced"]) {
echo " (Forced)";
}
echo "\n";
}
@@ -169,14 +199,15 @@ foreach ($topMatches as $index => $match) {
// Check for exact matches
$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)) {
echo "\n✅ EXACT MATCH FOUND!\n";
echo "===========================================\n";
foreach ($exactMatches as $match) {
echo "{$match['name']} ({$match['category']})\n";
echo "{$match["name"]} ({$match["category"]})\n";
}
echo "\n";
}
@@ -186,13 +217,13 @@ echo "\n💡 Recommendations:\n";
echo "===========================================\n";
$bestMatch = $topMatches[0];
$matchPercent = round($bestMatch['similarity'] * 100, 1);
$matchPercent = round($bestMatch["similarity"] * 100, 1);
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";
} 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";
} else {
echo "🆕 CREATE NEW: No close matches found (best match: {$matchPercent}%)\n";
@@ -209,21 +240,21 @@ echo "\nYour choice (1-3): ";
$choice = trim(fgets(STDIN));
switch ($choice) {
case '1':
case "1":
echo "\n✅ Great! Use the size name from the list above in your image configuration.\n";
break;
case '2':
case "2":
echo "\n📝 To create a new size, add it to: sites/default/settings/image_folders.php\n";
echo " Use this template:\n\n";
echo " 'your_size_name' => [\n";
echo " 'path' => 'uploads/images/your_path',\n";
echo " 'width' => {$dimensions['width']},\n";
echo " 'height' => {$dimensions['height']},\n";
echo " 'width' => {$dimensions["width"]},\n";
echo " 'height' => {$dimensions["height"]},\n";
echo " 'forced' => true,\n";
echo " 'crop' => true,\n";
echo " ],\n\n";
break;
case '3':
case "3":
echo "\n👋 Goodbye!\n";
break;
default:
@@ -232,5 +263,3 @@ switch ($choice) {
echo "\n";