October 30, 2008
PHP
No Comments
You probably have often searched for a value in an array using the in_array() function. Reading php|architect’s Guide to PHP Security by Ilia Alshanetsky, I’ve noticed that searching on values can be slower than searching on keys. I’ve wanted to compare performance between the three methods to check a value in a white list and it’s amazing how slower is in_array(), look at the result :
fga@brian:~/tmp$ php5 test_array.php
With in_array :
111
time : 0.349022865295 sec
With isset on keys :
111
time : 0.000214099884033 sec
With array_key_exists :
111
time : 0.00021505355835 sec
Searching on keys is 1000 to 10000 times faster regarding the bench on an array containing 700 000 elements.
Here is the code of the test :
<?php
// array to store white list as values
$t = array();
// array to store white list as keys
$u = array();
// elements count
$max = 700000;
$first = $last = $middle = '';
for($i=$max-1;$i>=0;$i--){
// values are random strings
$id = uniqid(rand());
$t[] = $id;
$u[$id] = '';
// we store first, last and middle values to
// to search for them
switch($i) {
case $max-1:
$first = $id;
break;
case 0:
$last = $id;
break;
case $max/2:
$middle = $id;
break;
}
}
echo "With in_array :\n";
$start_time = microtime(true);
echo in_array($first,$t);
echo in_array($last,$t);
echo in_array($middle,$t);
echo "\n";
echo "time : " . (microtime(true)-$start_time) . " sec";
echo "\n";
echo "With isset on keys :\n";
$start_time = microtime(true);
echo isset($u[$first]);
echo isset($u[$last]);
echo isset($u[$middle]);
echo "\n";
echo "time : " . (microtime(true)-$start_time) . " sec";
echo "\n";
echo "With array_key_exists :\n";
$start_time = microtime(true);
echo array_key_exists($first,$u);
echo array_key_exists($last,$u);
echo array_key_exists($middle,$u);
echo "\n";
echo "time : " . (microtime(true)-$start_time) . " sec";
echo "\n";
June 5, 2008
Zend Framework
4 Comments
Currently working on PDF generation, I’ve noticed that drawing a big PNG image with transparency (about 600×800, 150kB) brings the script computation up to 20 seconds. I’ve raised the bug in the Zend Framework issue tracker and for now, don’t forget to remove transparency in your background image if you can.
See the bug at : http://framework.zend.com/issues/browse/ZF-3392
June 4, 2008
PHP, Zend Framework
No Comments
A common issue in Zend_Pdf is to wrap text in a box. I’ve found partial solutions such as wrapping text each 80 characters for instance but the line width can vary regarding the font and the character width. Since we can’t rely on the character count unless using a monospaced font, we have to wrap text on the real box width.
In partial solutions, I’ve found a function which computes the real width of a string according to the font and the font size. By aggregating every chunks, I’ve made my getWrappedText() method which returns a string with the correct \n :
protected function getWrappedText($string, Zend_Pdf_Style $style,$max_width)
{
$wrappedText = '' ;
$lines = explode("\n",$string) ;
foreach($lines as $line) {
$words = explode(' ',$line) ;
$word_count = count($words) ;
$i = 0 ;
$wrappedLine = '' ;
while($i < $word_count)
{
/* if adding a new word isn't wider than $max_width,
we add the word */
if($this->widthForStringUsingFontSize($wrappedLine.' '.$words[$i]
,$style->getFont()
, $style->getFontSize()) < $max_width) {
if(!empty($wrappedLine)) {
$wrappedLine .= ' ' ;
}
$wrappedLine .= $words[$i] ;
} else {
$wrappedText .= $wrappedLine."\n" ;
$wrappedLine = $words[$i] ;
}
$i++ ;
}
$wrappedText .= $wrappedLine."\n" ;
}
return $wrappedText ;
}
/**
* found here, not sure of the author :
* http://devzone.zend.com/article/2525-Zend_Pdf-tutorial#comments-2535
*/
protected function widthForStringUsingFontSize($string, $font, $fontSize)
{
$drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $string);
$characters = array();
for ($i = 0; $i < strlen($drawingString); $i++) {
$characters[] = (ord($drawingString[$i++]) << 8 ) | ord($drawingString[$i]);
}
$glyphs = $font->glyphNumbersForCharacters($characters);
$widths = $font->widthsForGlyphs($glyphs);
$stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
return $stringWidth;
}
then you can draw the text easily :
$y = 700;
$lines = explode("\n",$this->getWrappedText($text,$style_text,400)) ;
foreach($lines as $line)
{
$page2->drawText($line, 140, $y);
$y-=15;
}
May 29, 2008
Javascript, Zend Framework
No Comments
For a customer need, I’ve integrated the scriptaculous slider as shown here in Zend framework.
To achieve this, I’ve created a formSlider view helper which is then used by a slider form element.
The view helper and form element code is here. To allow ZF to find the helper, don’t forget to add path to helper in your bootstrap :
$layout->getView()->addHelperPath('Mc/View/Helper','Mc_View_Helper') ;
You need of course to include scriptaculous and prototype to make it work (available here and here) :
<script src="/prototype-1.6.0.2.js" type="text/javascript"></script>
<script src="/scriptaculous.js" type="text/javascript"></script>
Then, you can create a slider like this :
require_once('Mc/Form/Element/Slider.php') ;
$my_form->addElement(new Mc_Form_Element_Slider('my_element')
,'my_element') ;
$my_form->my_element->setLabel('My element : ')
->setMin(10)
->setMax(30)
->setStep(2);
and retrieve the value as usual :
$my_form->my_element->getValue() ;
And if you want to override the default style in your css, don’t forget the !important argument :
div.slider {
width:256px !important;
margin:10px 0 !important;
background-color:lightgray !important;
height:15px !important;
position: relative !important;
}
div.slider div.handle {
width:10px !important;
height:15px !important;
background-color:darkgray !important;
cursor:move !important;
position: absolute !important;
}
May 28, 2008
Javascript
No Comments
I was looking for a javascript which could automatically resize a textarea but the ones I’ve found were a bit buggy or ugly, so I’ve written mine. There’s just a tiny bug with copy/paste with mouse.
function autoresize(txtbox)
{
var cols = txtbox.cols ;
var content = txtbox.value ;
var lineCount = 0 ;
var lastEOL = -1 ;
do {
var begin = lastEOL+1 ;
lastEOL = content.indexOf("\n",lastEOL+1) ;
var line = "" ;
if(lastEOL != -1) {
line = content.substring(begin,lastEOL) ;
} else {
line = content.substring(begin,content.length) ;
}
var rows_in_line = Math.floor(line.length/cols)+1 ;
lineCount += rows_in_line
} while (lastEOL != -1) ;
txtbox.rows = lineCount ;
}
and html code :
<textarea name="my_textbox" onkeyup="autoresize(this)"
onmouseup="autoresize(this)" rows="24" cols="80"></textarea>