Skip to content

Instantly share code, notes, and snippets.

@binary-data
Created January 14, 2016 08:47
Show Gist options
  • Save binary-data/a9e3e6487197bb4e99f1 to your computer and use it in GitHub Desktop.
Save binary-data/a9e3e6487197bb4e99f1 to your computer and use it in GitHub Desktop.
PHP resize image by width or heigth or both
<?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