Skip to content

Instantly share code, notes, and snippets.

@janzikan
Last active January 23, 2025 18:42
Show Gist options
  • Select an option

  • Save janzikan/2994977 to your computer and use it in GitHub Desktop.

Select an option

Save janzikan/2994977 to your computer and use it in GitHub Desktop.
PHP: Resize image - preserve ratio of width and height
/**
* Resize image - preserve ratio of width and height.
* @param string $sourceImage path to source JPEG image
* @param string $targetImage path to final JPEG image file
* @param int $maxWidth maximum width of final image (value 0 - width is optional)
* @param int $maxHeight maximum height of final image (value 0 - height is optional)
* @param int $quality quality of final image (0-100)
* @return bool
*/
function resizeImage($sourceImage, $targetImage, $maxWidth, $maxHeight, $quality = 80)
{
// Obtain image from given source file.
if (!$image = @imagecreatefromjpeg($sourceImage))
{
return false;
}
// Get dimensions of source image.
list($origWidth, $origHeight) = getimagesize($sourceImage);
if ($maxWidth == 0)
{
$maxWidth = $origWidth;
}
if ($maxHeight == 0)
{
$maxHeight = $origHeight;
}
// Calculate ratio of desired maximum sizes and original sizes.
$widthRatio = $maxWidth / $origWidth;
$heightRatio = $maxHeight / $origHeight;
// Ratio used for calculating new image dimensions.
$ratio = min($widthRatio, $heightRatio);
// Calculate new image dimensions.
$newWidth = (int)$origWidth * $ratio;
$newHeight = (int)$origHeight * $ratio;
// Create final image with new dimensions.
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
imagejpeg($newImage, $targetImage, $quality);
// Free up the memory.
imagedestroy($image);
imagedestroy($newImage);
return true;
}
/**
* Example
* resizeImage('image.jpg', 'resized.jpg', 200, 200);
*/
@kriit24

kriit24 commented Feb 7, 2017

Copy link
Copy Markdown

not working

if original is w: 2448, h: 3264
and desired is w: 1500, h: 500

@gnanet

gnanet commented Jul 7, 2018

Copy link
Copy Markdown

@kriit24

the code is clear, preserving the ratio is a primary goal, because it uses the lowest scaling ratio either width or height
$ratio = min($widthRatio, $heightRatio);

Also your example is about distortion, not preserved scale:
w: 2448, h: 3264 - portrait orientation , aspect ratio 3:4
w: 1500, h: 500 - landscape orientation, aspect ratio 3:1

@hudav

hudav commented Jun 7, 2019

Copy link
Copy Markdown

Elegant, easy, nice ! thanks

@narenthiran

Copy link
Copy Markdown

png is not working.

@janzikan

janzikan commented Jul 8, 2019

Copy link
Copy Markdown
Author

@narenthiran The resizeImage function only works with JPEG images.

@narenthiran

Copy link
Copy Markdown

Okay i found another solution. Thanks.

@kopitar

kopitar commented Jul 31, 2019

Copy link
Copy Markdown

Added PNG support in fork.

@Tub0

Tub0 commented May 19, 2020

Copy link
Copy Markdown

thanks

@nguyentam19008

nguyentam19008 commented Jun 5, 2020

Copy link
Copy Markdown

Giải quyết tất cả vấn đề với mọi loại hình ảnh (Solve all problems with all types of images)

function resizeImage($sourceImage, $targetImage, $maxWidth, $maxHeight, $quality = 80)
{
list($origWidth, $origHeight, $type) = getimagesize($sourceImage);

if ($type == 1)
{
    header ('Content-Type: image/gif');
    $image = imagecreatefromgif($sourceImage);
}
elseif ($type == 2)
{
    header ('Content-Type: image/jpeg');
    $image = imagecreatefromjpeg($sourceImage);
}
elseif ($type == 3)
{
    header ('Content-Type: image/png');
    $image = imagecreatefrompng($sourceImage);
}
else
{
    header ('Content-Type: image/x-ms-bmp');
    $image = imagecreatefromwbmp($sourceImage);
}

if ($maxWidth == 0)
{
    $maxWidth  = $origWidth;
}

if ($maxHeight == 0)
{
    $maxHeight = $origHeight;
}

// Calculate ratio of desired maximum sizes and original sizes.
$widthRatio = $maxWidth / $origWidth;
$heightRatio = $maxHeight / $origHeight;

// Ratio used for calculating new image dimensions.
$ratio = min($widthRatio, $heightRatio);

// Calculate new image dimensions.
$newWidth  = (int)$origWidth  * $ratio;
$newHeight = (int)$origHeight * $ratio;

// Create final image with new dimensions.

// if($type==1 or $type==3)
// {
//    $newImage = imagefill($newImage,0,0,0x7fff0000);
// }

$newImage = imagecreatetruecolor($newWidth, $newHeight);

$transparent = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
imagefill($newImage, 0, 0, $transparent);
imagesavealpha($newImage, true);


imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $origWidth, $origHeight);
imagepng($newImage, $targetImage);

// Free up the memory.
imagedestroy($image);
imagedestroy($newImage);

return true;

}

@sarry97

sarry97 commented Dec 13, 2020

Copy link
Copy Markdown

Everything works fine except PNG transparency. I am getting a black background on all PNGs.

@breneo1

breneo1 commented Dec 16, 2020

Copy link
Copy Markdown

work awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment