Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
**/.vs/
**/dist/
**/.idea
**/.sublime-project
**/.sublime-workspace
18 changes: 18 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
define('ROOT', dirname(__DIR__));
define('IMG_BIG', $_SERVER['DOCUMENT_ROOT'] . '/images/gallery_img/big/');
define('IMG_SMALL', $_SERVER['DOCUMENT_ROOT'] . '/images/gallery_img/small/');
define('TEMPLATES_DIR', ROOT . '/templates/');
define('LAYOUTS_DIR', 'layouts/');

/* DB config */
define('HOST', 'localhost');
define('USER', 'root');
define('PASS', 'root');
define('DB', 'images');

include ROOT . "/engine/db.php";
include ROOT . "/engine/functions.php";
include ROOT . "/engine/log.php";
include ROOT . "/engine/gallery.php";
include ROOT . "/engine/classSimpleImage.php";
85 changes: 85 additions & 0 deletions engine/classSimpleImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/*
* File: SimpleImage.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 08/11/06
* Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class SimpleImage {

var $image;
var $image_type;

function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
26 changes: 26 additions & 0 deletions engine/db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

function getDb(){
static $db = null;

if(is_null($db)){
$db = @mysqli_connect(HOST, USER, PASS, DB) or die('Could not connect: ' . mysqli_connect_error());
}
return $db;
}

function getAssocResult($sql){
$result = @mysqli_query(getDb(), $sql) or die(mysqli_error(getDb()));
$array_result = [];
while ($row = mysqli_fetch_assoc($result)) {
$array_result[] = $row;
}

return $array_result;
}


//update and delete
function executeQuery($sql){
return @mysqli_query(getDb(), $sql) or die(mysqli_error(getDb()));
}
56 changes: 56 additions & 0 deletions engine/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

function prepareVariables($page){
$params['layout'] = 'main';
switch ($page) {

case 'image':
$params['layout'] = 'gallery';
$params['image'] = getOneImage((int)$_GET['id']);
break;

case 'addLike':
$params['layout'] = 'gallery';
addLikes((int)$_GET['id']);
break;

case 'gallery':

if(isset($_POST['load'])){
$params['errors'] = uploadImage();
}

$params['layout'] = 'gallery';
$params['gallery'] = getGallery();
break;
case 'homework_3':
$params['layout'] = 'homework_3';
break;
}
return $params;
}

function render($page, $params){
return renderTemplate(LAYOUTS_DIR . $params['layout'], [
'content' => renderTemplate($page, $params),
'menu' => renderTemplate('menu', $params),
]
);
}

function renderTemplate($page, $params = []){
ob_start();

if (!is_null($params))
extract($params);

$fileName = TEMPLATES_DIR . $page . ".php";

if (file_exists($fileName)) {
include $fileName;
} else {
Die("Страницы {$fileName} не существует.");
}

return ob_get_clean();
}
48 changes: 48 additions & 0 deletions engine/gallery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
function getGallery(){
return getAssocResult("SELECT * FROM `images_info` ORDER BY views DESC");
}

function getOneImage($id){
return getAssocResult("SELECT * FROM `images_info` WHERE id = {$id}")[0];
}

function addLikes($id){
executeQuery("UPDATE `images_info` SET views = views + 1 WHERE id = {$id}");
}

function uploadImage(){
$path_big = IMG_BIG . $_FILES['image']['name'];
$path_small = IMG_SMALL . $_FILES['image']['name'];

$image_info = getimagesize($_FILES['image']['tmp_name']);

if ($image_info['mime'] != 'image/png' && $image_info['mime'] != 'image/gif' && $image_info['mime'] != 'image/jpeg') {
return "Можно загружать только jpg/png/gif/jpeg - файлы";
}

if ($_FILES['image']['size'] > 1024 * 5 * 1024) {
return "Размер файла не более 5 Мб";
}

$blacklist = ['.php', '.phtml', '.php3', '.php4'];
foreach ($blacklist as $item) {
if(preg_match("/$item\$/i", $_FILES['image']['name'])){
return "Загрузка php-файлов запрещена";
}
}

if (move_uploaded_file($_FILES['image']['tmp_name'], $path_big)) {

$filename = mysqli_real_escape_string(getDb(), $_FILES['image']['name']);
executeQuery("INSERT INTO `images_info` (`filename`) VALUES ('{$filename}')");

$image = new SimpleImage();
$image->load($path_big);
$image->resizeToWidth(250);
$image->save($path_small);
header("Location: /gallery");
} else {
return "Ошибка ресайза файла";
}
}
Empty file added engine/log.php
Empty file.
21 changes: 21 additions & 0 deletions engine/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

define('ROOT', $_SERVER['DOCUMENT_ROOT']);
// define('IMG_BIG', ROOT . '/images/gallery_img/big');

define('IMG', $_SERVER['DOCUMENT_ROOT'] . '/public/images/gallery_img/big/');

// include ROOT . "/config/config.php";


$db = mysqli_connect('localhost', 'root', 'root', 'images');
$res = mysqli_query($db, "SELECT * FROM `images_info`");

if ($res->num_rows == 0) {
echo "Таблица пустая. Заполнение данными об изображениях";
//INSERT INTO `images_info`(`filename`) VALUES
$images = array_splice(scandir(IMG), 2);
mysqli_query($db, "INSERT INTO `images_info`(`filename`) VALUES ('" . implode("'),('", $images) . "')");
} else {
echo "Таблица заполнена";
}
71 changes: 71 additions & 0 deletions images.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
-- phpMyAdmin SQL Dump
-- version 5.0.2
-- https://www.phpmyadmin.net/
--
-- Хост: 127.0.0.1:3306
-- Время создания: Окт 18 2020 г., 06:02
-- Версия сервера: 8.0.19
-- Версия PHP: 7.2.29

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- База данных: `images`
--

-- --------------------------------------------------------

--
-- Структура таблицы `images_info`
--

CREATE TABLE `images_info` (
`id` int NOT NULL COMMENT 'Идентификатор изображения',
`filename` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'Название изображения',
`views` int NOT NULL DEFAULT '0' COMMENT 'Количество просмотров'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

--
-- Дамп данных таблицы `images_info`
--

INSERT INTO `images_info` (`id`, `filename`, `views`) VALUES
(9, 'n_70.jpg', 2),
(10, 'n_72.jpg', 3),
(11, 'n_73.jpg', 0),
(12, 'n_76.jpg', 0),
(13, 'n_82.jpg', 1),
(14, 'n_95.jpg', 0);

--
-- Индексы сохранённых таблиц
--

--
-- Индексы таблицы `images_info`
--
ALTER TABLE `images_info`
ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT для сохранённых таблиц
--

--
-- AUTO_INCREMENT для таблицы `images_info`
--
ALTER TABLE `images_info`
MODIFY `id` int NOT NULL AUTO_INCREMENT COMMENT 'Идентификатор изображения', AUTO_INCREMENT=18;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
6 changes: 6 additions & 0 deletions public/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php
Binary file added public/images/gallery_img/big/n_70.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/big/n_72.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/big/n_73.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/big/n_76.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/big/n_82.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/big/n_95.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/small/n_70.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/small/n_72.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/small/n_73.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/small/n_76.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/small/n_82.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/gallery_img/small/n_95.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

include $_SERVER['$DOCUMENT_ROOT'] . "../config/config.php";

$url_array = explode('/', $_SERVER['REQUEST_URI']);

if ($url_array[1] == '') {
$page = 'index';
} else {
$page = $url_array[1];
}

$params = prepareVariables($page);
echo render($page, $params);
11 changes: 11 additions & 0 deletions public/js/modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const block = document.querySelector('.gallery_wrapper');

function handleEvents() {
block.addEventListener('click', (e) => {
if (e.target.classList.contains('gallery_img')) {
e.target.classList.toggle('active');
e.path[1].children[2].classList.toggle('image-views_invisible');
}
});
}
handleEvents();
Loading