Last active
December 27, 2015 13:49
-
-
Save nicklasos/7335688 to your computer and use it in GitHub Desktop.
Create Squared Image (with alpha channel)
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 | |
/** | |
* Create squared image | |
* | |
* @param $originalFile file path | |
* @param null $destinationFile if null - file will be displayed (output) | |
* @param int $squareSize px | |
* @return bool | |
*/ | |
private function createSquareImage($originalFile, $destinationFile = null, $squareSize = 100) | |
{ | |
// Get width and height of original image | |
$imageData = getimagesize($originalFile); | |
if (!$imageData) { | |
return false; | |
} | |
$originalWidth = $imageData[0]; | |
$originalHeight = $imageData[1]; | |
$imageType = ''; | |
if ($originalWidth > $originalHeight){ | |
$newHeight = $squareSize; | |
$newWidth = $newHeight * ($originalWidth / $originalHeight); | |
} else if ($originalHeight > $originalWidth) { | |
$newWidth = $squareSize; | |
$newHeight = $newWidth * ($originalHeight / $originalWidth); | |
} else { | |
$newWidth = $squareSize; | |
$newHeight = $squareSize; | |
} | |
$newWidth = round($newWidth); | |
$newHeight = round($newHeight); | |
// Load the image | |
if ($imageData['mime'] === 'image/png') { | |
$imageType = 'png'; | |
$originalImage = imagecreatefrompng($originalFile); | |
} | |
if ($imageData['mime'] === 'image/jpg' || $imageData['mime'] === 'image/jpeg') { | |
$imageType = 'jpg'; | |
$originalImage = imagecreatefromjpeg($originalFile); | |
} | |
if ($imageData['mime'] === 'image/gif') { | |
$imageType = 'gif'; | |
$originalImage = imagecreatefromgif($originalFile); | |
} | |
if (!isset($originalImage)) { | |
return false; | |
} | |
$smallerImage = imagecreatetruecolor($newWidth, $newHeight); | |
$squareImage = imagecreatetruecolor($squareSize, $squareSize); | |
// Save alpha! | |
imagealphablending($squareImage, false); | |
imagesavealpha($squareImage, true); | |
$transparent = imagecolorallocatealpha($squareImage, 255, 255, 255, 127); | |
imagefilledrectangle($squareImage, 0, 0, $newWidth, $newHeight, $transparent); | |
imagealphablending($smallerImage, false); | |
imagesavealpha($smallerImage, true); | |
$transparent = imagecolorallocatealpha($smallerImage, 255, 255, 255, 127); | |
imagefilledrectangle($smallerImage, 0, 0, $newWidth, $newHeight, $transparent); | |
imagecopyresampled($smallerImage, $originalImage, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); | |
if ($newWidth > $newHeight) { | |
$difference = $newWidth - $newHeight; | |
$halfDifference = round($difference / 2); | |
imagecopyresampled($squareImage, $smallerImage, 0 - $halfDifference + 1, 0, 0, 0, $squareSize + $difference, $squareSize, $newWidth, $newHeight); | |
} | |
if ($newHeight > $newWidth) { | |
$difference = $newHeight - $newWidth; | |
$halfDifference = round($difference / 2); | |
imagecopyresampled($squareImage, $smallerImage, 0, 0 - $halfDifference + 1, 0, 0, $squareSize, $squareSize + $difference, $newWidth, $newHeight); | |
} | |
if ($newHeight == $newWidth) { | |
imagecopyresampled($squareImage, $smallerImage, 0, 0, 0, 0, $squareSize, $squareSize, $newWidth, $newHeight); | |
} | |
// If no destination file was given then display a png | |
if (!$destinationFile) { | |
imagepng($squareImage, NULL, 9); | |
} | |
// Save the smaller image FILE if destination file given | |
if($imageType === "jpg") { | |
imagejpeg($squareImage, $destinationFile, 100); | |
} | |
if($imageType === 'gif') { | |
imagegif($squareImage, $destinationFile); | |
} | |
if($imageType === 'png') { | |
imagepng($squareImage, $destinationFile, 9); | |
} | |
imagedestroy($originalImage); | |
imagedestroy($smallerImage); | |
imagedestroy($squareImage); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment