nicklasos
11/6/2013 - 12:58 PM

Create Squared Image (with alpha channel)

Create Squared Image (with alpha channel)

<?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;
}