Programming

How to Install PHP 7 on CentOS 6

Previously i posted about upgrading PHP 5.3 to PHP 5.6 on CentOS 6.
If you want to install / upgrade it to PHP 7, here’s how:

1. Make sure to remove older version of PHP
yum remove php-common

2. Add EPEL and Remi repository
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
yum install http://rpms.remirepo.net/enterprise/remi-release-6.rpm

3. Install yum-utils
yum install yum-utils

4. Enable Remi repository
yum-config-manager --enable remi-php72

5. Now install PHP 7
yum install php php-xml php-mcrypt php-cli php-gd php-curl php-mysql php-ldap php-zip php-fileinfo

6. When it’s done, restart your web server (apache or nginx)

7. Check your PHP version
php -v

Hope it helps.

 

 

Programming

How to Upgrade PHP 5.3 to PHP 5.6 on CentOS 6

Follow this post to install PHP 7 to your CentOS 6

Nowadays, to keep up up-to-date with the latest PHP technology, you need at least PHP version 5.6 installed on your server.
But for CentOS 6 users, PHP 5.3.3 is the latest version of PHP available through the official CentOS package repository.

To update your PHP version to version 5.6, follow these steps:

  1. Install repository
    rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm
  2. Remove older PHP version
    yum remove php-common
  3. Install PHP 5.6
    yum install php56w php56w-mysql php56w-common php56w-pdo php56w-opcache
  4. Restart Apache
    service httpd restart
  5. Check PHP version
    php --version
    You will get something like:

    PHP 5.6.32 (cli) (built: Oct 29 2017 19:00:01)
    Copyright (c) 1997-2016 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

Congratulation, your PHP is now upgraded to version 5.6!

 

 

Programming

Convert PHP object to an array recursively

In PHP, you can easily convert an object to an array by typecasting it.

$array = (array) $object;

But if you have multi-dimensional / nested objects, this will only converts the top level of the object. So instead of looping through the object and typecasting it, just use this single line code:

$array = json_decode(json_encode($object), true);

And to convert an array to an object recursively:

$object = json_decode(json_encode($array, JSON_FORCE_OBJECT), false);

note: This only works if the properties of the object you are trying to convert are declared as public

Programming

PHP-GD: Create Image from Text

Some people want to hide their text into image for any reason. As example, to avoid spambot steal their email address, or to generate dynamic images content.

Its simply done with PHP and GD Library. See this simple function:

function imgfromstring($string){
	$font  = 5;
	//dynamic size of image. to fit your text.
	$width  = ImageFontWidth($font) * strlen($string);
	$height = ImageFontHeight($font);

	//start creating the image
	$gbr = imagecreate ($width,$height);

	//set background to white
	$bgcolor = imagecolorallocate($gbr, 255, 255, 255);
	//set color for text
	$txtcolor = imagecolorallocate ($gbr, 0, 0, 0);

	//generate the image
	imagestring($gbr, $font, 0, 0, $string, $txtcolor);
	header("Content-type: image/jpeg");
	return imagejpeg($gbr);
}

//usage:
$txt = "[email protected]";
echo imgfromstring($txt);

example output:

Another sample is using image as the background. Its useful when you need to create waterwark to protect your images.
Sample code:

function toImgWithBG($str){
	$bg = "background.jpg";
	$font = 5; //Font size

	$gbr =imagecreatefromjpeg($bg);

	//Calculate string width and height
	$pos_x  = imagesx($gbr) - (ImageFontWidth($font) * strlen($str));
	$pos_y = imagesy($gbr) - ImageFontHeight($font);

	//set text color
	$teks = imagecolorallocate($gbr, 255, 0, 0);

	//generate the image, and put on the right bottom
	imagestring($gbr, 5, $pos_x, $pos_y, $str, $teks);

	header("Content-type: image/jpeg");
	return imagejpeg($gbr);
}

//Usage:
echo toImgWithBG("copyright: http://www.akemapa.com/");

example output:

You can download source code of this example here:
download here download this file

Programming

PHP-GD: Resize Transparent Image PNG & GIF

By default, you will get black background if you resize a transparent image. Play Daisy slots to see how good the quality of the background images are. To fix it, you need set alpha channel imagecolorallocatealpha to 127.
With imagecolorallocatealpha, it will allocate a color for an image.

Usage:
int imagecolorallocatealpha ( resource image, int red, int green, int blue, int alpha)

From PHP manual:
imagecolorallocatealpha() behaves identically to imagecolorallocate() with the addition of the transparency parameter alpha which may have a value between 0 and 127. 0 indicates completely opaque while 127 indicates completely transparent.
Returns FALSE if the allocation failed.

Before using it, you must set to false the blending mode for an image and set true the flag to save full alpha channel information.

Example:

<? $newImg = imagecreatetruecolor($nWidth, $nHeight); imagealphablending($newImg, false); imagesavealpha($newImg,true); $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127); imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent); imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]); ?>
Programming

PHP: Procedural vs. Object Oriented Coding Style

PHP allows you to write code in two flavours, one is procedural and the other
is object oriented. You can even write procedural code in PHP5 and it will run
without any problems. If you are not clear about procedural and object oriented
programming, then we will have a look at these two different coding styles. The
following two examples are not fully running examples rather a pseudo code:

<?
$user_input = $_POST[‘field‘];
$filtered_content = filter($user_input); //user input filtering
mysql_connect(“dbhost”,”dbuser”,”dbpassword”); //database
mysql_select_db(“dbname”);
$sql = “some query”;
$result = mysql_query($sql);
while ($data = mysql_fetch_assoc())
{
process ($data);
}
process_user_input($filtered_content);
?>

You will notice using a lot of inline processing either directly or via using functions.
It may stand as an example of typical procedural operation. Let’s see how it looks
after converting it to OOP:

<?
$input_filter = new filter();
$input_filter->filter_user_input(); //filter the user inputs
$db = new dal(“mysql”); //data access layer
$db->connect($dbconfig); //using mysql
$result = $db->execute($sql);
ReportGenerator::makereport($result); //process data
$model = new Postmodel($filter->get_filtered_content());
$model->insert();
?>

Now if you take a look into these two code snippets, you will find that the latter
one is much more readable. Well, you can make the first one more readable by
introducing some more functions into it, but how many functions are you ready
to search into when you use them? The latter snippet is better organized because
you know which object is handling which process. If you write big applications in
procedural style, it will be almost impossible to manage after a few versions. Of
course you can implement strict coding conventions, but it is agreed by millions
of developers that it won’t give you the ultimate manageability and usability if it’s
procedural unless you do it in OO style. Almost all big applications are written using
the object oriented approach.