It’s been awhile ๐ Happy New Year, by the way. I’ve been busy with work. But glad to have the time tonight. On this particular project that I did, I’ve come to realize that with every project, there will always be something new. The most fun part is the learning experience you have along while working on it. Tonight, I will share new (but might not be new to you) stuffs I’ve learned.
1. Creating transparency on existing image.
Not until today that I’ve realized the importance of GD Library.There was a particular task that I need to modify and add transparency to an existing image. GD library worked like charm on this. To be able to do it effectively, the existing image should have a dark color that could easily be distinguish and separated from the entire image. Here is the snippet:
<?php
$imagefile = "image/sample.png";
$im = imagecreatefrompng($imagefile);
$black = imagecolorallocate($im, 0, 0, 0); // search for the color, which is black
imagecolortransparent($im, $black); // replace all black pixel values to be transparent
imagepng($im, $imagefile);
imagedestroy($im);
?>
2. Determine if the value is odd or even.
This becomes handy when you need to display alternate table row colors.
<?php
function is_odd($num)
{
return( $num& 1 );
}
?>
3. Return two or more variables from a function.
Shame on me that I’m not aware of this. Oh well… good for me now. ๐
<?php
function basicMath($a, $b){
$sum = $a + $b;
$difference = $a - $b;
return array($sum, $difference); //return as array values.
}
$a = 5;
$b = 3;
list($sum, $difference) = basicMath( $a, $b); //returns two variables via list()
echo $a." + ".$b." = ".$sum;
echo $a." - ".$b." = ".$difference;
?>
4. Sort arrays and retain its original keys.
There was a particular query that I need to add sorting features. But the hard part was, the query was so complicated that sorting couldn’t be possible from there. What I did was to get all the necessary fields and rows, put them on arrays. On the specific array that will be the lead and sorted out. I’ve used the asort and arsort function. Retaining the keys helped in organizing and order the other fields that are associated to it.
Here is a sample:
<?php
$firstname = array('Ana','Billy','Carol');
$lastname=array('Dela Cruz','Garcia','Rodriguez');
$average=array('85','96','90');
asort($firstname); //sort in ascending order
foreach($firstname as $key => $value)
{
echo $value." ".$lastname[$key]." average: ".$average[$key];
}
//sample output Ana Dela Cruz average: 85
?>
5. Get image size.
getimagesize() is handy too. I used this to determine the amount of space needed to adjust the page layout.
<?php
list($width, $height, $type, $attr) = getimagesize("image/sample.png");
?>
6. Adjusting symbol display on fpdf output
I had trouble displaying the registered trademark symbol and apostrophe on generated pdf using fpdf library. Using str_replace() and iconv() solved the issue.
<?php
str_replace("®", iconv("UTF-8", "ISO-8859-1", "ยจ"), str_replace("™", iconv("UTF-8", "ISO-8859-1", "ยช"), $row['title'])); //outputs the title with registered trademark symbol
str_replace('รขโฌโข', iconv('UTF-8', 'ISO-8859-1', " ' "),$title); //outputs the title with apostrophe
?>
So that’s about it. Hope this helps! ๐