/
engine.php
149 lines (117 loc) · 5.07 KB
/
engine.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
error_reporting(E_ALL);
ini_set('log_errors', 'On');
ini_set('error_log', 'error.log');
header('Content-Type: text/html; charset=utf-8');
class Engine
{
private $img_format = array('jpg', 'gif', 'png');
private $watermark = 'watermark.png';
function __construct()
{
$this->sql = new mysqli('localhost', 'root', '', 'testovoe');
}
//Возвращает тип файла
private function typeFile($image)
{
$name = basename($image);
$name = explode('.', $name);
return end($name);
}
//Проверка является ли файл изображением и был ли ранее загружен
private function checkImage($image)
{
if (!empty($image)) {
//Проверка формата файла картинки
if (in_array($this->typeFile($image), $this->img_format)) {
//Формат соответсвует разрешенному, проверим была ли ранее загружена такая ссылка
$query = $this->sql->query("SELECT `id` FROM `images` WHERE `url`='" . $this->sql->escape_string($image) . "'")->fetch_assoc();
if (empty($query['id'])) return true; else return false;
} else {
return false;
}
} else {
return false;
}
}
//Ресайз и наложение водяного знака
private function image_resize($source_path, $destination_path, $newwidth, $newheight = false, $quality = false)
{
ini_set("gd.jpeg_ignore_warning", 1);
list($oldwidth, $oldheight, $type) = getimagesize($source_path);
switch ($type) {
case IMAGETYPE_JPEG:
$typestr = 'jpeg';
break;
case IMAGETYPE_GIF:
$typestr = 'gif';
break;
case IMAGETYPE_PNG:
$typestr = 'png';
break;
}
$function = "imagecreatefrom$typestr";
$src_resource = $function($source_path);
if (!$newheight) {
$newheight = round($newwidth * $oldheight / $oldwidth);
} elseif (!$newwidth) {
$newwidth = round($newheight * $oldwidth / $oldheight);
}
$destination_resource = imagecreatetruecolor($newwidth, $newheight);
//Водяной знак
$watermark = imagecreatefrompng($this->watermark);
list($mark_width, $mark_height) = getimagesize($this->watermark);
imagecopyresampled($src_resource, $watermark, 0, 0, 0, 0, $mark_width, $mark_height, $mark_width, $mark_height);
imagecopyresampled($destination_resource, $src_resource, 0, 0, 0, 0, $newwidth, $newheight, $oldwidth, $oldheight);
if ($type = 2) {
imageinterlace($destination_resource, 1); // чересстрочное формирование изображение
imagejpeg($destination_resource, $destination_path, $quality);
} else {
$function = "image$typestr";
$function($destination_resource, $destination_path);
}
imagedestroy($destination_resource);
imagedestroy($src_resource);
}
//Обработка формы загрузки
public function upload()
{
if ($_FILES['images']['tmp_name']) {
$images = file_get_contents($_FILES['images']['tmp_name']);
$images = explode("\r\n", $images);
foreach ($images as $key => $val) {
if ($this->checkImage($val)) {
//Если файл прошел проверку, запишем ссылку в БД и получим ИД инкримента для присвоения имени
$this->sql->query("INSERT INTO `images` (`url`) VALUES ('" . $this->sql->escape_string($val) . "')");
$fileName = $this->sql->insert_id . '.' . $this->typeFile($val);
//Скачиваем картинку, сохраняем и ресайзим
$content = file_get_contents($val);
$file = 'upload/' . $fileName;
file_put_contents($file, $content);
$this->image_resize($file, $file, '', '200', '100');
}
}
}
}
public function images()
{
$html = '';
$result = $this->sql->query("SELECT * FROM `images`");
while ($data = $result->fetch_assoc()) {
$html .= '<div class="item"><img src="upload/' . $data['id'] . '.' . $this->typeFile($data['url']) . '"></div>';
}
return $html;
}
}
$Engine = new Engine();
switch ($_GET['mod']) {
case 'upload':
echo $Engine->upload();
break;
case 'images':
echo $Engine->images();
break;
default:
echo 'Ошибка 404, страница не найдена!';
break;
}