Created
October 25, 2017 16:44
-
-
Save heiswayi/5ba3b2c5bbcfd51a02e2d091e2af8ebb to your computer and use it in GitHub Desktop.
Internal Image Hosting Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Configuration | |
$title = 'Internal Image Hosting Script'; | |
$filedir = 'up'; // uploaded image dir | |
$maxsize = 5242880; //max size in bytes | |
$allowedExts = array('png', 'jpg', 'jpeg', 'gif'); | |
$allowedMime = array('image/png', 'image/jpeg', 'image/pjpeg', 'image/gif'); | |
$baseurl = $_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/'.$filedir; | |
function compressImage($source_url, $destination_url, $quality) { | |
$info = getimagesize($source_url); | |
if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); | |
elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url); | |
elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); | |
//save file | |
imagejpeg($image, $destination_url, $quality); | |
//return destination file | |
return $destination_url; | |
} | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title><?php print $title; ?></title> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css"> | |
<style> | |
body { padding: 20px; font-size: 16px; font-family: sans-serif; } | |
#upload { margin: 20px auto; max-width: 900px; border: 1px solid #ddd; padding: 20px; } | |
#info { font-size: 13px; margin-top: 20px; } | |
#image { margin: 20px auto; max-width: 900px; padding: 20px; border: 1px dashed #ddd; } | |
#image input[type="text"] { background: #f3f3f3; border: 1px solid #ddd; padding: 5px; width: 80%; } | |
#image img { margin: 0 auto; display: block; max-width: 100%; max-height: 100%; } | |
h1 { margin-top: 0; display: block; border-bottom: 1px solid #000; padding-bottom: 5px; } | |
</style> | |
<!--[if lt IE 9]> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<div id="upload"> | |
<h1><?php print $title; ?></h1> | |
<form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF']; ?>" method="POST"> | |
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" /> | |
<strong>Choose a image file to upload:</strong><br><br> | |
<input size="62" name="file" type="file" accept="image/*" /> | |
<input type="submit" value="Upload File" /> | |
</form> | |
<div id="info"> | |
Max. filesize: <strong>5MB</strong><br> | |
Supported image formats: <strong>PNG, JPG, GIF</strong><br> | |
Reduced image quality: <strong>25%</strong><br><br> | |
<a href="admin.php">Manage uploaded image files</a> → | |
</div> | |
</div> | |
<div id="image"> | |
<div name="image"> | |
<?php | |
if($_SERVER['REQUEST_METHOD'] == 'POST') { | |
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); | |
if ((in_array($_FILES['file']['type'], $allowedMime)) | |
&& (in_array(strtolower($ext), $allowedExts)) | |
&& (@getimagesize($_FILES['file']['tmp_name']) !== false) | |
&& ($_FILES['file']['size'] <= $maxsize)) { | |
$md5 = substr(md5_file($_FILES['file']['tmp_name']), 0, 7); | |
$newname = time().'.'.$ext; | |
compressImage($_FILES['file']['tmp_name'], $_FILES['file']['tmp_name'], 75); | |
move_uploaded_file($_FILES['file']['tmp_name'], $filedir.'/'.$newname); | |
$baseurl = $_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/'.$filedir; | |
$imgurl = 'http://'.$baseurl.'/'.$newname; | |
print 'Your Image URL: '; | |
print '<input type="text" value="'.$imgurl.'" onfocus="this.select();" onmouseup="return false;"><br /><br />'; | |
print '<a href="'.$imgurl.'"><img src="'.$imgurl.'" /></a><br />'; | |
} | |
else { | |
print '<span style="color:red">Oops, something went wrong!</span>'; | |
} | |
} | |
?> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment