Posts Tagged with php

Displaying 31-40 of 59 results.
Wiki: PHP Dominant Colour Function with GD Librarywiki
posted by admin on April 13, 2016
Someone asked if it was possible to find the dominant colour (the more frequent colour, in %) of an image.

So I read about the GD PHP Library, which enables image support. I found the interesting function imageColorAt() which the colour of a specific pixel. Hence, my function consists of resizing to a known size, 100 x 100, then looping into each pixel, storing it’s colour into an array, then sorting the array to find the more frequent key.

Here’s the result :

function getDominantColour($image, $type='jpeg') // $type can be gif, jpeg or png
// This function needs GD Library enabled

eval('$source = imagecreatefrom' . $type . '("' . $image . '");'); // we load the source image
$source_x = imagesx($source); // source's X
$source_y = imagesy($source); // source's Y
$resized_x = 100; // resized image's X
$resized_y = 100; // resized image's Y
$resized= imagecreatetruecolor($resized_x, $resized_y); // we create a blank 100 x 100 image
imagecopyresampled($resized, $source, 0, 0, 0, 0, $resized_x, $resized_y, $source_x, $source_y); // We then copy and resized the source image into the blank image

// Required vars declaration
$colours = array();
$rgb = '';
$index = array();
for ($x=0; $x<100; $x++) // We loop into the pixels X
for ($y=0; $y<100; $y++) // We loop into the pixels Y
$rgb = imagecolorat($resized, $x, $y); // get the RGB of the pixel
$index = imagecolorsforindex($resized, $rgb); // Get the RGB Index
$key = 'R' . $index['red'] . 'G' . $index['green'] . 'B' . $index['blue']; // Put it into a string
if (empty($colours[$key])) // If it's the first time this colour is encountered
$colours[$key] = 1; // We give it a value of 1
} else { // If it's not the first time it is encountered
$colours[$key]++; // We increment its value
arsort($colours, SORT_NUMERIC); // We then sort the colours array so that the first one is the most frequent one
return key($colours); // we return the most frequent value

Working with images sure is fun. Looking forward to have more functions with the GD libraryRead more
Wiki: PHP Convert Hex Color to RGB and RGB to Hexwiki
posted by admin on April 13, 2016
Converting certain values back and forth is often necessary when developing functions to help your website work the way you want it to. I needed a function that would convert a hex color to rgb and for some reason it took me a while to figure it out.

function hex2rgb($hex) {
$hex = str_replace("#", "", $hex);

if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
$rgb = array($r, $g, $b);
//return implode(",", $rgb); // returns the rgb values separated by commas
return $rgb; // returns an array with the rgb values

This function works with both shorthand hex codes such as #f00 and longhand hex codes such as #ff0000. It also accepts the number sign (#) just in case. You can see there are two return lines at the end of the function. The first, which is commented out, will return the rgb values separated by a comma. The second, which is the default, will return an array with the rgb values.

So now with this function in place we can use it like so:

$rgb = hex2rgb("#cc0");

The above function would output:

Array ( [0] => 204 [1] => 204 [2] => 0 )

Since we are going one way with this, might as well go the other. Here is a function to convert rgb to a hex color:

function rgb2hex($rgb) {
$hex = "#";
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

return $hex; // returns the hex value including the number sign (#)

This function works like so:

$rgb = array( 255, 255, 255 );
$hex = rgb2hex($rgb);
echo $hex;

The above function would output:

Read more
Resolved: Yii file save error Directory must be writable
posted by admin on April 11, 2016
I have form, and I create directory by mkdir

mkdir($mydirectory, 0777, true);

And got an error during save my file, which I try to upload from my _form.php

Directory must be writable
Read more
Resolved: Yii get last parameter from current url without GET name getQuery
posted by admin on April 10, 2016
My controller

public function actionVideo()
//want to get last url parametr

My URL like

I want to get dubaivideosRead more
Resolved: Convert an associative array to a simple array of its values in php
posted by admin on April 7, 2016
I would like to convert the array:

Array (
[category] => category
[post_tag] => post_tag
[nav_menu] => nav_menu
[link_category] => link_category
[post_format] => post_format


array(category, post_tag, nav_menu, link_category, post_format)
Read more
Resolved: PHP remove same values from two arrays
posted by admin on April 5, 2016
I have two arrays

$array1 = array("a" => "green", "red", "blue", "red");


$array2 = array("b" => "green", "yellow", "red");

I want to compare values, and get new array with unique elements.

[1] => blue
Read more
Resolved: PHP remove duplicate elements from array - unique array
posted by admin on April 4, 2016
I have array

$input = array("a" => "green", "red", "b" => "green", "blue", "red");

I want compose unique array from my array, without same duplicate valuesRead more
Resolved: PHP reverse array
posted by admin on March 31, 2016
How to reverse my arrayRead more
Resolved: PHP date or datetime to Yesterday, Today
posted by admin on March 30, 2016
In my social website, I want to convert datetimes in format, like
YesterdayRead more
Resolved: PHP trim my strings in array
posted by admin on March 27, 2016
I have array, and I want to trim my array elements
My array

$arr = array(" one", " two", " three ", ..., etc);
Read more