From 78c93db0fa5a337c49c472ecf71e77fc72db974b Mon Sep 17 00:00:00 2001 From: Vlad Utkin Date: Thu, 29 Jun 2017 12:42:30 +0300 Subject: [PATCH] add files --- database.php | 83 +++++++++++++++++++++++++++++++++++++ image_display.php | 50 +++++++++++++++++++++++ images.php | 78 +++++++++++++++++++++++++++++++++++ index.php | 101 ++++++++++++++++++++++++++++++++++++++++++++++ styles.css | 55 +++++++++++++++++++++++++ 5 files changed, 367 insertions(+) create mode 100644 database.php create mode 100644 image_display.php create mode 100644 images.php create mode 100644 index.php create mode 100644 styles.css diff --git a/database.php b/database.php new file mode 100644 index 0000000..8ee4cad --- /dev/null +++ b/database.php @@ -0,0 +1,83 @@ +link = new PDO("mysql:host=".$host.";dbname=".$db, $user, $pass); + $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + } + catch (PDOException $e){ + echo "Error: Unable to connect to MySQL: ". $e->getMessage(); + die; + } + + $this->InitializeImageTable(); + } + + public function __destruct() { + $this->link = null; + } + + public function UploadImage($imageName, $imageFP) { + $sql = $this->link->prepare("INSERT INTO images (name, image) VALUES (:name, :image);"); + $sql->bindParam(":name", $imageName); + $sql->bindParam(":image", $imageFP, PDO::PARAM_LOB); + + $sql->execute(); + + return $this->link->lastInsertId(); + } + + public function GetAllImages() { + $sql = $this->link->prepare("SELECT * FROM images;"); + $sql->execute(); + + $results = $sql->fetchAll(PDO::FETCH_OBJ); + + return $results; + } + + public function FindImage($id) { + $sql = $this->link->prepare("SELECT * FROM images WHERE id = :id;"); + $sql->bindParam(":id", $id, PDO::PARAM_INT); + $sql->execute(); + + $result = $sql->fetch(PDO::FETCH_OBJ); + return $result; + } + + private function InitializeImageTable() { + // Check to see if the table needs to be created + $results = $this->link->query("SHOW TABLES LIKE 'images';"); + if ($results == TRUE && $results->rowCount() > 0) { + return; + } + + // create table + $sql = "CREATE TABLE images ( + id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL DEFAULT '', + image LONGBLOB NOT NULL + );"; + + if ($this->link->query($sql) != TRUE) { + die("Error creating image table: " . $this->link->error); + } + } + } +?> diff --git a/image_display.php b/image_display.php new file mode 100644 index 0000000..9fd27b1 --- /dev/null +++ b/image_display.php @@ -0,0 +1,50 @@ + +image; + $srcSize = getImageSizeFromString($srcImage); + $srcWidth = $srcSize[0]; + $srcHeight = $srcSize[1]; + $srcType = $srcSize[2]; + $srcMime = $srcSize['mime']; + $srcImageResource = imageCreateFromString($srcImage); + + // set the header for the image + header("Content-type: ".$srcMime); + + if ((isset($_GET['width']) && is_numeric($_GET['width'])) === FALSE) { + // no width requested - just return the source + echo $srcImage; + exit; + } + + // resize/resample the image to the requested size + $destWidth = $_GET['width']; + $destHeight = $destWidth * $srcSize[1] / $srcSize[0]; + + $destImageResource = imageCreateTrueColor($destWidth, $destHeight); + imagealphablending($destImageResource, false); + imagesavealpha($destImageResource, true); + imageCopyResampled($destImageResource, $srcImageResource, 0,0,0,0, $destWidth, $destHeight, $srcWidth, $srcHeight); + + // export the image + switch ($srcType) { + case IMAGETYPE_JPEG: + imageJPEG($destImageResource); + break; + case IMAGETYPE_PNG: + imagePNG($destImageResource); + break; + default: + imageJPEG($destImageResource); + break; + } + + imageDestroy($destImageResource); +?> diff --git a/images.php b/images.php new file mode 100644 index 0000000..e3839ed --- /dev/null +++ b/images.php @@ -0,0 +1,78 @@ + +UploadImage($_FILES['imageToUpload']['name'], $imagefp); + header("Location: /"); + exit; + } + else { // not an image + echo ''; + exit; + } + } + else { // file too large + echo ''; + exit; + } + } + else { // upload failed + echo ''; + exit; + } + } + else { + echo ''; + exit; + } + } + + public static function GetImages() { + $database = new Database(); + $images = $database->GetAllImages(); + return $images; + } + + public static function GetImage($id) { + $database = new Database(); + $image = $database->FindImage($id); + return $image; + } + } +?> diff --git a/index.php b/index.php new file mode 100644 index 0000000..9fc6942 --- /dev/null +++ b/index.php @@ -0,0 +1,101 @@ + + + + ImageNet + + + + + + + + + + + + + +
+
+

My Images

+
+
+ + + +
+
+ +
+ id."' target='_blank'>"; + echo ""; + echo ""; + ?> +
+ +
+
+ + + + diff --git a/styles.css b/styles.css new file mode 100644 index 0000000..6a1a0da --- /dev/null +++ b/styles.css @@ -0,0 +1,55 @@ +/* Custom background color for the nav-bar element. */ +.navbar { + background-color: #d9edf7; /*matches bg-info*/ + background-image: none; + background-repeat: no-repeat; + filter: none; +} + +/* Support for custom-looking File Selection button. */ +.btn-file { + position: relative; + overflow: hidden; +} +.btn-file input[type=file] { + position: absolute; + top: 0; + right: 0; + min-width: 100%; + min-height: 100%; + font-size: 100px; + text-align: right; + filter: alpha(opacity=0); + opacity: 0; + outline: none; + background: white; + cursor: inherit; + display: block; +} + +/* Styling/framing for the image elements. */ +img { + width: 192px; + background-color: white; + padding: 5px 5px 30px 5px; + cursor: pointer; +} + +/* Custom queries for force line breaks depending on the resolution. */ +@media (min-width: 1200px) { +.col-lg-2:nth-child(6n+1) { + clear: both; + } +} + +@media (min-width: 992pxpx) and (max-width: 1200px) { + .col-md-4:nth-child(3n+1) { + clear: both; + } +} + +@media (min-width: 768px) and (max-width: 992px) { + .col-sm-6:nth-child(2n+1) { + clear: both; + } +}