Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 25 total  RSS 

Howto resize multiple pictures, graphics, images

for k in $(ls *.jpg); do convert -resize 800 -quality 80 $k r800-$k; done

date_select conversion

function to convert a value from a date_select into a more sql-friendly value

<%=date_select(:date,'',:start_year => 1950,:include_blank => false, :default => { :year => '1970' })%>

def convert_date(obj) 
  return “#{obj[‘(1i)’]}-#{obj[‘(2i)’]}-#{obj[‘(3i)’]}” 
end

Output JavaScript variables from PHP

Class with useful static methods for outputting PHP values into JavaScript format.

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

class JS{
	//generic and maybe not the desired results xD
	function value($o){
		if($o === null)
			return 'null';
		$t = strtolower(gettype($o));
		if($t == 'string' && is_numeric($o) && ($o[0] || strlen($o) == 1) || in_array($t, array('double', 'integer')))
			$t = 'number';
		elseif($t == 'string' && preg_match('@^\d{4}(?:-\d{1,2}){1,2}(?: (?:\d{1,2}:){2}\d{1,2})?$@', $o)) //strtotime works also with "strange" values strtotime('x')
			$t = 'date';
		elseif($t == 'array' && ($c = count($k = array_keys($o))) && $k !== range(0, $c - 1))
			$t = 'object';
		elseif(!in_array($t, array('boolean', 'string', 'array', 'object')))
			$t = 'string';
		$t = 'from' . $t;
		return self::$t($o);
	}
	function fromNumber($o){
		return +$o . '';
	}
	function fromObject($o){
		$r = array();
		foreach($o as $n => $v)
			$r[] = self::fromString($n) . ':' . self::value($v);
		return '{' . implode(',', $r) . '}';
	}
	function fromBoolean($o){
		return $o ? 'true' : 'false';
	}
	//$q = should quote? 
	//$c = char that will be used to quote
	function fromString($o, $q = true, $c = '"'){
		return ($p = $q ? $c : '') . preg_replace('/\r\n|\n\r|\r/', '\n', str_replace($c, '\\' . $c, str_replace('\\', '\\\\', $o))) . $p;
	}
	function fromArray($o){
		$s = '';
		foreach($o as $v)
			$s .= ($s ? ',' : '') . self::value($v);
		return '[' . $s . ']';
	}
	function fromDate($o){
		(is_numeric($o) && $o = +$o) || ($o = strtotime($o)) > 0 || ($o = mktime());
		$o = explode(',', date('Y,n,j,G,i,s', $o));
		foreach($o as $i => $v)
			$o[$i] = +$v;
		return 'new Date(' . implode(',', $o)  . ')';
	}
}


Example

$o = new stdClass;
$o->abc = 123;
echo implode("\n<br />", array(
	JS::value('1984-07-22 11:30:12'),
	JS::value('Test'),
	JS::value(1234),
	JS::value(true),
	JS::value(array(1,2,3)),
	JS::value(array('lala' => 'x')),
	JS::value($o)
));

Batch re-size a collection of images from the command line

for img in $(ls *.png); do convert $img -resize 75% smaller-$img; done;

UTF-8 Converter //JavaScript Object




Converts a sequence of ANSI characters to UTF-8 and vice-versa.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/geral/utf-8 [v1.0]

UTF8 = {
	encode: function(s){
		for(var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l;
			s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]
		);
		return s.join("");
	},
	decode: function(s){
		for(var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
			((a = s[i][c](0)) & 0x80) &&
			(s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ?
			o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = "")
		);
		return s.join("");
	}
};


Example

var s = "aáéíóúe";
document.write(
	('UTF8.encode("' + s + '") = ').bold(), UTF8.encode(s), "<br />",
	('UTF8.decode(UTF8.encode("' + s + '"))) = ').bold(), UTF8.decode(UTF8.encode(s))
);

Increment a date using Ruby

This Ruby code converts a string into a date and increments the day, week, month, quarter or year.

def date_add(sdate='', unit='',i=0)

  sdate[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]
  iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i
  
  case  unit
    when 'days'
      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
      t1 += (60 * 60 * 24 * i)
    when 'weeks'
      t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
      t1 += (60 * 60 * 24 * 7 * i) 
    when 'months'
      imonth += i
      if imonth < 12 then
        t1 = Time.local(iyear,imonth+i,iday,ihour,imin,isec)
      else
        t1 = Time.local(iyear+=1,imonth -12,iday,ihour,imin,isec)
      end
    when 'quarter'
      imonth += 3
      if imonth <= 12 then
        t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
      else
        t1 = Time.local(iyear+=1,imonth - 12,iday,ihour,imin,isec)
      end    
    when 'years'
      t1 = Time.local(iyear+i,imonth,iday,ihour,imin,isec)
    else
      raise 'not a valid date unit'
  end
  t1
end

date_add("17/03/2008 17:48:00",'months',2)

output: Sat May 17 17:48:00 +0100 2008

Convert from HTML to XHTML with HTML Tidy

This HTML Tidy example converts an html file into an xml file.

tidy -asxhtml -numeric < index.html > index.xml


example found from Tip: Convert from HTML to XML with HTML Tidy [ibm.com]

Convert a UTF-8 string to ISO-8859-1

Convert a utf string to iso, used this when generating a pdf with pdf-writer in Rails, all my text is UTF8 but pdf-writer does not support this.

#add this to environment.rb
#call to_iso on any UTF8 string to get a ISO string back
#example : "Cédez le passage aux français".to_iso

class String
  require 'iconv' #this line is not needed in rails !
  def to_iso
    Iconv.conv('ISO-8859-1', 'utf-8', self)
  end
end

Convert a video file to an audio file (.mp4 to .mp3)

Convert an mp4 file to avi, then to mp3 (including mixing the stereo down to mono). This code was executed from the command-line on Ubuntu 7.04.

mencoder video.mp4 -ovc lavc -vf scale=123:100 -oac lavc -o video.avi
ffmpeg -i video.avi -ac 1 audio1.mp3 

Convert a string to a character array

// converts a string into a character array, then displays the output 1 character at a time from the array.

y = 'testing'.scan(/./)
y.each do |c|
	puts c
end
« Newer Snippets
Older Snippets »
Showing 1-10 of 25 total  RSS