X-Git-Url: https://jasonwoof.com/gitweb/?p=wfpl.git;a=blobdiff_plain;f=encode.php;h=a395a10ef3e8558dc23b7a11214e811741b7d89d;hp=ab16a848b37c350806e23a16baa4855b7f86949f;hb=HEAD;hpb=203b704003fa9bfbe823e5ebc8d8e7cff919a073 diff --git a/encode.php b/encode.php index ab16a84..a395a10 100644 --- a/encode.php +++ b/encode.php @@ -1,19 +1,9 @@ . +# This program is in the public domain within the United States. Additionally, +# we waive copyright and related rights in the work worldwide through the CC0 +# 1.0 Universal public domain dedication, which can be found at +# http://creativecommons.org/publicdomain/zero/1.0/ # This file contains basic encodings. These are used by the encoder. You can @@ -42,6 +32,10 @@ function enc_jsdq($str) { return $str; } +function enc_json($str) { + return json_encode($str); +} + # encode for putting within double-quotes in SQL function enc_sql($str) { $str = str_replace("\\", "\\\\", $str); @@ -74,7 +68,7 @@ function enc_htmlbr($str) { # Example:

~foo htmlbrtab~

function enc_htmlbrtab($str) { $str = enc_htmlbr($str); - $whitespace_to_nbsp = create_function('$matches', '$count = 0; $chars = str_split($matches[0]); foreach ($chars as $c) { if ($c == " ") { $count += 2; } else if ($c == "\t") { $count += 8; } } return str_repeat(" ", $count);'); + $whitespace_to_nbsp = create_function('$matches', '$count = 0; $chars = str_split($matches[0]); foreach ($chars as $c) { if ($c == " ") { $count += 1; } else if ($c == "\t") { $count += 8; } } return str_repeat(" ", $count);'); $str = preg_replace_callback("|^[ \t]+|m", $whitespace_to_nbsp, $str); return $str; } @@ -437,34 +431,42 @@ function enc_evenodd($values, $name) { } } -function enc_image_src($str) { - list($src, $width, $height, $a, $b, $c) = explode(' ', $str); - return $src; -} - -function enc_image_width($str) { - list($src, $width, $height, $a, $b, $c) = explode(' ', $str); - return $width; -} - -function enc_image_height($str) { - list($src, $width, $height, $a, $b, $c) = explode(' ', $str); - return $height; +function wfpl_nth_word($str, $n) { + $a = explode(' ', $str); + return isset($a[$n]) ? $a[$n] : null; } -function enc_thumb_src($str) { - list($a, $b, $c, $src, $width, $height) = explode(' ', $str); - return $src; -} - -function enc_thumb_width($str) { - list($a, $b, $c, $src, $width, $height) = explode(' ', $str); - return $width; -} -function enc_thumb_height($str) { - list($a, $b, $c, $src, $width, $height) = explode(' ', $str); - return $height; +# encoding is a space separated list of: +# image_filename width height thumb_filename thumb_width thumb_height +function enc_image_src($str) { return wfpl_nth_word($str, 0); } +function _enc_image_src_at_width($str, $width) { + $src = enc_image_src($str); + if ($src) { + return + substr($src, 0, -4) + . 'w' + . $width + . substr($src, -4); + } + return ''; +} +# define these width constants in your config.php +function enc_image_src_full($str) { return _enc_image_src_at_width($str, WFPL_IMAGE_WIDTH_FULL); } +function enc_image_src_small($str) { return _enc_image_src_at_width($str, WFPL_IMAGE_WIDTH_SMALL); } +function enc_image_src_thumb($str) { return _enc_image_src_at_width($str, WFPL_IMAGE_WIDTH_THUMB); } +function enc_image_width($str) { return wfpl_nth_word($str, 1); } +function enc_image_height($str) { return wfpl_nth_word($str, 2); } +function enc_image_aspect($str) { + $a = explode(' ', $str); + if (count($a) < 3) { + return ''; + } + return ''.(round(100000 * ((int)$a[2]) / ((int)$a[1]) / 1000)).'%'; } +# obsolete: +function enc_thumb_src($str) { return wfpl_nth_word($str, 3); } +function enc_thumb_width($str) { return wfpl_nth_word($str, 4); } +function enc_thumb_height($str) { return wfpl_nth_word($str, 5); } # example template: Length: ~length html~ day~length s~ function enc_s($str) {