Question

How can I add a background color to thumbnail or change it's color in php trying to add background to for thumbnail?

private function _generateThumb() {
switch($this->imageType) {
        case 'jpg':
           $image = @imagecreatefromjpeg($this->pathToImage);
       break;
       case 'gif':
       $image = @imagecreatefromgif($this->pathToImage);
       break;
       case 'png':
          $image = @imagecreatefrompng($this->pathToImage);
       break;
}

if ($image === false) {
    trigger_error('image could not be created', 1024);
     print 'nöööööö'; exit;
}
$thumbImage = @ImageCreateTrueColor(933, $this->thumbHeight);

//select background color using number between 0 to 255
$color = imagecolorallocate($thumbImage,0,0,0);//black color
imagefill($thumbImage, 0, 0, $color);

if ($this->imageType == 'png' || $this->imageType == 'gif')
{
 imagealphablending($thumbImage, false);

$s=933-$this->thumbWidth;
$s=$s/2;//to keep image in center i.e distance from x-axis
ImageCopyResampled($thumbImage, $image,$s,0,0,0, $this->thumbWidth,          
$this->thumbHeight,$this->imageWidth, $this->imageHeight);
Was it helpful?

Solution

//select background color using number between 0 to 255
$color = imagecolorallocate($thumbImage,0,0,0);//black color
//$color=imagecolorallocate($thumbImage,255,255,255) white color
imagefill($thumbImage, 0, 0, $color);

//use range 0 to 255 to add colors to thumbnail background
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top