Jump to content

About images!


Downsider

Recommended Posts

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.