Skip to content

Instantly share code, notes, and snippets.

View kfriend's full-sized avatar

Kevin Friend kfriend

  • 742 Evergreen Terrace
View GitHub Profile
@kfriend
kfriend / gist:6833594
Created October 4, 2013 22:05
PHP: time_to_second() helper. Converts HH:MM:SS format to seconds. Can leave out hours or minutes.
<?php
function time_to_seconds($time = 0)
{
list($secs, $mins, $hours) = array_pad(array_reverse(explode(':', $time)), 3, 0);
return ($hours * 3600) + ($mins * 60) + $secs;
}
@kfriend
kfriend / gist:6706218
Last active August 11, 2016 00:20
PHP: Quick and dirty JIT image resizing (i.e. thumbnails) with built-in caching
<?php
$storagePath = './img';
$cachePath = $storagePath.'/cache';
// Maximum requestable dimensions
$maxWidth = 1000;
$maxHeight = 1000;
$forceJpeg = true;
@kfriend
kfriend / gist:6690005
Created September 24, 2013 19:28
PHP: array_splice_assoc() helper
<?php
function array_splice_assoc($array, $key, $replacement = array())
{
// Get numeric offset of the key to remove
$offset = array_search($key, array_keys($array));
if (!is_int($offset)) return $array;
unset($array[$key]);
on run
tell application "Numbers" to tell front document
set tPath to (POSIX file (get path)) as string
tell tPath to if it ends with ".numbers" then set tPath to text 1 thru -9 -- remove extension
save as "LSDocumentTypeCSV" in file tPath -- Numbers automatically adds the ".csv"
end tell
return tPath as alias -- return the path of the CSV file to the next action
end run
@kfriend
kfriend / gist:6396200
Last active December 22, 2015 01:28
AppleScript: Start screensaver
tell application "ScreenSaverEngine" to activate
@kfriend
kfriend / gist:6396198
Created August 31, 2013 04:17
Ruby: Split an image in two
#!/usr/bin/env ruby
# Takes a list of images as arguments and slices them in half
# All images should be in the same directory
require 'rubygems'
require 'RMagick'
include Magick
# Check to make sure at least one file was given
@kfriend
kfriend / gist:6396197
Created August 31, 2013 04:16
AppleScript: Create file w/ prompt
-- Get the currently selected item
tell application "Finder"
set currentFile to selection as text
end tell
set container to GetParentPath(currentFile)
display dialog "File name and extension" default answer "New File.txt"
set fileName to text returned of result
@kfriend
kfriend / gist:6396193
Created August 31, 2013 04:15
AppleScript: Get image dimensions
on open (filelist)
tell application "Image Events"
launch
repeat with i in filelist
set this_image to open i
copy dimensions of this_image to {current_width, current_height}
-- do stuff
end repeat
end tell
@kfriend
kfriend / gist:6396190
Created August 31, 2013 04:14
Ruby: Create image thumbnails using OS X sips
thumb_width = 117
thumb_height = 141
raise ArgumentError, 'No images given. You must supply at least one image to process' if ARGV.empty?
ARGV.each do |image|
if File.file?(image)
width = `sips -g pixelWidth "#{image}"`.match(/pixelWidth: \d+$/im).to_s.match(/\d+/).to_s.to_i
height = `sips -g pixelHeight "#{image}"`.match(/pixelHeight: \d+$/im).to_s.match(/\d+/).to_s.to_i
@kfriend
kfriend / gist:6396185
Created August 31, 2013 04:13
Shell: Create tarball without system cache files
export COPYFILE_DISABLE=true && tar -czvf tarball.tar.gz --exclude='.DS_Store' --exclude='._*' --exclude='Thumbs.db' /path/to/archive