· 8 years ago · Apr 15, 2018, 03:56 PM
1<?php
2$directory = $_GET['cat'].'/';
3$columns = 4;
4$thmb_width = 200;
5$thmb_height = 150;
6
7function resizeImage($originalImage,$toWidth,$toHeight){
8 // Get the original geometry and calculate scales
9 list($width, $height) = getimagesize($_GET['cat']."/".$originalImage);
10 $xscale=$width/$toWidth;
11 $yscale=$height/$toHeight;
12
13 // Recalculate new size with default ratio
14 if ($yscale>$xscale){
15 $new_width = round($width * (1/$yscale));
16 $new_height = round($height * (1/$yscale));
17 }
18 else {
19 $new_width = round($width * (1/$xscale));
20 $new_height = round($height * (1/$xscale));
21 }
22 // Resize the original image
23 $imageResized = imagecreatetruecolor($new_width, $new_height);
24 $imageTmp = imagecreatefromjpeg ($_GET['cat']."/".$originalImage);
25 imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
26
27 return $imageResized;
28}
29
30function generateThumbnails(){
31 global $directory,$thmb_width,$thmb_height;
32
33 // Open the actual directory
34 if ($handle = opendir($directory))
35 {
36 echo "'$directory' is a proper path/to/directory.";
37 // Read all file from the actual directory
38 while (false !== ($file = readdir($handle))) {
39 // Check whether tha actual item is a valid file
40 if (is_file($directory."/".$file)){
41 // Check whether the actual image is a thumbnail
42 if (strpos($file,'_th.jpg')){
43 $isThumb = true;
44 } else {
45 $isThumb = false;
46 }
47
48 if (!$isThumb) {
49 // Process the file string
50 $dirName = substr($directory.$file,0,strpos($directory.$file,basename($file)));
51 if (strlen($dirName) < 1) $dirName = '.';
52 $fileName = basename($directory.$file);
53 $fileMain = substr($fileName,0,strrpos($fileName,'.'));
54 $extName = substr($fileName,strrpos($fileName,'.'),
55 strlen($fileName)-strrpos($fileName,'.'));
56
57 // Check if the actual file is a jpeg image
58 if (($extName == '.jpg') || ($extName == '.jpeg')){
59 $thmbFile = $directory.'/'.$fileMain.'_th.jpg';
60 // If a thumbnail dosn't exists tahn create a new one
61 if (!file_exists($thmbFile)){
62 imagejpeg(resizeImage($file,$thmb_width,$thmb_height),$thmbFile,80);
63 }
64 }
65 }
66 }
67 }
68 } else {
69 echo "'$directory' is not a proper path/to/directory.";
70 }
71}
72
73function getNormalImage($directory,$file){
74 $base = substr($directory.$file,0,strrpos($directory.$file,'_th.jpg'));
75 if (file_exists($directory.$base.'.jpg')) return $directory.$base.'.jpg';
76 elseif (file_exists($directory.$base.'.jpeg')) return $directory.$base.'.jpeg';
77 else return "";
78}
79
80function displayPhotos($directory,$file){
81 global $columns;
82
83 generateThumbnails();
84 $act = 0;
85 // Open the actual directory
86 if ($handle = opendir($directory))
87 {
88 // Read all file from the actual directory
89 while (false !== ($file = readdir($handle))) {
90 // Check whether tha actual item is a valid file
91 if (is_file($directory."/".$file)){
92 // Check whether the actual image is a thumbnail
93 if (strpos($file,'_th.jpg')){
94 ++$act;
95 if ($act > $columns) {
96 echo '</tr><tr><td class="photo"><a href="'.getNormalImage($directory.$file).'"><img src="'.$directory.$file.'" alt="'.$file.'"/></a></td>';
97 $act = 1;
98 } else {
99 echo '<td class="photo"><a href="'.getNormalImage($directory.$file).'"><img src="'.$directory.$file.'" alt="'.$file.'"/></a></td>';
100 }
101
102 }
103 }
104 }
105 } else {
106 echo "'$directory' is not a proper path/to/directory.";
107 }
108}
109?>
110<table cellpadding="0" cellspacing="0" class="rounded3">
111<tr><?php if(isset($directory) && $directory != ''){ displayPhotos($directory); } ?></table>