Created
January 14, 2016 08:47
-
-
Save binary-data/a9e3e6487197bb4e99f1 to your computer and use it in GitHub Desktop.
PHP resize image by width or heigth or both
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
<?php | |
/** | |
* Basic function for resize. It doesn't save image. To save use imagejpeg() | |
*/ | |
function resize($width = null, $height = null) | |
{ | |
if (!$width && !$height) { | |
return false; | |
} | |
$original = 'image.jpg'; | |
list($originalWidth, $originalHeight) = getimagesize($original); | |
if (!$height) { | |
$height = round((($originalHeight * $width) / $originalWidth)); | |
} elseif (!$width) { | |
$width = round((($originalWidth * $height) / $originalHeight)); | |
} | |
$destinationImage= imagecreatetruecolor($width, $height); | |
$sourceImage = imagecreatefromjpeg($original); | |
imagecopyresampled($destinationImage, $sourceImage, 0, 0, 0, 0, $width, $height, $originalWidth, $originalHeight)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment