PHP

How to Convert Pdf to Tiff

If you are looking for an easy open source solution on how to convert a pdf to tiff and you are reading this article you may just got lucky today, because it tooks me more than four hours to get to this point.

There are many reasons to convert a pdf to a tiff format, in my case it was a simple conversion that will provide a specifics tiff format (Group 4) to be emailed (using PHP mail function) to a fax machine then faxed to the receptionists.


Read the rest of this entry »

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

PHP: Automatic ellipsis for blocks of text

This little function will help you truncate a string to a specified length when copying data to a place where you can only store or display a limited number of characters, then it will append “…” to it showing that some characters were removed from the original entry.

  1. function addEllipsis($string, $length, $end=‘…’)
  2. {
  3.   if (strlen($string) > $length)
  4.   {
  5.     $length -=  strlen($end);  // $length =  $length – strlen($end);
  6.     $string  = substr($string, 0, $length);
  7.     $string .= $end;  //  $string =  $string . $end;
  8.   }
  9.   return $string;
  10. }

Well this is very straightforward isn’t it? Anyways let’s explain how this works: The function take 3 arguments; the string to truncate, the maximum length allowed, and a default text, typically “…”.
We first start by checking the string length against the maximum length allowed. If it’s is greater than the max length then we have to trim the string to the specified length, otherwise we return it intact.

Trimming the string is very easy:
1- Recalculate the max length by subtracting the extra text length from it.
2- Truncate the string to the new length.
3- Append the extra text then return the new value.

You like short code, then you got it:

  1. function addEllipsis($string, $length, $end=‘…’)
  2. {
  3.   return (strlen($string) > $length) ? substr($string, 0, $lengthstrlen($end )) . $end : $string;
  4. }

I hope this will help :-)

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...

Conditional Statements in PHP

Just like in any other development language, PHP make use of the conditional statements to perform different actions based on different conditions and situations.

PHP offers three types of conditional statements:

  1. if…else statement:
  2. This statement is used if we want to execute a set of code when a condition is true and another one or none if the condition is false

    1-1 Syntax
    1. if (condition) {
    2.   statements_1
    3. } [else {
    4.   statements_2
    5. }]

    if condition is true, statement_1 is executed. if the optional else clause is specified, statement_2 will take place if the condition is false.

    1-2 Example

    The following example reflect a “traffic light” scenario:

    1. if ($light == "green") {
    2.   echo "Go!";
    3. } else {
    4.   echo "Stop!";
    5. }

    the same scenario can be described as

    1. $msg = "Stop!";
    2. if ($light == "green")
    3.   $msg = "Go!";
    4. echo $msg;

    but I like to use the following short code instead

    1. <?php echo ($light == "green") ? "Go!" : "Stop!"; ?>

    the same as

    1. <?= ($light == "green") ? "Go!" : "Stop!"; ?>

    or

    1. $msg = ($light == "green") ? "Go!" : "Stop!";
    2. echo $msg;

    Note: Open and Close brackets are optional if the set of code to be executed is only one statements.

    1. if ($light == "green")
    2.    echo "Go!";
    3. else
    4.    echo "Stop!";

    When evaluating a boolean expression to true or false use the operator “===” or “!==” instead of “==” or “!=”

    1. if ($green_light === true) {
    2.    echo "Go!";
    3.    echo "I Said Go!";
    4. } else
    5.    echo "Stop!";

    Note: Try to combine as many conditions as you can using the logic operator || (or), && (and) etc… instead of breaking your code to multiple statements.

  3. elseif statement:
  4. This statement is used if the set of code to execute depend on a sequence of multiple conditions.
    2-1 Syntax

    1. if (condition_1) {
    2.    statement_1
    3. }
    4. [elseif (condition_2) {
    5.    statement_2
    6. }]
    7. [elseif (condition_n-1) {
    8.    statement_n-1
    9. }]
    10. [else {
    11.    statement_n
    12. }]
    2-2 Example

    In the previous “traffic light” example we had only two cases, either the light is green or unknown. We wanted the drivers to stop in all cases except when the light is green. but this is not what we see in a real life. Let’s change this light color to “white”, rotate it little bit and keep it for our pedestrians. In this example we will be dealing with a typical “traffic light”.

    1. if ($light == "green") {
    2.     echo "Go!";
    3. } elseif ($light == "yellow") {
    4.     if ($light == "flashing")
    5.          echo "Slow down and be especially alert!";
    6.     else
    7.         echo "Slow down and be prepared to Stop!";
    8. } else {
    9.      echo "Stop!"
    10. }
    2-3 The if(); elseif(); else; endif;`alternative’:
    1. if ($light == "green") :
    2.     echo "Go!";
    3. elseif ($light == "yellow") :
    4.     if ($light == "flashing") :
    5.          echo "Slow down and be especially alert!";
    6.     else :
    7.         echo "Slow down and be prepared to Stop!";
    8.     endif;
    9. else :
    10.     echo "Stop!";
    11. endif;
    12. }

    Obviously, there is more complex cases. Some “traffic light” have an orange color instead of yellow, a flashing red and a flashing green may be. anyways in this case using the previous conditional statement become ambiguous, very complex, and hard to maintain. As a best practice using the “switch” statement comes more handy in terms of readability than a sequence of if elses.

  5. switch statement:
  6. The “switch” statement have the same magic effect as a sequence of “if elses”, only that the switch statement provide a non-iterative choice between any number of set of code based on specified conditions. it compare an expression to a set of labels and select any matching ones. An optional default case is executed if no much was found.
    1-1 Syntax

    1. switch (expression) {
    2.    case label_1:
    3.       statements_1
    4.       [break;]
    5.    case label_2:
    6.       statements_2
    7.       [break;]
    8.    …
    9.    case label_n-1:
    10.       statements_n-1
    11.       [break;]
    12.    [default:
    13.      statements_n
    14.      [break;]]
    15. }
    3-2 Example

    Back to our “traffic light” example with “switch”:

    1. switch ($light) {
    2.    case "green":
    3.       echo "Go!";
    4.       break;
    5.    case "Orange":
    6.       echo "Slow down and be prepared to Stop!";
    7.       break;
    8.    case "yellow":
    9.       echo "Slow down and be prepared to Stop!";
    10.       break;
    11.   case "flashing":
    12.       echo "Slow down and be especially alert!";
    13.       break;
    14.    default:
    15.      echo "Stop!";
    16.      break;
    17. }

    You can imagine any scenario you want and make this example more accurate. the default statement will take place if the light is “red” or in any non specified case like when a light is broken or any other strange case that may occur.
    Note: The switch can be a little tricky, especially because you can easily forget to place the terminating colons, the break statement, or forget some conditions. I personally always use the optional “default” case, it’s like handling exceptions.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

PHP RGB Color Class Make it Simple

I have created this Class while working on a PHP/Ming project to simplify my coding experience. Now it’s time to share this experience with my visitors.
ColorRGB is a PHP base color class and a simple container for the pixel red, green, and blue values. All you have to do is instantiate it and enjoy your color manipulation.

  1. <?php
  2. /**
  3. * Class ColorRGB
  4. * Description: this is a rgb base color class
  5. * Author: Rachid Lamtabbet
  6. * Version: 1.0
  7. * Author URI: http://www.alfasky.com
  8. * please read the license
  9. */
  10. class ColorRGB {
  11.         private $RGB = array();
  12.         function __construct() {
  13.           $this->RGB=array(
  14.             ‘aliceblue’             => array(240, 248, 255),
  15.             ‘antiquewhite’          => array(250, 235, 215),
  16.             ‘aqua’                  => array(  0, 255, 255),
  17.             …
  18.             ‘yellow’                => array(255, 255,   0),
  19.             ‘yellowgreen’           => array(154, 205,  50)
  20.           );
  21.         }
  22.  
  23.         public function Color2RGB($color=‘black’) {
  24.            $this->$color= strtolower($color);
  25.            return $this->RGB[$this->$color];
  26.         }
  27. }
  28. ?>

The class is very easy to use, just create your instance,

  1. <?php
  2. $color = new ColorRGB();
  3. $this->black = $color->Color2RGB(‘black’);
  4. $this->lightgrey = $color->Color2RGB(‘lightgrey’);
  5. $this->snow = $color->Color2RGB(‘snow’);
  6. $this->white = $color->Color2RGB(‘white’);
  7. ?>

the default color is black, no alpha is specified, this could be the subject of the next release. If you are using this class with the ming for a “Fill” color
know that your color object is an array(), use the index to separate your RGB values.

  1. <?php
  2. $shape->setLine($unit,  $this->snow[0], $this->snow[1], $this->snow[2]);
  3. ?>

You are free to use the class for any purpose just attribute the work by providing a link to AlfaSky and keep the author name and signature.
download

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Welcome to AlfaSky!

Hello everyone,
hope this blog will get you some help, I’m the owner of minassa.com. I have decided to move most of my articles to my blog to better serve and discuss their subjects.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...