Downsider Posted January 30, 2010 Share Posted January 30, 2010 Hey. I've been programming in C# for a few while now, and I'm currently making a 2D game. In this game, I've got an editor for tile-based levels. So far, I've been using this editor to create the tiles and the lightmap by placing several lights on the stage and rendering them to a PNG file. It takes a bit long for large maps, and I was wondering if there was any way I could offload this process onto a PHP server and generate a PNG image from it! However, I've got minimal experience in PHP. I've done some simple stuff, but nothing too intense.. What I really need to know how to do is to take several two-dimensional arrays with R G B and A values and turn them into a PNG image. I'm sure there's a library for this, no? I've seen things like this done before. If anybody can help me out, I'd love to see you post below! Thanks! Link to comment https://forums.phpfreaks.com/topic/190320-about-images/ Share on other sites More sharing options...
jskywalker Posted January 30, 2010 Share Posted January 30, 2010 all info about images and PHP is on http://www.php.net/manual/en/refs.utilspec.image.php But, why do you think that PHP can do this faster than your current solution? It might only work faster if this PHP-server has more (or faster) CPU's, and has nothing to do now, or it will slow down you PHP-server in what its doing now... Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004160 Share on other sites More sharing options...
Downsider Posted January 30, 2010 Author Share Posted January 30, 2010 all info about images and PHP is on http://www.php.net/manual/en/refs.utilspec.image.php But, why do you think that PHP can do this faster than your current solution? It might only work faster if this PHP-server has more (or faster) CPU's, and has nothing to do now, or it will slow down you PHP-server in what its doing now... I know. The platform that the application was written for is fairly weak, and it's running on an inefficient interpreted language as well. It's mostly an experiment, a sort of little hacking project. Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004223 Share on other sites More sharing options...
Downsider Posted January 30, 2010 Author Share Posted January 30, 2010 I decided upon GD library. It seems the most widely used and supported. <?php header('Content-type: image/png'); $lightmap = imagecreate(64, 64); $colors = array(1024); $count = 0; for ($dx=0;$dx<64;$dx++) { for ($dy=0;$dy<64;$dy++) { $colors[$count] = imagecolorallocate($lightmap, rand()%255, rand()%255, rand()%255); imagerectangle($lightmap, $dx, $dy, 1, 1, $colors[$count]); $count++; } } imagepng($lightmap); ?> I have that. I would assume that it would generate a 64x64 image with a random colorr for each pixel. However, it doesn't work. Can anybody Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004307 Share on other sites More sharing options...
jskywalker Posted January 30, 2010 Share Posted January 30, 2010 on http://nl.php.net/manual/en/function.imagecreate.php it says: We recommend the use of imagecreatetruecolor(). so change: imagecreate to: imagecreatetruecolor and change: imagerectangle($lightmap, $dx, $dy, 1, 1, $colors[$count]); to: imagerectangle($lightmap, $dx, $dy, $dx, $dy, $colors[$count]); Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004319 Share on other sites More sharing options...
Downsider Posted January 30, 2010 Author Share Posted January 30, 2010 Awesome! Worked perfectly! I'm assuming the truecolor function allows you to generate a 32-bit image? Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004321 Share on other sites More sharing options...
jskywalker Posted January 30, 2010 Share Posted January 30, 2010 no, it wont to a 32-bit image it does a "Create a new true color image" RTFM: http://nl.php.net/manual/en/function.imagecreatetruecolor.php Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004323 Share on other sites More sharing options...
Downsider Posted January 30, 2010 Author Share Posted January 30, 2010 no, it wont to a 32-bit image it does a "Create a new true color image" RTFM: http://nl.php.net/manual/en/function.imagecreatetruecolor.php I assume that's 32 or 24-bit. GIF doesn't support 24 or 32-bit and "true color" doesn't support GIF. Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004326 Share on other sites More sharing options...
Downsider Posted January 30, 2010 Author Share Posted January 30, 2010 So far I've got this much: <?php header('Content-type: image/png'); $image_scale = 512; $lightmap = imagecreatetruecolor($image_scale, $image_scale); $count = 0; for ($dx=0;$dx<$image_scale;$dx++) { for ($dy=0;$dy<$image_scale;$dy++) { $shadowed = checkLine(0, 0, $dx, $dy); if ($shadowed) { $color = imagecolorallocate($lightmap, 1, 1, 1); imagerectangle($lightmap, $dx, $dy, $dx, $dy, $color); } else { $color = imagecolorallocate($lightmap, 255, 255, 255); imagerectangle($lightmap, $dx, $dy, $dx, $dy, $color); } } } function onwall($wx, $wy) { if ($wx > 32 && $wx < 43) { if ($wy > 32 && $wy < 43) { return true; } } return false; } function checkLine($sx, $sy, $ex, $ey) { if ($sx == $ex && $sy == $ey) return false; $dx = $sx-$ex; $dy = $sy-$ey; $maxdist = sqrt($dx*$dx+$dy*$dy); $ang = atan2($ey-$sy,$ex-$sx); $mx = cos($ang); $my = sin($ang); for ($i=0;$i<$maxdist;$i++) { if (onwall($sx, $sy)) { return true; } $sx = $mx + $sx; $sy = $my + $sy; } return false; } imagepng($lightmap); ?> Works fine with a 512 scale, albeit a bit slow. I'd like to increase the speed, and see several ways to do it considering my tiles are 16x16. However, when I set the scale to 1024, it simply doesn't work. It says the image has errors and cannot be displayed. Any ideas? Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004386 Share on other sites More sharing options...
jskywalker Posted January 31, 2010 Share Posted January 31, 2010 yep, its slow... and i get a 'simple' black&white image from that.... there should be a simpler way to generate the exact same image... but i'm not in the mood to try and figure out what you are trying to do with the script ..... With $image_scale=1024, it took my CPU 9mins and 21secs to create a larger PNG without errors.. (I could not do this from a browser because of a Maximum execution time) Link to comment https://forums.phpfreaks.com/topic/190320-about-images/#findComment-1004711 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.