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.
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.
-
function addEllipsis($string, $length, $end=‘…’)
-
{
-
{
-
$string .= $end; // $string = $string . $end;
-
}
-
return $string;
-
}
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:
-
function addEllipsis($string, $length, $end=‘…’)
-
{
-
}
I hope this will help
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:
- if…else statement,
- elseif statement,
- switch statement,
- if…else statement:
-
if (condition) {
-
statements_1
-
} [else {
-
statements_2
-
}]
-
$msg = "Stop!";
-
if ($light == "green")
-
$msg = "Go!";
-
echo $msg;
-
<?= ($light == "green") ? "Go!" : "Stop!"; ?>
-
$msg = ($light == "green") ? "Go!" : "Stop!";
-
echo $msg;
- elseif statement:
-
if (condition_1) {
-
statement_1
-
}
-
[elseif (condition_2) {
-
statement_2
-
}]
-
…
-
[elseif (condition_n-1) {
-
statement_n-1
-
}]
-
[else {
-
statement_n
-
}]
- switch statement:
-
switch (expression) {
-
case label_1:
-
statements_1
-
[break;]
-
case label_2:
-
statements_2
-
[break;]
-
…
-
case label_n-1:
-
statements_n-1
-
[break;]
-
[default:
-
statements_n
-
[break;]]
-
}
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
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:
the same scenario can be described as
but I like to use the following short code instead
the same as
or
Note: Open and Close brackets are optional if the set of code to be executed is only one statements.
When evaluating a boolean expression to true or false use the operator “===” or “!==” instead of “==” or “!=”
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.
This statement is used if the set of code to execute depend on a sequence of multiple conditions.
2-1 Syntax
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”.
2-3 The if(); elseif(); else; endif;`alternative’:
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.
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
3-2 Example
Back to our “traffic light” example with “switch”:
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.
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.
-
<?php
-
/**
-
* Class ColorRGB
-
* Description: this is a rgb base color class
-
* Author: Rachid Lamtabbet
-
* Version: 1.0
-
* Author URI: http://www.alfasky.com
-
* please read the license
-
*/
-
class ColorRGB {
-
function __construct() {
-
…
-
);
-
}
-
-
public function Color2RGB($color=‘black’) {
-
return $this->RGB[$this->$color];
-
}
-
}
-
?>
The class is very easy to use, just create your instance,
-
<?php
-
$color = new ColorRGB();
-
$this->black = $color->Color2RGB(‘black’);
-
$this->lightgrey = $color->Color2RGB(‘lightgrey’);
-
$this->snow = $color->Color2RGB(‘snow’);
-
$this->white = $color->Color2RGB(‘white’);
-
?>
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.
-
<?php
-
$shape->setLine($unit, $this->snow[0], $this->snow[1], $this->snow[2]);
-
?>
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.
![]()
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.



