Creating an image with PHP code.

Hello,

I have a website called http://flappycreator.com/ where users can upload multiple images to create their own version of Flappy Bird. As a result of this I created a PHP page that takes a GET param to create a preview image of their game to display in various locations on the website.

Here is an example:
http://flappycreator.com/preview.php?id=5305a9ad9dc30


My issue is, I have given my users the ability to upload PNGs or JPGs and trying to figure out reliable code to display the preview has become a huge hassle. In the example above the Pac-Man character shows a white background but when you play the game, it is actually transparent. In other situations I am getting extremely bad color distortion. The code I have provided is my final attempt after a ton of trial-and-error and the PNG imagealphablending() and imagesavealpha() commands have shown me the best results, however I don’t know what they do.

Please help! Thanks.

[php]<?php

$bird_picture = “default/bird.png”;
$tube1_picture = “default/tube1.png”;
$tube2_picture = “default/tube2.png”;
$ground_picture = “default/ground.png”;
$bg_picture = “default/bg.png”;

// If an id is set, then reset values
if (isset ( $_GET [‘id’] )) {

require ('XXX.php');
$db = new DBAccess ();

$query = "SELECT * FROM XXX WHERE XXX = '{$_GET['id']}'";
$result = $db->query ( $query );
$num_results = $result->num_rows;
$row = mysqli_fetch_array ( $result );

if ($num_results != 0) {
	if ($row ['bird_picture'] != "") {
		$bird_picture = "instances/" . $row ['folder_dir'] . "/" . $row ['bird_picture'];
	}

	if ($row ['tube1_picture'] != "") {
		$tube1_picture = "instances/" .$row ['folder_dir'] . "/" . $row ['tube1_picture'];
	}

	if ($row ['tube2_picture'] != "") {
		$tube2_picture = "instances/" .$row ['folder_dir'] . "/" . $row ['tube2_picture'];
	}

	if ($row ['ground_picture'] != "") {
		$ground_picture = "instances/" . $row ['folder_dir'] . "/" . $row ['ground_picture'];
	}

	if ($row ['bg_picture'] != "") {
		$bg_picture = "instances/" . $row ['folder_dir'] . "/" . $row ['bg_picture'];
	}
}

}

$temp = explode(".", $bg_picture);
$extension = end($temp);
if ($extension == “jpeg” || $extension == “jpg”) {
$background = imagecreatefromjpeg($bg_picture);
} else {
$background = imagecreatefrompng($bg_picture);
// Turn off alpha blending and set alpha flag
imagealphablending($background, true);
imagesavealpha($background, true);
}

$temp = explode(".", $ground_picture);
$extension = end($temp);
if ($extension == “jpeg” || $extension == “jpg”) {
$ground = imagecreatefromjpeg($ground_picture);
} else {
$ground = imagecreatefrompng($ground_picture);
// Turn off alpha blending and set alpha flag
imagealphablending($ground, false);
imagesavealpha($ground, true);
}

$temp = explode(".", $bird_picture);
$extension = end($temp);
if ($extension == “jpeg” || $extension == “jpg”) {
$bird = imagecreatefromjpeg($bird_picture);
} else {
$bird = imagecreatefrompng($bird_picture);
// Turn off alpha blending and set alpha flag
imagealphablending($bird, false);
imagesavealpha($bird, true);
}

$temp = explode(".", $tube1_picture);
$extension = end($temp);
if ($extension == “jpeg” || $extension == “jpg”) {
$tube1 = imagecreatefromjpeg($tube1_picture);
} else {
$tube1 = imagecreatefrompng($tube1_picture);
// Turn off alpha blending and set alpha flag
imagealphablending($tube1, false);
imagesavealpha($tube1, true);

}

$temp = explode(".", $tube2_picture);
$extension = end($temp);
if ($extension == “jpeg” || $extension == “jpg”) {
$tube2 = imagecreatefromjpeg($tube2_picture);
} else {
$tube2 = imagecreatefrompng($tube2_picture);
// Turn off alpha blending and set alpha flag
imagealphablending($tube2, false);
imagesavealpha($tube2, true);
}

imagecopymerge($background, $tube1, 200, -200, 0, 0, 52, 320, 100);
imagecopymerge($background, $tube2, 200, 200, 0, 0, 52, 320, 100);
imagecopymerge($background, $ground, 0, 320, 0, 0, 336, 112, 100);
imagecopymerge($background, $bird, 70, 190, 0, 0, 36, 26, 100);

header(‘Content-Type: image/png’);
imagepng($background);

Destroy

imagedestroy($background);
imagedestroy($ground);
imagedestroy($bird);
imagedestroy($tube1);
imagedestroy($tube2);

?>[/php]

Perhaps a silly question. You know that regular jpg doesn’t have transparency, right?

PNG-24,PNG-8 and GIF support transparency.
JPG does not support transparency.

Sponsor our Newsletter | Privacy Policy | Terms of Service