2

Code below resizes the images but I have a new requirement to it.

Requirement: I want to add padding to short edge with any colour. So when it is scaled down to 100(w)X150(h), it should be saved as 150X150 afted being padded.

For the padding option I've seen these two posts but failed to implement. Please help me to modify my code.

Thanks in advance

enter image description here

$this->defaults['width'] = 100;
$this->defaults['height'] = 150;


private function createThumbnail($sourceImage, $targetImage)
{
    list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);

    switch ($sourceType)
    {
        case IMAGETYPE_GIF:
            $sourceGdImage = imagecreatefromgif($sourceImage);
            break;
        case IMAGETYPE_JPEG:
            $sourceGdImage = imagecreatefromjpeg($sourceImage);
            break;
        case IMAGETYPE_PNG:
            $sourceGdImage = imagecreatefrompng($sourceImage);
            break;
    }

    if ($sourceGdImage === false)
    {
        return false;
    }

    $sourceAspectRatio = ($sourceWidth / $sourceHeight);
    $thumbnailAspectRatio = ($this->defaults['width'] / $this->defaults['height']);

    if ($sourceWidth <= $this->defaults['width'] && $sourceHeight <= $this->defaults['height'])
    {
        $thumbnailWidth = $sourceWidth;
        $thumbnailHeight = $sourceHeight;
    }
    elseif ($thumbnailAspectRatio > $sourceAspectRatio)
    {
        $thumbnailWidth = (int) ($this->defaults['height'] * $sourceAspectRatio);
        $thumbnailHeight = $this->defaults['height'];
    }
    else
    {
        $thumbnailWidth = $this->defaults['width'];
        $thumbnailHeight = (int) ($this->defaults['width'] / $sourceAspectRatio);
    }

    $thumbnailGdImage = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
    imagecopyresampled($thumbnailGdImage, $sourceGdImage, 0, 0, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);
    switch ($sourceType)
    {
        case IMAGETYPE_GIF:
            imagegif($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_PNG:
            imagepng($thumbnailGdImage, $targetImage, 9);
            break;
    }
    imagedestroy($sourceGdImage);
    imagedestroy($thumbnailGdImage);

    return true;
}
Community
  • 1
  • 1
BentCoder
  • 12,257
  • 22
  • 93
  • 165

1 Answers1

0

SOLUTION:

thumbnailDefaults:
    height: 200
    width: 200
    red: 200
    green: 0
    blue: 0

private function createThumbnailWithPadding($sourceImage, $targetImage)
{
    list($sourceWidth, $sourceHeight, $sourceType) = getimagesize($sourceImage);

    $sourceGdImage = imagecreatefromstring(file_get_contents($sourceImage));

    //Determine scale based on the longest edge
    if ($sourceHeight > $sourceWidth)
    {
        $scale = ($this->thumbnailDefaults['height'] / $sourceHeight);
    }
    else
    {
        $scale = ($this->thumbnailDefaults['width'] / $sourceWidth);
    }

    //Calculate new image dimensions
    $thumbnailWidth =  ($sourceWidth * $scale);
    $thumbnailHeight =  ($sourceHeight * $scale);

    //Determine offset coordinates so that new image is centered
    $offsetX = (($this->thumbnailDefaults['width'] - $thumbnailWidth) / 2);
    $offsetY = (($this->thumbnailDefaults['height'] - $thumbnailHeight) / 2);

    //Create new image and fill with background colour
    $thumbnailGdImage = imagecreatetruecolor($this->thumbnailDefaults['width'], $this->thumbnailDefaults['height']);

    //Set background colour
    $bgColor = imagecolorallocate(
        $thumbnailGdImage,
        $this->thumbnailDefaults['red'],
        $this->thumbnailDefaults['green'],
        $this->thumbnailDefaults['blue']
    );

    //Fill background colour
    imagefill($thumbnailGdImage, 0, 0, $bgColor);
    //Copy and resize original image into center of new image
    imagecopyresampled($thumbnailGdImage, $sourceGdImage, $offsetX, $offsetY, 0, 0, $thumbnailWidth, $thumbnailHeight, $sourceWidth, $sourceHeight);

    //clearstatcache();

    switch ($sourceType)
    {
        case IMAGETYPE_GIF:
            imagegif($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_JPEG:
            imagejpeg($thumbnailGdImage, $targetImage, 90);
            break;
        case IMAGETYPE_PNG:
            imagepng($thumbnailGdImage, $targetImage, 9);
            break;
    }

    imagedestroy($sourceGdImage);
    imagedestroy($thumbnailGdImage);

    return true;
}
BentCoder
  • 12,257
  • 22
  • 93
  • 165