PHP 5.1+ Date vs Strftime Timezone Annoyance

Posted by Chief on Feb 21, 2010 in Gotchas
No Comments

In the old PHP4 days, you could change the default timezone by: putenv("TZ=America/Los_Angeles");. In php5, you can still do this and it’ll work for strtotime, strftime, gmstftime (maybe more). It will not work for methods date and gmdate. For those, starting in version 5.1, you’ll need to call date_default_timezone_set("America/Los_Angeles");. This is just another one of those annoying things that make PHP a dying language.

Together, to be cross-version compliant:

putenv("TZ=America/Los_Angeles");
if( function_exists("date_default_timezone_set") ) { 
 date_default_timezone_set("America/Los_Angeles");
}

We need both to be reverse-compatible with PHP4.

Tags: , , , ,

The little known PHP htmlspecialchars

Posted by Chief on Feb 20, 2010 in Reference
No Comments

Want to replace only xml entities <, >, &? Don’t use htmlentites or str_replace; use htmlspecialchars.

The only named entities for XML are &amp;, &gt; and &lt;. For all others you need to use the Unicode character code (eg. &#160;). Webkit: Entity ‘nbsp’ not defined – Convert HTML entities to XML

Tags: , , , ,

PHP 5.3.1 Error Logging and Display of Said Errors

Posted by Chief on Feb 18, 2010 in Reference
No Comments

I just installed the PHP 5.3.1 Windows installer bundle [VC9 x86 Non Thread Safe (2009-Nov-19 09:53:39)] on my Windows 7 [Ultimate] machine. I want the PHP error output to get sent to stderr when run from the command line. I don’t want the errors logged to a file because I’m developing cli tools. Since we’re dealing with the cli, I also don’t want html errors. Should be pretty easy… if you know how to edit the settings.

These worked for me:

error_reporting = E_ALL
display_errors = stderr
display_startup_errors = On
log_errors = Off
html_errors = Off
;error_log = php-errors.log

Ensure that error_log is not set, otherwise error will not be displayed. This problem may be related, but is obviously not, the date bug mentioned in the PHP bug reports. It may be fixed in the latest snapshot. As a note, the php.ini date.timezone setting is date.timezone = "Pacific/Honolulu", and I still had this problem.

Other things I noticed:
When error_log = syslog, you can find the PHP events in the windows Event Viewer -> Windows Logs -> Application. I mention it in case you want that type of thing… or think error that logging may not be working.

Tags: , , , , , ,

PHP/GD Bug: imageFilledEllipse Centered on Y=0

Posted by Chief on Nov 23, 2009 in Scripts, Tricks and Hacks
No Comments

I was drawing circles using PHP’s GD function imageFilledEllipse. I noticed that when I went to draw a circle having its center at $y=0,there would be a line missing at $y=1.

Here’s the work-around:

if( -1 <= $py && $py <= 1 ) {
  // Undocumented PHP bug when Y coordinate is zero.
  imageFilledEllipse($img,$px,$py,$X,$X,$color);
  imageFilledRectangle($img,$px-$halfX,$py-1,$px+$halfX,$py+1,$color);
} else {
  imageFilledEllipse($img,$px,$py,$X,$X,$color);
}

PHP Version:

PHP 4.3.9 (cgi) (built: Apr  1 2009 10:41:42)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

$ yum search php-gd

php-gd.i386                              4.3.9-3.26             installed
Matched from:
php-gd
The php-gd package contains a dynamic shared object that will add
support for using the gd graphics library to PHP.

Tags: , , , , ,

Using Log Math to Solve Iterative Problems

Posted by Chief on Nov 20, 2009 in Reference
No Comments

Premise: You have a map of infinite resolution, but finite viewing space. You have points on that map that have a fixed foot print.

Question: How much do you need to zoom before the points are clearly distinguishable?

Let us arbitrarily define the criteria for distinguishability as an icon of 25 pixels in size. This icon, it can be said, has a radius of 13 pixels. In reality, icon size is a function of your viewport’s size. I leave that up to you to calculate.

Let us define the scale of the map as the absolute width of the map (in map units) divided by the width of the view port. As an example, let’s use the Earth’s longitude range: -180 to +180 degrees, but in a global Mercator projection. The coordinate range of longitude for this map is [-20037508,+20037508) meters. Our viewport has a range of [0,255] pixels (viewport units). The scale of this map in this viewport is (2*20037508)/256. Be careful not to double-count +20037508, as -20037508 = +20037508 and would yield an improper scale. The viewport size is an integer and does not wrap; there are no fractions of a pixel in a viewport.

We shall now define a target scale. The target scale is the desired scale of the map that allows our icons to become viewable. Our example will use buildings having a foot print of 300 meters (map units) in width and depth. Our target scale, therefore, is 300/25 = 12 meters per pixel.

Question: How much do we need to zoom in before our buildings are distinguishable?

Implementation 1 (naive, iterative):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function zoomLevel( $maprange, $viewport, $distFactor=0.10, $zoomFactor=2 ) {
  // $viewport = 256;
  // $maprange = 2*M_PI*6378137;
  $scale = $maprange / $viewport;
 
  // 10% of viewport required to distinguish an icon.
  $target = $distFactor * $viewport;
 
  $zoom = 0;
  while ( $scale > $target ) {
    $zoom += 1;
    $scale /= $zoomFactor;
  }
  return $zoom;
}

Implementations 2, 3 and 4:

1
2
3
4
5
6
7
8
9
10
11
12
13
function zoomLevel( $maprange, $viewport, $distFactor=0.10, $zoomFactor=2 ) {
  $scale = $maprange / $viewport;
  $target = $distFactor * $viewport;
 
  // naive log
  return ceil( (log($scale)/log($zoomFactor) - log($target)/log($zoomFactor)) );
 
  // commutative log
  return ceil( (log($scale) - log($target)) / log($zoomFactor) );
 
  // subtraction property of logs (best implementation)
  return ceil( log($scale/$target)/log($zoomFactor) );
}

Tags: , , , , , , , , ,

Compiling Datascope.so for PHP

Posted by Chief on Oct 10, 2008 in System Administration
No Comments

Does the following sentence make sense?

In order to be able to simply dl(“Datascope.so”) in your PHP code, you first need to make sure Datascope.so is compiled and copied to the extension_dir, as specified in php.ini (ex: /usr/lib/php4).

Answer:
No: Stop right now. You’re not qualified to read this article.
Yes: Keep reading.

Notes

You’re not compiling the PHP source code for actual use, so please don’t install it. If you do, you run the risk of clobbering any pre-existing php installations, and overwriting previously compiled-in modules. The only reason why you ”make” PHP is so that the Zend libraries are properly initialized.

The PHP ”make” step may be avoidable, but given that the compile doesn’t affect system performance, I didn’t bother.

Paths should be changed to reflect your setup. If you don’t have the src/contrib/data/php/datascope code, you can get it from the contrib source at BRTT’s web site.

Contrib appears to be maintained by Indiana University.

Paths

  • Antelope Bash Script ”/opt/antelope/4.9/setup.sh”
  • Datascope Source ”/opt/antelope/4.9/src/contrib/data/php/datascope”
  • PHP Source Code URL ”http://downloads.php.net/ilia/php-5.1.4.tar.bz2”
  • PHP Modules Directory ”/usr/lib/php/modules/”

Compile and Install Steps

  1. wget <PHP Source Code>
  2. bunzip2 php-5.1.4.tar.bz2
  3. tar xf php-5.1.4.tar
  4. cd php-5.1.4
  5. PHP_INC=`pwd`
  6. export PHP_INC
  7. ./configure
  8. make
  9. cd <Datascope Source>
  10. source <Antelope Bash Script>
  11. sudo make install
  12. sudo cp Datascope.so <PHP Modules Directory>

PHP Script Example

Proper usage of the ”Datascope.so” module follows:

putenv( "PFPATH=/opt/antelope/4.9/data/pf" );
putenv( "ANTELOPE=/opt/antelope/4.9" );
 
set_include_path(
  get_include_path().PATH_SEPARATOR.getenv('ANTELOPE')."/data/php"
);
 
 
if( !extension_loaded( "Datascope.so" ) ) {
  dl( "Datascope.so") || exit( "Failed to load datascope.so" ) ;
}

Tags: , , , , ,

Copyright © 2010 cat brain.log | less All rights reserved.
Shades v1.2 theme from BuyNowShop.com.