PHP image gallery and filter images with color

Topics focus on Web Developments, Programming, Database, Digital Marketing Tutorials, Code Snippets, SEO, Social Media Marketing and Inspirational articles.

PHP image gallery and filter images with color 1

PHP image gallery and filter images with color

PHP image gallery and filter images with color

With PHP you know how to create a gallery. But here you will not only create a gallery but also extract Color from the uploaded image. So next time you give the user to filter images with color.

Creating this type of gallery is very easy and user friendly. Basic file upload knowledge is compulsory.

Then you include one file called “colors.inc.php” (you can find this in the downloaded folder) including file and doing actual stuff here.

Create table called php_image_gallery with following query:

1
2
3
4
5
6
7
8
9
 
CREATE TABLE `php_image_gallery` (
 
`id` BIGINT( 20 ) NOT NULL AUTO_INCREMENT ,
`path` TEXT NOT NULL ,
`colors` TEXT NOT NULL ,
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY ( `id` )
) ENGINE = InnoDB;

Note: include “colors.inc.php” file for color filtering

The following code indicates that how to upload a file and extract colours from it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
 
if(isset($_POST['upload'])){
 
$error='';
if(!empty($_FILES['image']['tmp_name'])){
 
//image data
$imagedata= getimagesize($_FILES['image']['tmp_name']);
 
if(substr($imagedata['mime'],0,5)=='image'){
	$file=time().$_FILES['image']['name'];
	$path="uploads/".$file;
 
	//checking whether image is uploaded or not, we use uploaded image
if(move_uploaded_file($_FILES['image']['tmp_name'],$path)){
//defining parameters
$delta=24;
$reduce_brightness=true;
$reduce_gradients=true;
$num_results=4;
 
//including file
include_once("colors.inc.php");
 
/************
* using the class defination
************/
$ex=new GetMostCommonColors();
 
//passing parameters
$colors=$ex->Get_Color($path,$num_results,$reduce_brightness,$reduce_gradients,$delta);
 
$keys=array_keys($colors);
$colorsCommaSeperated=implode($keys,",");
//inserting data into database. image path and colors with comma seperated
$sql=mysql_query("INSERT INTO php_image_gallery VALUES('', '".$file."', '".$colorsCommaSeperated."', CURRENT_TIMESTAMP)");
if($sql){
	header("location:index.php");
}
else{
	die("Error in inserting data");
}
}
 
}else{
	die("Uploaded file is not an image");
}
}
}
 
?>

And here we insert data into database.

We fetch all colors from the table and remove duplicates using array_unique().

Then we create color pallet as shown in the figure(side bar).

First we display all images without filtering. When user selects the particular color then images will be filtered with the selected color.

One comment

Add comment


 

Pin It on Pinterest

Share This