Last active
August 29, 2015 14:06
-
-
Save steinbring/3bada7213a837feddec0 to your computer and use it in GitHub Desktop.
No matter what the original width and height is of the source image, this function will resize your source file to a particular width and height of your choosing. The function preserves the original aspect ratio and then just adds solid bars on the top or bottom, if needed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
Description: No matter what the original width and height is of the source image, this function will resize your source file to a particular width and height of your choosing. The function preserves the original aspect ratio and then just adds solid bars on the top or bottom, if needed. | |
Author: Joe Steinbring (http://steinbring.net) | |
Date: 09/12/2014 | |
--> | |
<?php | |
function imageResize($NewWidth, $NewHeight, $NewFilename, $OldFilename) { | |
// Get the details on the target $OldFilename | |
$ImageDetails = getimagesize($OldFilename); | |
// What's the mimetype? | |
$MimeType = $ImageDetails["mime"]; | |
// What are the current dimensions? | |
$CurrentWidth = $ImageDetails[0]; | |
$CurrentHeight = $ImageDetails[1]; | |
// Calculate the scale to fit the image inside of the target dimensions | |
$scale = min($NewWidth/$CurrentWidth, $NewHeight/$CurrentHeight); | |
// What should the actual new dimensions be, based upon the calculated scale? | |
$CalcNewWidth = ceil($scale*$CurrentWidth); | |
$CalcNewHeight = ceil($scale*$CurrentHeight); | |
// Create new empty, new image | |
$NewImage = imagecreatetruecolor($NewWidth, $NewHeight); | |
// START: Make the background transparent | |
imagesavealpha($NewImage, true); | |
imagealphablending($NewImage, false); | |
$white = imagecolorallocatealpha($NewImage, 255, 255, 255, 127); | |
imagefill($NewImage, 0, 0, $white); | |
// END: Make the background transparent | |
// Read the file behind $OldFilename into variable | |
if($MimeType == 'image/jpeg'){ | |
// It's a JPEG | |
$OldImage = imagecreatefromjpeg($OldFilename); | |
}else if($MimeType == 'image/gif'){ | |
// It's a GIF | |
$OldImage = imagecreatefromgif($OldFilename); | |
}else if($MimeType == 'image/png'){ | |
// It's a PNG | |
$OldImage = imagecreatefrompng($OldFilename); | |
} | |
// Resize old image into new | |
imagecopyresampled($NewImage, $OldImage, | |
($NewWidth-$CalcNewWidth)/2, ($NewHeight-$CalcNewHeight)/2, 0, 0, | |
$CalcNewWidth, $CalcNewHeight, $CurrentWidth, $CurrentHeight); | |
// Write the image to the file behind $NewFilename | |
if($MimeType == 'image/jpeg'){ | |
// It's a JPEG | |
imagejpeg($NewImage, $NewFilename); | |
}else if($MimeType == 'image/gif'){ | |
// It's a GIF | |
imagegif($NewImage, $NewFilename); | |
}else if($MimeType == 'image/png'){ | |
// It's a PNG | |
imagepng($NewImage, $NewFilename); | |
} | |
// Clean up your stuff | |
imagedestroy($NewImage); | |
imagedestroy($OldImage); | |
} | |
imageResize(400, 300, 'BackDoor_med.jpg', 'BackDoor.jpg'); | |
imageResize(150, 160, 'BackDoor_thmb.jpg', 'BackDoor.jpg'); | |
imageResize(400, 300, 'MyBad_med.gif', 'MyBad.gif'); | |
imageResize(150, 160, 'MyBad_thmb.gif', 'MyBad.gif'); | |
imageResize(400, 300, 'coffee_med.png', 'coffee.png'); | |
imageResize(150, 160, 'coffee_thmb.png', 'coffee.png'); | |
?> | |
<!-- | |
Medium Image: 400x300 | |
Thumbnail: 150x160 | |
--> | |
<img src="BackDoor.jpg"> | |
<img src="BackDoor_med.jpg"> | |
<img src="BackDoor_thmb.jpg"> | |
<hr> | |
<img src="MyBad.gif"> | |
<img src="MyBad_med.gif"> | |
<img src="MyBad_thmb.gif"> | |
<hr> | |
<img src="coffee.png"> | |
<img src="coffee_med.png"> | |
<img src="coffee_thmb.png"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment