PHP के GD extension का उपयोग करके images को resize करना एक सामान्य कार्य है। यह फंक्शन aspect ratio बनाए रखते हुए, cropping विकल्प के साथ images को resize करता है।
पूर्ण Image Resize फंक्शन
function imageResize($filePath, $maxWidth, $maxHeight, $crop = false) {
// Image जानकारी प्राप्त करें
$imageInfo = getimagesize($filePath);
if (!$imageInfo) {
throw new Exception("Invalid image file: $filePath");
}
list($srcWidth, $srcHeight, $type) = $imageInfo;
// MIME type के अनुसार image resource बनाएं
switch ($type) {
case IMAGETYPE_JPEG:
$source = imagecreatefromjpeg($filePath);
break;
case IMAGETYPE_PNG:
$source = imagecreatefrompng($filePath);
break;
case IMAGETYPE_GIF:
$source = imagecreatefromgif($filePath);
break;
case IMAGETYPE_WEBP:
$source = imagecreatefromwebp($filePath);
break;
default:
throw new Exception("Unsupported image type");
}
// Aspect ratio गणना
$srcRatio = $srcWidth / $srcHeight;
$dstRatio = $maxWidth / $maxHeight;
if ($crop) {
// Crop mode: fill entire area, crop excess
if ($srcRatio > $dstRatio) {
// Source wider - crop sides
$srcNewWidth = $srcHeight * $dstRatio;
$srcX = ($srcWidth - $srcNewWidth) / 2;
$srcY = 0;
$srcNewHeight = $srcHeight;
} else {
// Source taller - crop top/bottom
$srcNewHeight = $srcWidth / $dstRatio;
$srcY = ($srcHeight - $srcNewHeight) / 2;
$srcX = 0;
$srcNewWidth = $srcWidth;
}
$dstWidth = $maxWidth;
$dstHeight = $maxHeight;
} else {
// Fit mode: maintain aspect ratio, fit inside box
if ($srcRatio > $dstRatio) {
$dstWidth = $maxWidth;
$dstHeight = $maxWidth / $srcRatio;
} else {
$dstHeight = $maxHeight;
$dstWidth = $maxHeight * $srcRatio;
}
$srcX = $srcY = 0;
$srcNewWidth = $srcWidth;
$srcNewHeight = $srcHeight;
}
// Destination image बनाएं (true color for quality)
$dst = imagecreatetruecolor($dstWidth, $dstHeight);
// Transparency के लिए (PNG, GIF, WEBP)
if ($type === IMAGETYPE_PNG || $type === IMAGETYPE_GIF || $type === IMAGETYPE_WEBP) {
imagealphablending($dst, false);
imagesavealpha($dst, true);
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagefilledrectangle($dst, 0, 0, $dstWidth, $dstHeight, $transparent);
}
// Resample (high quality)
imagecopyresampled(
$dst, $source,
0, 0, $srcX, $srcY,
$dstWidth, $dstHeight,
$srcNewWidth, $srcNewHeight
);
return $dst;
}
// उपयोग उदाहरण
$resized = imageResize('photo.jpg', 800, 600, false); // Fit mode
imagejpeg($resized, 'photo_resized.jpg', 90);
imagedestroy($resized);
उपयोग के उदाहरण
// 1. Thumbnail बनाएं (crop के साथ)
$thumb = imageResize('large_photo.jpg', 250, 250, true);
imagejpeg($thumb, 'thumb_250x250.jpg', 85);
// 2. Web के लिए optimize करें (max width 1200px)
$web = imageResize('raw_photo.jpg', 1200, 1200, false);
imagejpeg($web, 'web_ready.jpg', 80);
// 3. PNG transparency बनाए रखें
$png = imageResize('logo.png', 300, 200, false);
imagepng($png, 'logo_resized.png', 6); // Compression level 0-9
आवश्यक PHP Extensions
gd- मुख्य extension (आमतौर पर pre-installed)fileinfo- MIME type detection के लिए
सामान्य समस्याएं और समाधान
| समस्या | समाधान |
|---|---|
| Memory limit exceeded | ini_set('memory_limit', '256M') या chunked processing |
| Transparency lost (PNG) | imagesavealpha($dst, true) सेट करें |
| Quality खराब | imagecopyresampled उपयोग करें (नहीं imagecopyresized) |
| Animated GIF टूट जाता है | GD animated GIF support नहीं करता; ImageMagick उपयोग करें |