Re: Writing PNG Images to File from Command Line

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> Because of some latency issues in the display of dynamically created PNG
> images, I've been trying to separate the image-creation process from the
> image-display process. Essentially, I'm trying to run a cron job that
> writes a PNG image to file every 5 seconds. The php file pulls data from a
> MySQL db and creates a color-coded map of a cancer treatment building. The
> presence of normal levels of Neutrons would lead to the creation of a
> green flag, above-normal levels yields a yellow flag, and high levels
> yields a red flag. Another page will simply use a SRC tag to display the
> image, and will refresh every 5-6 seconds to get the latest
> readings/image.

The potential problem here is that your PHP script will frequently be
attempting to read the file at the same time that your cron job has the
file open for writing -- and presumably "locked" at that point.

I don't know enough about FileSystem internals to be certain, but it
sounds like a recipe for trouble.

> The concept worked extremely well when the image was output to the
> browser, except for the fact that a latency caused the image in the
> browser to flicker when it refreshed.

I don't think a latency in creating the image is at fault here...

The browser is gonna get the HTML first, find the IMG tag, and download
the image, and depending on how large the image is, and on your network,
there will always be some flicker there.

I suspect that GD spewing out an image is probably not significantly
slower than reading the image from the hard drive.

You can minimize it by:
  Making the image smaller (fewer colors, lower resolution)
  Using WIDTH and HEIGHT attributes on your IMG tag
  Buying more bandwidth or decreasing network hops (see traceroute)

> So I've attempted the above, but without success, in that I can't get the
> cron job to actually make the image file. I know that the file is being
> processed because I can echo out a few words to the screen. And I know
> that the php file can create an image file because it does so when I add
> the file_name attribute to the imagecreate() function.

Which user is running the cron job?  It's probably NOT the same user as
what runs PHP, so your permissions tests may not be valid.

Are you providing a FULL PATH to the php binary, the script, the image
file and all your includes?  You should assume NOTHING about the current
working directory when you run a cron job.

> I've checked permissions on folders, etc, and have tried both the CLI and
> SAPI versions of php, with and without available command line flags, but
> no luck. And it's not the db query portion of the process because I've
> commented out that portion of the code.

Add some error-checking to your fopen(..., 'w') to log any error and the
result of the fopen.
http://php.net/error_log

You may be missing out on some crucial info the computer is trying to give
you, simply by ignoring its error messages.  Kind of like a girl, only
much less complicated :-)

All that said:
If you only have a handful of color coded flags, and you are using GD to
re-draw an image every time, that's kinda silly...

Perhaps I'm missing something here, but:

Just use your query to decide which flag to *USE* instead of drawing a new
one every time.

<html>
  <body>
    <?php
      /* db query to determine flag color here */
      echo "<img src=\"$flag_color.png\">";
    ?>
  </body>
</html>

No need to fire up GD on every image if you only have a handful of
possible outputs.

Or does a single image consist of many many many flags?  Oh, okay.  GD is
the answer then.  Probably.  An HTML table of a bunch of tiny images
*might* be faster/smoother.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux