From d295687b720dfe86f4ef97f4fc5010645795977b Mon Sep 17 00:00:00 2001 From: Sean Leffler Date: Tue, 26 Jan 2021 16:12:25 -0800 Subject: [PATCH 1/3] Track .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8bd4a3a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +emotes \ No newline at end of file From a7fb7b8f6ade035d6da59a7389d2e5785ddd540f Mon Sep 17 00:00:00 2001 From: Sean Leffler Date: Tue, 26 Jan 2021 17:16:01 -0800 Subject: [PATCH 2/3] Super simple server-side search filtering --- emotes.php | 211 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 129 insertions(+), 82 deletions(-) diff --git a/emotes.php b/emotes.php index 91dec70..6d741a3 100644 --- a/emotes.php +++ b/emotes.php @@ -96,113 +96,160 @@ function toggleHidden(){ $old_y) - { - $thumb_w = $new_width; - $thumb_h = $old_y*($new_height/$old_x); - } + if($old_x > $old_y) + { + $thumb_w = $new_width; + $thumb_h = $old_y*($new_height/$old_x); + } - if($old_x < $old_y) - { - $thumb_w = $old_x*($new_width/$old_y); - $thumb_h = $new_height; - } + if($old_x < $old_y) + { + $thumb_w = $old_x*($new_width/$old_y); + $thumb_h = $new_height; + } - if($old_x == $old_y) - { - $thumb_w = $new_width; - $thumb_h = $new_height; - } + if($old_x == $old_y) + { + $thumb_w = $new_width; + $thumb_h = $new_height; + } - $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h); + $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h); - imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); + imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); - // New save location - $new_thumb_loc = $moveToDir . $image_name; + // New save location + $new_thumb_loc = $moveToDir . $image_name; - if($mime['mime']=='image/png') { - $result = imagepng($dst_img,$new_thumb_loc,8); - } - if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') { - $result = imagejpeg($dst_img,$new_thumb_loc,80); - } + if($mime['mime']=='image/png') { + $result = imagepng($dst_img,$new_thumb_loc,8); + } + if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') { + $result = imagejpeg($dst_img,$new_thumb_loc,80); + } - imagedestroy($dst_img); - imagedestroy($src_img); + imagedestroy($dst_img); + imagedestroy($src_img); - return $result; + return $result; } $files = glob("emotes/*.*"); usort($files, 'strnatcasecmp'); $prev = '?'; -echo 'total emotes: ' . count($files) . '
' . 'randoms: gun, shades, smug, stare, thumbsup (use !randemote)' . '
'; +echo 'total emotes: ' . count($files) . '
' . 'randoms: gun, shades, smug, stare, thumbsup (use !randemote)' . '

'; + +echo '
Search:
'; + +if (isset($_GET["search_query"]) && isset($_GET["form_submit"]) && $_GET["form_submit"] === "Search") { + $needle = filter_var($_GET["search_query"], FILTER_SANITIZE_STRING); -echo ''; -for ($i = 0; $i < count($files); $i++) { + for ($i = 0; $i < count($files); $i++) { $image = $files[$i]; - $next = strtoupper($image[7]); - $fname = str_replace("`Q", "?", pathinfo($image, PATHINFO_FILENAME)); - /*$imagesmall = "emotes/resized/" . pathinfo($image, PATHINFO_BASENAME); - if (pathinfo($image, PATHINFO_EXTENSION) == "jpg" || pathinfo($image, PATHINFO_EXTENSION) == "png"){ - if (!file_exists($imagesmall)){ - createThumbnail(pathinfo($image, PATHINFO_BASENAME), 300, 300, "emotes", "emotes/resized/"); - } + $next = strtoupper($image[7]); + $fname = str_replace("`Q", "?", pathinfo($image, PATHINFO_FILENAME)); + /*$imagesmall = "emotes/resized/" . pathinfo($image, PATHINFO_BASENAME); + if (pathinfo($image, PATHINFO_EXTENSION) == "jpg" || pathinfo($image, PATHINFO_EXTENSION) == "png"){ + if (!file_exists($imagesmall)){ + createThumbnail(pathinfo($image, PATHINFO_BASENAME), 300, 300, "emotes", "emotes/resized/"); + } + } + else{ + $imagesmall = $image; + }*/ + $hidden = substr($fname, -1) === "~"; + + if (is_numeric($next)){ + $next = "0-9"; } - else{ - $imagesmall = $image; - }*/ - $hidden = substr($fname, -1) === "~"; - - if (is_numeric($next)){ - $next = "0-9"; - } - if ($next != $prev){ - echo '

' . $next . '

'; - } - if (!$hidden){ - echo ''; - } - else { - echo ''; - } - $prev = $next; + if ($next != $prev){ + echo '

' . $next . '

'; + } + if (!$hidden){ + echo ''; + } + else { + echo ''; + } + $prev = $next; + } } ?> From d825f9129c61a41e803d2d62394d536782829a7b Mon Sep 17 00:00:00 2001 From: Sean Leffler Date: Tue, 26 Jan 2021 17:24:13 -0800 Subject: [PATCH 3/3] Add commenting to explain new code. --- emotes.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/emotes.php b/emotes.php index 6d741a3..52f32b2 100644 --- a/emotes.php +++ b/emotes.php @@ -154,15 +154,23 @@ function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDi $prev = '?'; echo 'total emotes: ' . count($files) . '
' . 'randoms: gun, shades, smug, stare, thumbsup (use !randemote)' . '

'; +// Generate a form for the search box. This consists of a text box (which should contain the previous search term, if any), +// a "Search" button which submits the form and reloads the page, and a "Clear" button which returns to viewing all emotes. echo '
Search:
'; +// If the search query string is set, filter $files for filenames which have substrings matching the search query, and +// then return those thumbnails only. if (isset($_GET["search_query"]) && isset($_GET["form_submit"]) && $_GET["form_submit"] === "Search") { $needle = filter_var($_GET["search_query"], FILTER_SANITIZE_STRING); + // If the search query is an empty string, we don't want to show anything; otherwise we could accidentally show all emotes, + // as the empty string is a substring of all strings, and that would be slow. $filtered = []; if ($needle !== "") { $filtered = array_values(array_filter($files, function($image) use ($needle) {