Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
rb:mugshots [2016/10/04 14:00]
127.0.0.1 external edit
rb:mugshots [2016/12/04 20:15] (current)
madoverlord
Line 1: Line 1:
-<​PHP>​ +Test page - no longer active.
- +
-// paths +
- +
-$website_path = '/​img/​riding-bean';​ +
-$server_path = '/​opt/​bitnami/​apache2/​htdocs/​img/​riding-bean';​ +
-//​$server_path = '/​Applications/​MAMP/​htdocs/​img/​riding-bean';​ +
- +
-// image dimensions +
- +
-$width = 600; +
-$height = 750; +
-$ratio = 0.80; +
- +
-$secret_code = '';​ +
- +
-if ($_SERVER['​REQUEST_METHOD'​] === '​POST'​) { +
- +
- $result = upload_rb_image($server_path,'​original'​);​ +
-  +
- $secret_code = $_POST['​person'​];​ +
-  +
- if ($result[0]) { +
-  +
- // before we do anything, clean out the temp directory of files more than 2 hours old +
-  +
- foreach (scandir("​$server_path/​temp"​) as $f) { +
- if ((substr($f,​-4) === '​.png'​) || (substr($f,​-4) === '​.jpg'​) || (substr($f,​-5) === '​.jpeg'​)) { +
- $fname = "​$server_path/​temp/​$f";​ +
- if (time()-filemtime($fname) > 2 * 3600) { +
- unlink($fname);​ +
-+
-+
-+
- +
- // tell user result +
-  +
- echo "<​center><​span style='​color:​green'>​$result[1]</​span><​br /><br />";​ +
- +
- // saving or viewing? +
- +
- $save = (count($result) > 3); +
-  +
- $fname = $result[2];​ +
-  +
- if ($save) { +
- $ftype = $result[3];​ +
- $x = $result[4];​ +
- $y = $result[5];​ +
- $sy = intval($y / ($x / $width)); +
-  +
- // get the image +
-  +
- $original = "​$server_path/​original/​$fname.$ftype";​ +
- $image = new Imagick($original);​ +
- +
- // orient it properly +
-  +
- $orientation = $image->​getImageOrientation();​  +
- +
-     switch($orientation) {  +
-         case imagick::​ORIENTATION_BOTTOMRIGHT:​  +
-             $image->​rotateimage("#​000",​ 180); // rotate 180 degrees  +
-         break;  +
- +
-         case imagick::​ORIENTATION_RIGHTTOP:​  +
-             $image->​rotateimage("#​000",​ 90); // rotate 90 degrees CW  +
-         break;  +
- +
-         case imagick::​ORIENTATION_LEFTBOTTOM:​  +
-             $image->​rotateimage("#​000",​ -90); // rotate 90 degrees CCW  +
-         break;  +
-     }  +
- +
- // crop to desired aspect ratio +
- +
- if (($x / $y) > $ratio) { // wide image, crop sides +
- $nx = intval($y * $ratio); +
- $image->​cropImage($nx,​$y,​intval(($x-$nx)/​2),​0);​ +
- } else { //​ tall image, crop top/​bottom +
- $ny = intval($x / $ratio); +
- $image->​cropImage($x,​$ny,​0,​intval(($y-$ny)/​2));​ +
-+
-  +
- // keep a copy of the image +
- +
- $image->​setImageCompressionQuality(100);​ +
- $image->​stripImage();​ +
- +
- // if image is more than 2x needed resolution, scale it down +
- +
- if ($image->​getImageWidth() > ($width+$width)) { +
- $image->​scaleImage($width+$width,​$height+$height,​false);​ +
-+
- +
- // delete the original, replace with the (possibly scaled) version in jpg +
- +
- unlink($original);​ +
- $image->​writeImage("​$server_path/​original/​$fname.jpg"​);​ +
- +
- } else { // grab the saved image so we can view it +
- $image = new Imagick("​$server_path/​original/​$fname.jpg"​);​ +
- $image->​setImageCompressionQuality(100);​ +
- $image->​stripImage();​ +
-+
-  +
- // scale original to display size +
- +
- $image->​scaleImage($width,​$height,​false);​ +
-  +
- // create version that shows hilighted area +
-  +
- $mask = new Imagick();​ +
- $mask->​newImage($width,​$height,'#​666666'​);​ +
- $circ = new ImagickDraw();​ +
- $circ->​setFillColor('#​FFFFFF'​);​ +
- $circ->​setStrokeColor('#​AAAAAA'​);​ +
- $circ->​setStrokeWidth(1);​ +
- $circ->​circle(intval($width/​2),​intval($height/​2),​0,​intval($height/​2));​ +
- $mask->​drawImage($circ);​ +
-  +
- $image->​compositeImage($mask,​ Imagick::​COMPOSITE_MULTIPLY,​ 0, 0);  +
- $image->​writeImage("​$server_path/​temp/​$fname.jpg"​);​ +
-  +
- $dt = date('​Uu'​);​ // hack to disable caches +
- +
- echo "Your mugshot, cropped with focus area higlighted:<​br /><​img src='​$website_path/​temp/​$fname.jpg?​d=$dt'​ width=$width height=$height /><br /><br />";​ +
-  +
- echo "You can re-upload a new version if you are not satisfied with the composition.</​center><​br /><br />";​ +
-  +
- } else { +
- echo "<​center><​span style='​color:​red'>​Ooops! $result[1]</​span></​center><​br /><br />";​ +
-+
-  +
-+
- +
-// page form +
- +
-echo <<<​EOT +
- +
-<style media='​screen'​ type='​text/​css'>​ +
- form.rb div { display:​inline-block; padding-bottom:​ 5px; } +
- form.rb label { display:​inline-block;​ height: 45px; vertical-align:​ middle; } +
- form.rb label > input { visibility: hidden; position: absolute; } +
- form.rb label > input + img { cursor:​pointer;​ } +
- form.rb label > input:​checked + img {outline: 4px solid red; outline-offset:​ -4px; } +
- form.rb div.hide { height: 0px;​width:​0px;​ overflow:​hidden;​ } +
-</​style>​ +
- +
-<​h3>​Upload your Mugshot</​h3>​ +
-<form class='​rb'​ action=''​ method='​post'​ enctype='​multipart/​form-data'>​ +
-<img src='/​img/​riding-bean/​example/​mugshot-310.jpg'​ width=310 height=390>​ +
-<br /><br /> +
-Enter your 16-character secret code +
-<​div>​ +
-<input style='​width:​16em'​ type='​text'​ name='​person'​ value='​$secret_code'​ size='​20'​ maxlength='​20'​ /> +
-</​div>​ +
-. Then either +
-<​div>​ +
-<input type='​button'​ value='​select a file' name='​clicker'​ onclick='​document.getElementById("​upfile"​).click();'​ /> +
-</​div>​ +
-or a +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​1"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-1.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​2"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-2.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​3"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-3.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​4"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-4.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​5"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-5.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​6"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-6.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​7"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-7.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​8"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-8.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​9"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-9.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​10"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-10.png">​ +
-</​label>​ +
-<​label>​ +
-  <input type="​radio"​ name="​stock"​ value="​11"​ /> +
-  <img src="/​img/​riding-bean/​example/​Thumb/​Mugshot-11.png">​ +
-</​label>​ +
-stock photo and +
-<​div>​ +
-<input type='​submit'​ value='​upload'​ name='​submit'​ /> +
-</​div>​ +
-your mugshot, or +
-<​div>​ +
-<input type='​submit'​ value='​view'​ name='​view'​ /> +
-</​div>​ +
-your current mugshot (if you've already uploaded one). +
-<div class='​hide'>​ +
-<input id='​upfile'​ type='​file'​ name='​fileToUpload'​ id='​fileToUpload'​ size='​5'​ /> +
-</​div>​ +
-</​form>​ +
- +
-EOT; +
- +
-// upload validation +
- +
-function upload_rb_image($path) { +
- +
- $view = isset($_POST['​view'​]);​  +
- +
- if(!isset($_POST['​submit'​]) && (!$view)) { +
- return(array(false,'​Invalid form request type.'​));​ +
- }  +
-  +
- // validate secret code +
- +
- $secret_code = $_POST['​person'​];​ +
-  +
- // fix common mistakes +
- +
- $secret_code = str_replace('​[','',​$secret_code);​ +
- $secret_code = str_replace('​]','',​$secret_code);​ +
- $secret_code = str_replace('​1','​I',​$secret_code);​ +
- $secret_code = str_replace('​0','​O',​$secret_code);​ +
- +
- if ((strlen($secret_code) !== 16) || (strspn($secret_code,'​23456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'​) !== 16)) { +
- return(array(false,"​Your secret code [$secret_code] was not properly formed."​));​ +
-+
-  +
- $db = new PDO("​mysql:​host=localhost;​dbname=riding_bean","​readonly",""​);​ +
- $table = "​secret_codes";​ +
- $stmt = $db->​query("​SELECT COUNT(*) FROM secret_codes WHERE code='​$secret_code'"​);​ +
-  +
- if ($stmt->​fetchColumn() < 1) { +
- return(array(false,'​Unknown secret code; please check and re-enter.'​));​ +
-+
-  +
- $stock = $_POST['​stock'​];​ +
- echo '​['​ & $_FILES['​fileToUpload'​]['​tmp_name'​] & '​]';​ +
- $upload = $_FILES['​fileToUpload'​]['​tmp_name'​];​ +
- +
- if ($view) { +
-  +
- // handle view request +
-  +
- if (file_exists("​$path/​original/​$secret_code.jpg"​)) { +
- return(array(true,"​Current mugshot for backer with secret code [$secret_code]",​$secret_code));​ +
- } else { +
- return(array(false,"​No current mugshot for backer with secret code [$secret_code]"​));​ +
-+
-  +
- } else { +
-  +
- // Check if image file is formatted correctly +
-  +
- if ($upload === ''​) { +
- if (isset($stock)) { +
- $tmp_name = "​$path/​example/​Stock/​Mugshot-$stock.png";​ +
-+
- if ( ($upload === ''​) && (!file_exists($tmp_name)) ) { +
- return(array(false,'​You forgot to choose a file or select a stock image.'​));​ +
-+
- } else { +
- $tmp_name = $upload; +
-+
-  +
- $info = getimagesize($tmp_name);​ +
-  +
- if($info === false) { +
- return(array(false,'​The file you submitted is not an image.'​));​ +
-+
-  +
- //echo print_r($info,​true) . "​\n\n";​ +
-  +
- $x = $info[0]; +
- $y = $info[1]; +
-  +
- if ($info[2] == IMAGETYPE_JPEG) { +
- $imageFileType = '​jpg';​ +
- } elseif ($info[2] == IMAGETYPE_PNG) { +
- $imageFileType = '​png';​ +
- } else { +
- $imageFileType = '';​ +
-+
-  +
- //echo IMAGETYPE_JPEG . "​\n\n";​ +
-  +
- //echo $imageFileType . "​\n\n";​ +
-  +
- // Allow certain file formats +
-  +
- if ($imageFileType === ''​) { +
- return(array(false,'​Invalid image type; only jpg, jpeg, & png files are allowed.'​));​ +
-+
-  +
- // Check file size +
-  +
- if ($_FILES['​fileToUpload'​]['​size'​] > 6291456+100000) { +
- return(array(false,'​Image too large; 6M limit.'​));​ +
-+
-  +
- // minimum size: 400h x 500v pixels +
-  +
- if (($y < 750) || ($x < 600)) { +
- return(array(false,'​Image must be at least 600h x 750v pixels; yours is ' . $x . 'h x ' . $y . '​v.'​));​ +
-+
-  +
- $target_file = "​$path/​original/​$secret_code.$imageFileType";​ +
-  +
- if ($upload === ''​ && copy($tmp_name,​ $target_file)) { +
- return(array(true,'​Mugshot for backer with secret code ['. $secret_code . '] has been added to MOST-WANTED list.',​$secret_code,​$imageFileType,​$x,​$y));​  +
- } elseif (move_uploaded_file($tmp_name,​ $target_file)) { +
- return(array(true,'​Mugshot for backer with secret code ['. $secret_code . '] has been added to MOST-WANTED list.',​$secret_code,​$imageFileType,​$x,​$y));​ +
- } else { +
- $err = print_r(error_get_last(),​$true);​ +
- return(array(false,'​Could not save your mugshot. Please try again. If the error persists, please let trebor@animeigo.com know.\n\n' ​$err)); +
-+
-  +
-+
- +
-+
- +
-</​PHP>​ +
- +
- +
- +
-~~NOCACHE~~+