Created
June 14, 2016 12:32
-
-
Save tormjens/e1f4e154286ffcd9ebcec01c5f749e55 to your computer and use it in GitHub Desktop.
Force images to be square.
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 | |
/** | |
* Parameters | |
* image = URL to original image | |
* size = The size of the square (e.g. 300) | |
**/ | |
header("Content-Type: image/png"); | |
$image = isset($_GET['image']) ? $_GET['image'] : null; | |
$size = isset($_GET['size']) ? $_GET['size'] : 300; | |
if($image && $size) { | |
$name = basename($image); | |
// Create new img | |
$img = imagecreatetruecolor($size, $size); | |
imagesavealpha($img, true); | |
$color = imagecolorallocatealpha($img, 0, 0, 0, 127); | |
imagefill($img, 0, 0, $color); | |
// Get existing image | |
$imageString = file_get_contents($image, false, NULL); | |
$src = imagecreatefromstring($imageString); | |
$width = imagesx($src); | |
$height = imagesy($src); | |
$y = $height != 300 ? 300/2 - ($height/2) : 0; | |
$x = $width != 300 ? 300/2 - ($width/2) : 0; | |
imagecopy($img, $src, $x, $y, 0, 0, $width, $height); | |
imagepng($img); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment