Browse Source

Initial commit

master
jonw-repo 1 week ago
commit
8fe7f5f6ce
4 changed files with 186 additions and 0 deletions
  1. +42
    -0
      README.MD
  2. BIN
      image_shot_logo.png
  3. +18
    -0
      index.html
  4. +126
    -0
      upload.php

+ 42
- 0
README.MD View File

@@ -0,0 +1,42 @@
# Image Shot

## Description

Image Shot is quick, dirty, and simple internet image hosting script. It is **not a gallery** which means you won't upload an image and then be able to show it to others in your Image Shot gallery. Image Shot simply takes an image from you, resizes it in as may ways as you want, and then spits back both an HTML and a Markdown link for you to copy and paste somewhere else such as a blog post.

## Installation

You need a web server running PHP with the ImageMagick extensions installed.

1. Copy the project files into a web accessible directory.
1. Create a directory to hold the images. Make sure it is web server read/writeable.

## Configuration

1. Edit the upload.php file:
1. Change the $target_dir to whatever directory you created in the step above.
1. Change the $url to the actual web accessible URL for the location you put this code
1. If desired, change the values in $img_sizes. This will determine how many images you get and what sizes
1. Create a file named salt outside your web accessible directories. This code assumes it is two directories up (line 12). Put the password you'd like to use in this file.

## Use

1. Visit your Image Shot installation.
1. Browse for an image
1. Enter the salt you chose in the Configuration step
1. Hit Upload
1. Profit

## Other ideas

My installation also sends the image links to me in Slack. If you want to do something like that, add the necessary code around the print statements in lines 72-ish.

## TODO

Add Credits (some code was borrowed in the true FOSS tradition)
Make resizing smoother






BIN
image_shot_logo.png View File

Before After
Width: 644  |  Height: 96  |  Size: 25 KiB

+ 18
- 0
index.html View File

@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Image Shot!</title>
</head>

<body style="font-size:40px;font-family:helvetica,arial">

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload"><p/>
<input type="password" name="post_salt" id="post_salt"><p />
<input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>


+ 126
- 0
upload.php View File

@@ -0,0 +1,126 @@
<?php
$target_dir = "uploads";
$url = "https://yourdomain.com";
$target_file = $target_dir."/".basename($_FILES["fileToUpload"]["name"]);
$img_sizes=array(400,600,800);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Only run if it's post data
if(isset($_POST["submit"])) {

// Authenticate-ish
$images_salt=trim(file_get_contents("../../salt"));
$post_salt=trim($_POST["post_salt"]);
if ($images_salt != $post_salt ){
die ("No");
}

// Make sure it is an image
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check == false) {
echo "No";
die();
}
// Check file size (10 MB)
if ($_FILES["fileToUpload"]["size"] > 10000000) {
echo "No.";
die();
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
echo "No.";
die();
}
// if everything is ok proceed
$img=new Imagick($_FILES['fileToUpload']['tmp_name']);
$head="<!DOCTYPE html>
<html>
<head>
<title>Image Shot!</title>
<meta name='viewport' content='width=device-width, initial-scale=1' />
</head>
<body>";
$foot="</body>
</html>";

print $head;

// Do the resizing routine
$img_width=$img->getImageWidth();

// Unique filename
$salt=date('U');
$target_filename=md5($new_image_name.$salt

for ( $i=0; $i<count($img_sizes); $i++ ) {
if ($img_width > $img_sizes[$i] ){
$img->adaptiveResizeImage($img_sizes[$i],$img_sizes[$i],TRUE);
}
else { next; }

// Tmp file name
$new_image_name=$_FILES['fileToUpload']['tmp_name']."_$i";

// Strip exif data
$orientation = $img->getImageOrientation();
$img->stripImage();
$img=autorotate($img);
// Save it
$img->writeImage($new_image_name);

if (copy($new_image_name, "$target_dir/$target_filename-".$img_sizes[$i].".$imageFileType")) {
print "<img src='$url/$target_dir/$target_filename-".$img_sizes[$i].".$imageFileType' /><p />\n";
print "<input type=text size=100 value='<img src=\"$url/$target_dir/$target_filename-".$img_sizes[$i].".$imageFileType\" />'/><br />\n";
print "<input type=text size=100 value='![]($url/$target_dir/$target_filename-".$img_sizes[$i].".$imageFileType)'/><p />\n";
} else {
echo "Blerch. Something broke.<br />";
}
unlink ($new_image_name);
imagedestroy($img);
$img = new Imagick($_FILES['fileToUpload']['tmp_name']);
}
unlink ($_FILES['fileToUpload']['tmp_name']);
print $foot;
}

// Orient it correctly
function autorotate(Imagick $img)
{
switch ($img->getImageOrientation()) {
case Imagick::ORIENTATION_TOPLEFT:
break;
case Imagick::ORIENTATION_TOPRIGHT:
$img->flopImage();
break;
case Imagick::ORIENTATION_BOTTOMRIGHT:
$img->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_BOTTOMLEFT:
$img->flopImage();
$img->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_LEFTTOP:
$img->flopImage();
$img->rotateImage("#000", -90);
break;
case Imagick::ORIENTATION_RIGHTTOP:
$img->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_RIGHTBOTTOM:
$img->flopImage();
$img->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_LEFTBOTTOM:
$img->rotateImage("#000", -90);
break;
default: // Invalid orientation
break;
}
$img->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
return $img;
}

?>


Loading…
Cancel
Save