Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

php image collage- need background white instead of black WordPress

  • SOLVED

I use this code to make a php image collage of 5 images into one - I cant get the background to be white - It doesn't matter to me if the final image is png or jpeg I just need the canvas to be white - anyone know what I need to change in this code

Im pretty sure its these lines that need changing:


$white = imagecolorallocate($this->image, 255, 255, 255);

imagefill($this->image, 0, 0, $white);




class imageGrid
{

private $realWidth;
private $realHeight;
private $gridWidth;
private $gridHeight;
private $image;

public function __construct($realWidth, $realHeight, $gridWidth, $gridHeight)
{
$this->realWidth = $realWidth;
$this->realHeight = $realHeight;
$this->gridWidth = $gridWidth;
$this->gridHeight = $gridHeight;

// create destination image
$this->image = imagecreatetruecolor($realWidth, $realHeight);

$white = imagecolorallocate($this->image, 255, 255, 255);
imagefill($this->image, 0, 0, $white);

}

public function __destruct()
{
imagedestroy($this->image);
}

public function display()
{
header("Content-type: image/jpeg");
imagejpeg($this->image);
}

public function putImage($img, $sizeW, $sizeH, $posX, $posY)
{
// Cell width
$cellWidth = $this->realWidth / $this->gridWidth;
$cellHeight = $this->realHeight / $this->gridHeight;

// Conversion of our virtual sizes/positions to real ones
$realSizeW = ceil($cellWidth * $sizeW);
$realSizeH = ceil($cellHeight * $sizeH);
$realPosX = ($cellWidth * $posX);
$realPosY = ($cellHeight * $posY);

$img = $this->resizePreservingAspectRatio($img, $realSizeW, $realSizeH);

// Copying the image
imagecopyresampled($this->image, $img, $realPosX, $realPosY, 0, 0, $realSizeW, $realSizeH, imagesx($img), imagesy($img));
}

public function resizePreservingAspectRatio($img, $targetWidth, $targetHeight)
{
$srcWidth = imagesx($img);
$srcHeight = imagesy($img);

$srcRatio = $srcWidth / $srcHeight;
$targetRatio = $targetWidth / $targetHeight;
if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight))
{
$imgTargetWidth = $srcWidth;
$imgTargetHeight = $srcHeight;
}
else if ($targetRatio > $srcRatio)
{
$imgTargetWidth = (int) ($targetHeight * $srcRatio);
$imgTargetHeight = $targetHeight;
}
else
{
$imgTargetWidth = $targetWidth;
$imgTargetHeight = (int) ($targetWidth / $srcRatio);
}

$targetImg = imagecreatetruecolor($targetWidth, $targetHeight);

imagecopyresampled(
$targetImg,
$img,
($targetWidth - $imgTargetWidth) / 2, // centered
($targetHeight - $imgTargetHeight) / 2, // centered
0,
0,
$imgTargetWidth,
$imgTargetHeight,
$srcWidth,
$srcHeight
);

return $targetImg;
}

}

$imageGrid = new imageGrid(700, 400, 10, 2);
$celebrity = imagecreatefromjpeg($pic_fullpath);

$imageGrid->putImage($celebrity, 4, 2, 0, 0);
imagedestroy($celebrity);
if ($results[0]) {
$product = get_post_meta( $results[0], '_wp_attached_file', true );
$product = "wp-content/uploads/" . $product;
$product = imagecreatefromjpeg($product);
$imageGrid->putImage($product, 3, 1, 4, 0);
imagedestroy($product);

}
if ($results[1]) {
$product = get_post_meta( $results[1], '_wp_attached_file', true );
$product = "wp-content/uploads/" . $product;
$product = imagecreatefromjpeg($product);
$imageGrid->putImage($product, 3, 1, 4, 1);
imagedestroy($product);

}


if ($results[2]) {
$product = get_post_meta( $results[2], '_wp_attached_file', true );
$product = "wp-content/uploads/" . $product;
$product = imagecreatefromjpeg($product);
$imageGrid->putImage($product, 3, 1, 7, 0);
imagedestroy($product);

}
if ($results[3]) {
$product = get_post_meta( $results[3], '_wp_attached_file', true );
$product = "wp-content/uploads/" . $product;
$product = imagecreatefromjpeg($product);
$imageGrid->putImage($product, 3, 1, 7, 1);
imagedestroy($product);

}
$imageGrid->display();



Answers (4)

2014-10-14

Dbranes answers:

Replace:

$targetImg = imagecreatetruecolor($targetWidth, $targetHeight);


with for example:

$targetImg = imagecreatetruecolor($targetWidth, $targetHeight);
$white = imagecolorallocate($targetImg, 255, 255, 255);
imagefill( $targetImg, 0, 0, $white );



Katie comments:

Perfect! Its white!! :)

2014-10-14

zebra webdesigns answers:

Can you provide some more information, where you get this code and the page where you have utilised this.


Katie comments:

the last answer here:
http://stackoverflow.com/questions/12913943/image-collage-in-php/12979857#12979857

I a using it here:
http://www.starstyle.com/style.php?ID=176644

2014-10-14

Arnav Joy answers:

in this
http://www.starstyle.com/style.php?ID=176644
i can see the background is white , so what's the problem?


Katie comments:

see how the edges are black - that is the canvas - I need that part white

2014-10-14

Hariprasad Vijayan answers:

Hi,

Why don't you try <em>imagecolortransparent()</em> instead of <em>imagefill()</em>, then you can set white background for the container using CSS i think.

Thanks.


Katie comments:

thanks I think I tried that first actually but I need it so its white without css - ugh :(