x to y of z search results

posted in code with 0 comments

I’m the dozen hundredth person to code this solution. I am always rebuilding my site on an ongoing basis-see a better way, code a better way. Today I’ve worked on the search results template. I want to be able to sort results, and have a counter for the results in the manner of “results x to y of z”:

It is pretty easy. Three numbers are needed:

  1. Current page number.
  2. Number of posts displayed per page.
  3. Total count of search results.

Calculate the top count-range number first (y in ‘x to y of z’):

$page_number * $posts_per_page;

Then the bottom count-range number:

$count_low  = ($count_high - $posts_per_page) + 1;

And finally round the top count-range if it is over the total:

$count_high = ($count_high > $total_results) ? $total_results : $count_high;

Here is the completed function. You can just drop this into your functions.php as-is.

function search_results_count($page_num, $total_results) {
    $page_num = ($page_num == 0) ? 1 : $page_num;
    $posts_per_page = get_option('posts_per_page');
    $count_high = $page_num * $posts_per_page;
    $count_low  = ($count_high - $posts_per_page) + 1;
    $count_high = ($count_high > $total_results) ? $total_results : $count_high;
    echo 'Results ' . $count_low . ' to ' . $count_high . ' of ' . $total_results;
}

Execute it with this line in search.php:

<?php global $wp_query; search_results_count(get_query_var('paged'), $wp_query->found_posts); ?>

It will output this string (for example):

Results 1 to 10 of 158

by Mark -

Went to Guardians of the Galaxy with friends

posted in friends with 0 comments

Galway geeks :)

Guardians of the Galaxy was brilliant and funny. I organized the group for Mike, who has been ill lately, but he bowed out at the last moment. At that point everyone has made their bookings so it was too late to completely cancel.

Clockwise from bottom left: Mary, Derek, not sure, Katie, not sure, Duncan, Ciaran, Oisin, Alanna, Patrick, Linda, Kieran.

by Mark -

Croydon, London in 2005

posted in photo with 0 comments

Tilt-shift of Wellesley Road, Croydon, from the Jurys Inn hotel

This is one of my oldest photographs. I took it while at a World of Warcraft guild meetup with friends (Gabi) in London on the last weekend of July 2005. I just put this up now because I wanted to see what a non-Instagram thumbnail looked like. By the time a photograph gets from my phone to the blog it has been compressed a few times:

  1. On the phone when it is taken.
  2. When it cropped and uploaded to Instagram.
  3. When it is shrunk for thumbnail upload.

All those images are soft and grainy at this size, because of compression and crappy iPhone lens.

by Mark -

Prep version 5

posted in code with 0 comments

Killer test photograph

This script hasn’t changed too much since the last posted revision, but here we are. I had to make certain changes to the script after yesterday’s thumbnail refresh, which was a good as time as any to post revisions.

This script does:

  1. Take in $folder_name and $images as arguments $2 through to $∞
  2. mkdir $folder_name, copy $images into the folder.
  3. Rename $images in an incrementing numerical sequence.
  4. Create thumbnail (well, 1000 pixel width) images, compressed for fast display.
  5. Shrink, but do not compress, the original images to 1700 pixels wide.
  6. Move $folder_name into the relevant Dropbox folder.
  7. Generate HTML code for the images.
  8. Put the HTML code into my clipboard.

The script presented here has between tweaked for Windows (Cygwin), can run on Linux with minimal effort, and is probably easily adjusted for OS X.

#!/bin/bash
# mark@bhalash.com

# Path to public Dropbox folder.
dropbox="$HOME/Dropbox/Public/content"
# Dropbox UUID.
uuid="4144919"
# Name of thumbnails folder.
medium_folder="m"
# Size and quality of full image.
large_size=1700
large_quality=100
# Size and quality of medium image.
medium_size=1000
medium_quality=70
# Thumbnail folder.
temp="/tmp/prep"

add_line() {
    # Add a line to the eventual HTML code. 
    # $1 = Dropbox UUID
    # $2 = Unique folder name.
    # $3 = File name.
    # $4 = Temp folder. 
    echo "<a title=\"CHANGE ME\" href=\"https://dl.dropboxusercontent.com/u/$1/content/\$2/$3\">\
    <img src=\"https://dl.dropboxusercontent.com/u/$1/content/$2/m/$3\" alt=\"CHANGE ME\" /></a>" >> $4
}

resize_image() {
    # First size, second file. The \> prevents the file from being upscaled. 
    # $1 = Desired x size.
    # $2 = file to be resized.
    mogrify -quality "$1" -format jpg -resize "$2"x\> "$3"
}

if [ ! -d $1 ]; then
    # Create the directory.
    mkdir -p $1/$medium_folder
fi

if [ -d $dropbox/$1 ]; then
    # So we can overwrite the existing directory.
    rm -r $dropbox/$1
fi

src=$1
shift
count=1

for n in "$@"; do
    if [ -e "$1" ]; then
        cp "$1" $src/$count.jpg
        cp "$1" $src/$medium_folder/$count.jpg
        let count++
        shift
    fi
done

cd $src

if [ $(ls -1 *.jpg 2gt; /dev/null | wc -l) == 0 ]; then
    # Exit out if we didn't copy any files.
    echo "No images selected!"
    rm -r $(pwd)
    exit 1
fi

for n in *.jpg; do
    # Resize the 'master' and thumbnail images.
    resize_image $large_quality $large_size $n
    resize_image $medium_quality $medium_size "$medium_folder"/$n
    # Add a line to the final HTML.
    add_line $uuid $src $n $temp
done

# putclip and getclip are special Cygwin programs that respectively copy to and
# from the Windows clipboard
# cat $temp | xclip -sel clip
cat $temp | putclip
rm $temp
mv $(pwd) $dropbox

exit 0

by Mark -

Changing thumbnails

posted in the website with 0 comments

I don’t strip either EXIF or IPTC metadata from photographs I upload. It’s part of my “for-posterity” goal with the blog:

  1. Photo is linked to a time and place via metadata.
  2. Photo folder is linked to a blog post ID by name.
  3. Blog post should give important context for the photograph.

If both parts-post and images-stay together, then you will always have some useful information about the photograph. Since ~2011 an increasing proportion of images on the site has had GPS information too. I haven’t run into any privacy concerns, given that if an image could be considered that private, I simply wouldn’t upload it. But this chain of thought did encourage me to dig around with analytic information yesterday. I drew up Google Analytics, asked a few readers, and made some wild and crazy-haired guesses. I came up with:

  1. Readership here is low (~= 50 hits per day). That’s fine, since I don’t push the site.
  2. Most users come here because of specific forum links or Google searches (my post on Spacing Guild heighliners is really popular).
  3. People take what they need and leave.

But practically, I am still concerned about bandwidth usage both because I chose to host my images through Dropbox, and because of future portability.

For Basic accounts, the total amount of traffic that all of your links together can generate without getting banned is 20 GB per day.Dropbox’s policy.

Emphasis is theirs. I’ve been stung for that twice: Once when the heighliner post was linked on a big science fiction forum. The other occasion was when my World of Warcraft art was linked on Reddit. On top of this, I made some changes to the site’s theme in order to look better at larger screen sizes.

I’ve made some test changes: I’ve recompressed all of the thumbnail images on the website, and reduced their quality. The original, full size remains unaffected. Performance should be better without too much of a quality loss. If I like the effect I will generate a new set of thumbnails at 1000 pixel width and open up the content column width.

find . -type d -name 'm' -exec find {} -type f -iname "*.jpg" \; | xargs -n 1 mogrify -verbose -quality 70

EDIT: YOLO

#!/bin/bash

for dir in $(ls -1d *); do 
    if [[ $(ls "$dir" | grep ".jpg" | wc -l) -gt 0 ]]; then 
        if [[ -d "$dir/m" ]]; then 
            cd "$dir" cp *.jpg m 
            mogrify -quality 70 -resize 1000x+0+0\> "m/*.jpg" 
            cd ~- 
        fi 
    fi 
done 

exit 0

by Mark -