ProductReduceImageSizeFix Documentation
Product Reduce Image Size Fix Controller Documentation
File: /controllers/productReduceImageSizeFix.php
Purpose: Utility script to batch resize product images while preserving aspect ratio
Last Updated: December 20, 2024
Total Functions: 3+
Lines of Code: ~149
---
๐ Overview
This utility script batch processes product images to reduce file sizes while maintaining aspect ratios. Used for:
- โข Optimizing product images for web display
- โข Reducing storage space and bandwidth
- โข Standardizing image dimensions
- โข Improving website performance
Primary Functions
- โ Batch image resizing
- โ Aspect ratio preservation
- โ Format conversion support
- โ Performance optimization
---
๐ Key Functions
1. reduceImageSize() - Core Resizing Function
Location: Line 76
Purpose: Resize images while preserving aspect ratio
function reduceImageSize($imageName, $targetDir, $targetDirDestination) {
// 1. Get original dimensions
list($width, $height, $type, $attr) = getimagesize($targetDir . $imageName_basename);
// 2. Calculate new dimensions
$newDim = getNewWidthHeightAfterReduce($width, $height);
$width = $newDim[0];
$height = $newDim[1];
// 3. Load and resize
$theImageURL = "../views/default/images/product_image/$imageName";
$src = imagecreatefromstring(file_get_contents($theImageURL));
$dst = imagecreatetruecolor($width, $height);
// 4. Resample image
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight);
// 5. Save resized image
imagedestroy($src);
imagepng($dst, $targetDirDestination . $imageName);
imagedestroy($dst);
}
2. getNewWidthHeightAfterReduce() - Dimension Calculator
Location: Line 136
Purpose: Calculate new dimensions maintaining aspect ratio
function getNewWidthHeightAfterReduce($width, $height) {
$ratio = $width / $height; // Calculate aspect ratio
if ($ratio > 1) {
// Landscape orientation
$width = 500;
$height = 500 / $ratio;
} else {
// Portrait orientation
$width = 500 * $ratio;
$height = 500;
}
return array($width, $height);
}
3. Batch Processing Loop
Location: Lines 50-63
Purpose: Process all products in batches
$start = 0;
$limit = 50;
$all_products_count = $productExt->getProductsCount('', $joinConditionWithStore);
for ($i = $start; $i <= $all_products_count->productId; $i += $limit) {
$allproduct = $productExt->queryAllOrderedLimitedSimple($start, $limit);
foreach ($allproduct as $value) {
if (!empty($value->logo) && $value->logo != "." && $value->logo != "no image") {
reduceImageSize($value->logo,
"../views/default/images/product_image/",
"../views/default/images/product_image_resized/");
$count++;
}
}
$start += $limit;
}
---
๐ Workflows
Workflow: Batch Image Resizing
---
๐งฎ Calculation Methods
Aspect Ratio Preservation
$ratio = $width / $height;
// For landscape images (width > height)
if ($ratio > 1) {
$new_width = 500; // Fixed width
$new_height = 500 / $ratio; // Calculated height
}
// For portrait images (height > width)
else {
$new_width = 500 * $ratio; // Calculated width
$new_height = 500; // Fixed height
}
Size Calculation Example
// Original: 1000x600 (landscape)
$ratio = 1000 / 600 = 1.67
$new_width = 500
$new_height = 500 / 1.67 = 300
// Result: 500x300 (maintains ratio)
// Original: 600x1000 (portrait)
$ratio = 600 / 1000 = 0.6
$new_width = 500 * 0.6 = 300
$new_height = 500
// Result: 300x500 (maintains ratio)
---
๐ Security & Permissions
File System Access
// Check if output directory exists
if (!file_exists($targetDirDestination . "../")) {
mkdir($targetDirDestination . "../product_image_resized/", 0777, true);
}
Image Validation
- โข Uses
getimagesize()for validation - โข Filters out invalid image names
- โข Proper memory management with
imagedestroy()
---
โ ๏ธ Issues Identified
1. Variable Reference Error
Location: Line 49
Issue: Undefined variable $joinConditionWithStore
$all_products_count = $productExt->getProductsCount('', $joinConditionWithStore);
// $joinConditionWithStore is not defined in this script
2. Syntax Error
Location: Lines 131-133
Issue: Missing semicolon and orphaned semicolon
return $newParcode // Missing semicolon
; // Orphaned semicolon
3. Directory Path Issues
Location: Line 86-88
Issue: Complex directory path logic
if (!file_exists($targetDirDestination . "../")) {
mkdir($targetDirDestination . "../product_image_resized/", 0777, true);
}
// Unclear directory structure
---
๐ Performance Considerations
Memory Management
// Proper cleanup after each image
imagedestroy($src);
imagedestroy($dst);
Batch Processing
- โข 50 images per batch prevents memory exhaustion
- โข Progressive processing for large datasets
Output Format
- โข Always outputs as PNG for consistency
- โข May increase file size vs JPEG for some images
---
๐งช Testing Scenarios
Test Case 1: Various Aspect Ratios
1. Test with landscape images (16:9, 4:3)
2. Test with portrait images (3:4, 9:16)
3. Test with square images (1:1)
4. Verify aspect ratio preservation
Test Case 2: File Handling
1. Test with different input formats (jpg, png, gif)
2. Verify output consistency (all PNG)
3. Check directory creation
4. Validate file permissions
---
๐ Recommendations
Fix Variable Issue
// Define missing variable or remove dependency
$joinConditionWithStore = '';
// OR
$all_products_count = $productExt->getProductsCount();
Fix Syntax Error
// Remove orphaned code from barcode function
// This function doesn't belong in this script
Improve Error Handling
if (!$src) {
error_log("Failed to load image: $theImageURL");
return false;
}
Add Format Flexibility
// Determine output format based on input
$extension = pathinfo($imageName, PATHINFO_EXTENSION);
if ($extension === 'jpg' || $extension === 'jpeg') {
imagejpeg($dst, $targetDirDestination . $imageName, 85);
} else {
imagepng($dst, $targetDirDestination . $imageName);
}
---
Documented By: AI Assistant
Review Status: โ ๏ธ Needs Review - Contains undefined variables and syntax errors
Next Review: Fix variable references and syntax errors