Page 1 of 2
Dynamically Create Image then Tempoarily Store
Posted: Sun Nov 26, 2006 8:45 pm
by Trenchant
Well I remember posting about this before but I'm still baffled.
Here's the situation... I'm creating an online car racing game. There are pictures of users cars and parts in a database. The location of which are all stored in a database. All the car parts have their own row in a table of car parts. Along with that is their compatability to other parts and vehicles aswell as the X Y positions of where they must be assembled.
Here's the problem... Every time a user logs in I want to create the users image and then store it in some temporary form. I'm thinking storing the images in the users session as that is what was suggested before. The site is using session authentication.
Anyone know how about I would do this? The part that has me stuck is building multiple images into one using XY corrordinates with a car body as the canvas.
All car bodies follow this same style and size:

Posted: Mon Nov 27, 2006 4:23 pm
by Trenchant
image fixed.
Posted: Mon Nov 27, 2006 4:59 pm
by Christopher
I think you rather than temporary storage, you might want to think about creating a directory or database records identified by the username and store the generated images there. Every username is unique so the images can be created when they are building their car and then fetched based on the username when needed. Each user would have an identical structure (whether database records or directory/files).
Posted: Mon Nov 27, 2006 6:42 pm
by nickvd
Another solution would be to store the "Instructions" for building the car (store parts id's into the users database/table) that way you can re-create the car whenever you need to, it also will allow you to easily re-build/change parts without needing to re-create the image on disk.
However, storing the images on disk will be less intensive for the server as it doesn't have to re-create the image every time it needs to be displayed...
Posted: Mon Nov 27, 2006 8:00 pm
by Trenchant
This will be a high load website with a projected user load of 700 active users minimum and a max of about 1500. That is the goal for the initial startup of the website when we fire up our advertising program.
With that many users online at once with that load spread over 1-2 high end servers what would be best? I don't have much experience with dynamic images and I need to figure out what would be the most resourceful. Most online game do not take up a lot of HD space they normally use a lot more database connections. So knowing that would it be wise to simply save the users car on the server HD perminately when they register and update it as they add parts?
My second question. How would I go about creating the car with all the dynamic parts? Whats the most effective function / design to go after? If someone can throw a function name out their I'll go figure it out.
Posted: Fri Dec 01, 2006 7:41 am
by Trenchant
Does anyone know a function I could start searching up? I can't seem to find one for combining multiple layers to make one image.
Posted: Fri Dec 01, 2006 7:58 am
by kaszu
Posted: Fri Dec 01, 2006 10:42 pm
by Trenchant
Thanks for the information. I'll look that stuff up and post back if I have any problems.
Posted: Fri Dec 01, 2006 11:42 pm
by Trenchant
Well I started working on this tonight and I hit my first snag.
The images don't have their normal transparent backgrounds.
Code: Select all
<?php
header("Content-type: image/png");
// Create the base of the car(body/frame)
$base = @imagecreate(500, 175);
$background_color = imagecolorallocate($base, 255, 255, 255);
// Load the body.
$body = imagecreatefrompng('../images/cars/dodge/challenger/70challenger.png');
// Load the tire.
$part = imagecreatefrompng('../images/cars/dodge/challenger/tire.png');
// Add the tires onto the base of the car.
imagecopymerge ($base, $part, '372', '110', '0', '0', '68', '68', '100');
// Add the body onto the base.
imagecopymerge ($base, $body, '0', '0', '0', '0', '500', '175', '100');
imagepng($base);
imagedestroy($body);
?>
Does anyone know what I'm doing wrong? I've only worked with PHP images maybe once

Posted: Sun Dec 03, 2006 4:58 pm
by Trenchant
Does anyone know what might be causing this deformation in the images?
Posted: Mon Dec 04, 2006 5:02 am
by Mini
You cant use png images with transparant parts in it as far is i know. You could make the transparant white and then make the white transparant with gd.
Code: Select all
$white = imagecolorclosest($pic, 255, 255, 255);
imagecolortransparent($pic, $white);
imagecopymerge($pic1, $pic, $posx, $posy, 0, 0, imagesx($pic), imagesy($pic), 100);
Or use gif and imagecreatefromGif(); and export it as png. That's what i did

.
Posted: Mon Dec 04, 2006 3:28 pm
by Trenchant
Well that solved one problem. I changed everything so that the system now uses GIF images the only problem is that I can't view multiple layers at once. The first layer copied is the tire but once I copy the car layer over that you can't see the tire whithin the wheel well. I have checked the alignment and it is correct.
Did I miss something about transparent images? The colour is now corrected but all it shows is the body and a white background(like it should except the tire doesn't show up at all)
Code: Select all
<?php
header("Content-type: image/gif");
// Create the base of the car(body/frame)
$base = @imagecreate(500, 175);
$background_color = imagecolorallocate($base, 255, 255, 255);
// Load the body.
$body = imagecreatefromgif('../images/cars/dodge/challenger/70challenger.gif');
// Load the tire.
$part = imagecreatefromgif('../images/cars/rims/5_spoke_silver.gif');
// Add the tires onto the base of the car.
imagecopymerge ($base, $part, '372', '110', '0', '0', '68', '68', '100');
// Add the body onto the base.
imagecopymerge ($base, $body, '0', '0', '0', '0', '500', '175', '100');
imagegif($base);
imagedestroy($body);
?>
I experimented with the opacity of the car body and this is what showed up when I set the car bodies opacity to 50%

Posted: Tue Dec 05, 2006 5:25 am
by Flamie
youre not applying transparency here
And dont forget to destroy the images after you used them
Code: Select all
<?php
header("Content-type: image/gif");
// Create the base of the car(body/frame)
$base = @imagecreate(500, 175);
$background_color = imagecolorallocate($base, 255, 255, 255);
// Load the body.
$body = imagecreatefromgif('../images/cars/dodge/challenger/70challenger.gif');
// Load the tire.
$part = imagecreatefromgif('../images/cars/rims/5_spoke_silver.gif');
//apply transparency
imagecolortransparent($body, $background_color);
imagecolortransparent($part, $background_color);
// Add the tires onto the base of the car.
imagecopymerge ($base, $part, '372', '110', '0', '0', '68', '68', '100');
imagedestroy($part);
// Add the body onto the base.
imagecopymerge ($base, $body, '0', '0', '0', '0', '500', '175', '100');
imagedestroy($body);
imagegif($base);
imagedestroy($base);
?>
Posted: Tue Dec 05, 2006 4:37 pm
by Trenchant
The only problem is that that applies transparancy over the entire image for all the area's which are white. The end result is a faded image with many details missing.
Is there any way for GD to simply recognize what is transparent? I figured GD would automatically do that.
Posted: Tue Dec 05, 2006 4:46 pm
by Flamie
... just make the part you want transparent a unique color that you will not use anywhere else.
could be anything really 255,255,254 (almost white) or any other color.