-
Notifications
You must be signed in to change notification settings - Fork 3
/
getimages.php
97 lines (74 loc) · 2.57 KB
/
getimages.php
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
function img_resize($src, $dest, $width, $height, $rgb = 0xFFFFFF, $quality = 100) {
if (!file_exists($src)) {
return false;
}
$size = getimagesize($src);
if ($size === false) {
return false;
}
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
$icfunc = 'imagecreatefrom'.$format;
if (!function_exists($icfunc)) {
return false;
}
$x_ratio = $width / $size[0];
$y_ratio = $height / $size[1];
if ($height == 0) {
$y_ratio = $x_ratio;
$height = $y_ratio * $size[1];
} elseif ($width == 0) {
$x_ratio = $y_ratio;
$width = $x_ratio * $size[0];
}
$ratio = min($x_ratio, $y_ratio);
$use_x_ratio = ($x_ratio == $ratio);
$new_width = $use_x_ratio ? $width : floor($size[0] * $ratio);
$new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio);
$new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
$new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
$isrc = $icfunc($src);
$idest = imagecreatetruecolor($width, $height);
imagefill($idest, 0, 0, $rgb);
imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);
imagejpeg($idest, $dest, $quality);
imagedestroy($isrc);
imagedestroy($idest);
return true;
}
function createThumbnail($img, $height) {
$dirName = dirname($img);
$fileName = basename($img);
$thumbsDirPath = $dirName.'/'.THUMBS_DIR;
$thumbPath = $thumbsDirPath.'/'.$fileName;
if (!file_exists($thumbsDirPath)) mkdir($thumbsDirPath);
$ans = $thumbPath;
if (!file_exists($thumbPath)) {
if (img_resize($img,$thumbPath,0,100)) {
$ans = $thumbPath;
} else {
$ans = DEFAULT_IMAGE;
}
}
return $ans;
}
const DEFAULT_IMAGE = 'default.gif';
const THUMBS_DIR = 'thumbs';
const THUMB_HEIGHT = 100;
const EMULATE_SLOW_CONNECTION = false;
$dir = "img/"; // задаём имя директории
if (is_dir($dir)) { // проверяем наличие директории
$files = scandir($dir); // сканируем (получаем массив файлов)
shuffle($files);
for ($i=0; $i < sizeof($files); $i++) {
$imgName = $files[$i];
$imgPath = $dir.$imgName;
if (is_file($imgPath)) {
$thumb = createThumbnail($imgPath, THUMB_HEIGHT);
echo '<a href="'.$imgPath.'" title="'.$imgName.'"><img src="'.$thumb.'" class="thumb" /></a>';
}
}
} else {
echo 'Error: there is no such directory ('.$dir.').<br>';
}
?>