-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.php
More file actions
121 lines (107 loc) · 3.71 KB
/
generator.php
File metadata and controls
121 lines (107 loc) · 3.71 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
require 'connection.php';
$name = $_GET['name'];
$size = $_GET['size'];
$sql1 = $pdo->prepare("SELECT width, height FROM sizes1 WHERE code = :size");
$sql1->execute(array(':size' => $size));
$row = $sql1->fetch();
$w = (int)$row['width'];
$h = (int)$row['height'];
$cache_ext = fileBuildPath('cache_ext');
if (!file_exists($cache_ext)) {
mkdir($cache_ext);
}
function fileBuildPath(...$segments): string {
return join(DIRECTORY_SEPARATOR, $segments);
}
/**
* @param $filepath
* @return GdImage|resource
* @throws Exception
*/
function imageCreateFromAny($filepath) {
$type = exif_imagetype($filepath);
switch ($type) {
case IMAGETYPE_GIF :
$im = imageCreateFromGif($filepath);
break;
case IMAGETYPE_JPEG :
$im = imageCreateFromJpeg($filepath);
break;
case IMAGETYPE_PNG :
$im = imageCreateFromPng($filepath);
break;
}
if (!$im) {
throw new Exception("Не удалось открыть изображение. Возможно, файл не является одним из типов: GIF, JPEG/JPG, PNG");
}
return $im;
}
/**
* @param $name
* @param $width_new
* @param $height_new
* @return string
* @throws Exception
*/
function doImagePreview($name, $width_new, $height_new): string {
$hash_dir_name = md5($name);
$dir_path = fileBuildPath('cache_ext', $hash_dir_name);
if (!file_exists($dir_path)) {
mkdir($dir_path);
}
$filename_preview = fileBuildPath('cache_ext', $hash_dir_name, $width_new . $height_new . '.jpg');
$filename_original = fileBuildPath('gallery', $name);
if (!file_exists($filename_original) or !is_readable($filename_original)) {
throw new Exception("файл {$name} по пути {$filename_original} не существует или не доступен");
}
if (!in_array(mime_content_type($filename_original), ['image/gif', 'image/jpeg', 'image/png'])) {
throw new Exception("файл {$name} не является одним из типов: GIF, JPEG/JPG, PNG");
}
$info = getimagesize($filename_original);
if (!$info) {
throw new Exception("файл {$name} не является изображением");
}
$width_original = $info[0];
$height_original = $info[1];
try {
$img = imageCreateFromAny($filename_original);
} catch (Exception $e) {
echo 'Выброшено исключение: ', $e->getMessage(), "\r\n";
}
$tmp = imageCreateTrueColor($width_new, $height_new);
imageCopyResampled($tmp, $img, 0, 0, 0, 0, $width_new, $height_new, $width_original, $height_original);
imagejpeg($tmp, $filename_preview, 100);
imagedestroy($tmp);
return $filename_preview;
}
/**
* @param $name
* @param $width_new
* @param $height_new
* @return string
*/
function getImagePreview($name, $width_new, $height_new): string {
$hash_dir_name = md5($name);
$filename_preview = fileBuildPath('cache_ext', $hash_dir_name, $width_new . $height_new . '.jpg');
if (!file_exists($filename_preview))
{
try {
$filename_preview = doImagePreview($name, $width_new, $height_new);
} catch (Exception $e) {
echo 'Выброшено исключение: ', $e->getMessage(), "\r\n";
}
}
return $filename_preview;
}
$url = getImagePreview($name, $w, $h);
try {
if (!file_exists($url)) {
throw new Exception("не удалось сгенерировать изображение из {$name} по пути {$url}");
}
header('Content-type: image/jpeg');
readfile($url);
}
catch (Exception $e) {
echo 'Выброшено исключение: ', $e->getMessage(), "\r\n";
}