HOME       >>       Programming

Dynamic Php Image And Better Php Code Question


Normano

Im working on a dynamic image, can i put 2 images in same dynamic image, and can i make this code shorter?

if ( $goal == 31 ) {$xp2 = ('14833');} elseif ( $goal == 32 ) {$xp2 = ('16456');} elseif ( $goal == 33 ) {$xp2 = ('18247');} elseif ( $goal == 34 ) {$xp2 = ('20224');} elseif ( $goal == 35 ) {$xp2 = ('22406');} elseif ( $goal == 36 ) {$xp2 = ('24815');} elseif ( $goal == 37 ) {$xp2 = ('27473');} elseif ( $goal == 38 ) {$xp2 = ('30408');} elseif ( $goal == 39 ) {$xp2 = ('33648');} elseif ( $goal == 40 ) {$xp2 = ('37224');} elseif ( $goal == 41 ) {$xp2 = ('41171');} elseif ( $goal == 42 ) {$xp2 = ('45529');} elseif ( $goal == 43 ) {$xp2 = ('50339');} elseif ( $goal == 44 ) {$xp2 = ('55649');} elseif ( $goal == 45 ) {$xp2 = ('61512');} elseif ( $goal == 46 ) {$xp2 = ('67983');} elseif ( $goal == 47 ) {$xp2 = ('75127');} elseif ( $goal == 48 ) {$xp2 = ('83014');} elseif ( $goal == 49 ) {$xp2 = ('91721');} elseif ( $goal == 50 ) {$xp2 = ('101333');} elseif ( $goal == 51 ) {$xp2 = ('111945');} elseif ( $goal == 52 ) {$xp2 = ('123660');} elseif ( $goal == 53 ) {$xp2 = ('136594');} elseif ( $goal == 54 ) {$xp2 = ('150872');} elseif ( $goal == 55 ) {$xp2 = ('166636');} elseif ( $goal == 56 ) {$xp2 = ('184040');} elseif ( $goal == 57 ) {$xp2 = ('203254');} elseif ( $goal == 58 ) {$xp2 = ('224466');} elseif ( $goal == 59 ) {$xp2 = ('247886');} elseif ( $goal == 60 ) {$xp2 = ('273742');} elseif ( $goal == 61 ) {$xp2 = ('302288');} elseif ( $goal == 62 ) {$xp2 = ('333804');} elseif ( $goal == 63 ) {$xp2 = ('368599');} elseif ( $goal == 64 ) {$xp2 = ('407015');} elseif ( $goal == 65 ) {$xp2 = ('449428');} elseif ( $goal == 66 ) {$xp2 = ('496254');} elseif ( $goal == 67 ) {$xp2 = ('547953');} elseif ( $goal == 68 ) {$xp2 = ('605032');} elseif ( $goal == 69 ) {$xp2 = ('668051');} elseif ( $goal == 70 ) {$xp2 = ('737627');} elseif ( $goal == 71 ) {$xp2 = ('814445');} elseif ( $goal == 72 ) {$xp2 = ('899257');} elseif ( $goal == 73 ) {$xp2 = ('992895');} elseif ( $goal == 74 ) {$xp2 = ('1096278');} elseif ( $goal == 75 ) {$xp2 = ('1210421');} elseif ( $goal == 76 ) {$xp2 = ('1336443');} elseif ( $goal == 77 ) {$xp2 = ('1475581');} elseif ( $goal == 78 ) {$xp2 = ('1629200');} elseif ( $goal == 79 ) {$xp2 = ('1798808');} elseif ( $goal == 80 ) {$xp2 = ('1986068');} elseif ( $goal == 81 ) {$xp2 = ('2192818');} elseif ( $goal == 82 ) {$xp2 = ('2421087');} elseif ( $goal == 83 ) {$xp2 = ('2673114');} elseif ( $goal == 84 ) {$xp2 = ('2951373');} elseif ( $goal == 85 ) {$xp2 = ('3258594');} elseif ( $goal == 86 ) {$xp2 = ('3597792');} elseif ( $goal == 87 ) {$xp2 = ('3972294');} elseif ( $goal == 88 ) {$xp2 = ('4385776');} elseif ( $goal == 89 ) {$xp2 = ('4842295');} elseif ( $goal == 90 ) {$xp2 = ('5346332');} elseif ( $goal == 91 ) {$xp2 = ('5902831');} elseif ( $goal == 92 ) {$xp2 = ('6517253');} elseif ( $goal == 93 ) {$xp2 = ('7195629');} elseif ( $goal == 94 ) {$xp2 = ('7944614');} elseif ( $goal == 95 ) {$xp2 = ('8771558');} elseif ( $goal == 96 ) {$xp2 = ('9684577');} elseif ( $goal == 97 ) {$xp2 = ('10692629');} elseif ( $goal == 98 ) {$xp2 = ('11805606');} elseif ( $goal == 99 ) {$xp2 = ('13034431');} else {$xp2 = ('0');}


vujsa

Yes, you can have multiple images in your dynamic image. It has been a while since I've done dynamic images so I won't try to code it for you today but I'll explain how it normally works.

 

You normally start your dynamic image script by loading a background image or setting a blank or solid colored image with imagecreate() or similar function.

You then add an image over the top of that image with imagecopy() or similar function.

You can then use image copy again and again and it will insert the part of the image to use into your new image where you specify.

Each new image inserted will be layered on top of what ever already exists in the dynamic image.

So you can imagecopy() a a button or some other image segment then do another imagecopy() to place a border around that etc...

 

Now, for your second question...

You could use a switch!

switch ($goal) {case 31: $xp2 = ('14833'); break;case 32: $xp2 = ('16456'); break;case 33: $xp2 = ('18247'); break;// etc...}
Not particularly better...

 

I would use an array.

$xp_array = array(14833, 16456, 18247, 20224, 22406); // etc...$xp2 = $xp_array[$goal - 31];

In this case, $xp_array[0] = 14833;

So to get to get the zero index, we have to subtract 31 from $goal right!

As a result, $xp_array[1] = 16456;

Because 32 - 31 = 1

 

I hope you understand how this works.

The array index starts at zero but your scale starts at 31. To get your scale to match the array index, you have to subtract 31. As a result, the following is true:

0 = 31 = 14833

1 = 32 = 16456

2 = 33 = 18247

3 = 34 = 20224

4 = 35 = 22406

 

You may then want to have a check to see if the value of $goal is below 31 or above 99 and act accordingly.

 

Good luck

vujsa


Normano

Thanks very much, its works good and it faster now

$xp_array = array(14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 899257, 814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558, 9684577, 10692629, 13034431);$xp2 = $xp_array[$goal - 31];
I gonna try imagecopy().

vujsa

Thanks very much, its works good and it faster now

$xp_array = array(14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 899257, 814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558, 9684577, 10692629, 13034431);$xp2 = $xp_array[$goal - 31];
I gonna try imagecopy().
I'm glad that I could help. Yeah, the script should run much faster now. It is amazing how much easier and quicker a script can run just by changing the code a little bit. Before, the server had to check 68 conditional statements (if, else if, else) but now it only looks for one specific item in an array.

If I had quickly found a pattern to the $xp2 values, it would be even quicker to do it all mathematically.
Something like this:
$xp2 = $goal - 31 + 14833 + (($goal - 31) * .01);
This of course isn't the correct formula but if you had a formula to use instead of an array, you would save even more lines of code and speed up the script.

Good luck,
vujsa

Normano

I got a small problem, when It load the background on the dynamic image, if the image file dont exist it say error in the image, do u remember or know how to fix it? I use

$im = imagecreatefrompng("$string.png");
edit: If a varible is more then 100, can i do so it change to 100?

vujsa

I got a small problem, when It load the background on the dynamic image, if the image file dont exist it say error in the image, do u remember or know how to fix it? I use

$im = imagecreatefrompng("$string.png");
edit: If a varible is more then 100, can i do so it change to 100?
No problem, just check for the image first...
if (file_exists($string.png)) { $im = imagecreatefrompng("$string.png");}
This way the image copy is only attempted if the image exists.

As for your 100 question, I assume you mean if $goal for some reason is larger than 99...
Let's say that a $goal of 99 is the maximum so if a number is larger than that, it should be reset to 99 and maybe 31 is the minimum so reset $goal below that to 31 like so:
if($goal > 99){ $goal = 99;}else if ($goal < 31){ $goal = 31;}$xp_array = array(14833, 16456, 18247, 20224, 22406); // etc...$xp2 = $xp_array[$goal - 31];

I hope this answers your questions.
vujsa

Normano

Thanks very much:) its work good:)


vujsa

Thanks very much:) its work good:)

Glad to hear that this resolved your issues. I look forward to your future PHP discussions.
vujsa

Normano

if($goal < $lvl){imagestring($im, 2, 150, 14, "Goal Reached, Congrats!", $black);imagestring($im, 2, 149, 13, "Goal Reached, Congrats!", $white);}else{imagestring($im, 2, 150, 14, "XP Left: $xp_left", $black);imagestring($im, 2, 149, 13, "XP Left: $xp_left", $white);}
this script do so if $lvl is bigger then $goal it say "Goal Reached, Congrats!" but if $goal and $lvl is same how do i code so it say "Goal Reached, Congrats!"? thanks for all help

vujsa

if($goal < $lvl){imagestring($im, 2, 150, 14, "Goal Reached, Congrats!", $black);imagestring($im, 2, 149, 13, "Goal Reached, Congrats!", $white);}else{imagestring($im, 2, 150, 14, "XP Left: $xp_left", $black);imagestring($im, 2, 149, 13, "XP Left: $xp_left", $white);}
this script do so if $lvl is bigger then $goal it say "Goal Reached, Congrats!" but if $goal and $lvl is same how do i code so it say "Goal Reached, Congrats!"? thanks for all help
Well, you would use "less than or equal to" instead of "less than".

Here is your modified code:
if($goal <= $lvl){imagestring($im, 2, 150, 14, "Goal Reached, Congrats!", $black);imagestring($im, 2, 149, 13, "Goal Reached, Congrats!", $white);}else{imagestring($im, 2, 150, 14, "XP Left: $xp_left", $black);imagestring($im, 2, 149, 13, "XP Left: $xp_left", $white);}

Here is some more information about comparison operators.
http://forums.xisto.com/no_longer_exists/

Hope this helps,
vujsa

Normano

thanks:), i didnt find that site when i searched:P good to know



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.