Dynamically Create Image then Tempoarily Store

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Dynamically Create Image then Tempoarily Store

Post 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:
Image
Last edited by Trenchant on Mon Nov 27, 2006 8:21 pm, edited 2 times in total.
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

image fixed.
Last edited by Trenchant on Mon Nov 27, 2006 8:11 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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).
(#10850)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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...
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post 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.
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post 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.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

Thanks for the information. I'll look that stuff up and post back if I have any problems.
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

Well I started working on this tonight and I hit my first snag.

The images don't have their normal transparent backgrounds.
Image

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 :lol:
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post by Trenchant »

Does anyone know what might be causing this deformation in the images?
User avatar
Mini
Forum Newbie
Posts: 23
Joined: Mon Dec 04, 2006 4:39 am
Location: Netherlands

Post 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 :).
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post 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%
Image
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post 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);
?>
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post 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.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post 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.
Post Reply