Wednesday 12 August 2015

php image gallery



Steps:


  1. create your main folder. [example pictures ]  [ mkdir pictures]
  2. create another folder as images under pictures [ mkdir pictures/images ] [ NOTE: In the following code the subfolder name it expect as images ]
  3. copy the following code to pictures folder, as filename index.php
  4. copy all your images to pictures/images




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Gallery from Folder Demo</title>
<style type="text/css">
<!--
li{
list-style-type:none;
margin-right:10px;
margin-bottom:10px;
float:left;
}


-->
</style></head>


<body>


<ul>
<?php
$dirname = "images/";
$images = scandir($dirname);
shuffle($images);
$ignore = array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<li><a href=\"$dirname$curimg\"><img src=\"$dirname$curimg\" height=200 width=200 /></a></li>\n ";
}
}
?>
</ul>


</body>
</html>


### compress image ###

Along with the above you also might need to compress the file.

at ubuntu, you can use "convert" command to compress the image.

NOTE: the smaller the percentage the smaller is the file.

convert source.jpg -resize 20% destination.jpg

A 4MB file 20% came around 350KB file.