Wednesday, December 5, 2012

Tuesday, April 12, 2011

SHARE - Client OS Detection

    <%= request.env['HTTP_USER_AGENT'] %>
    <%= request.env['HTTP_USER_AGENT'] =~ /Linux/ %>
    <%= request.env['HTTP_USER_AGENT'] =~ /Windows/ %>
    <%= request.env['HTTP_USER_AGENT'] =~ /Mac/ %>
    <%= request.env['HTTP_USER_AGENT'] =~ /iPhone/ %>

SHARE - How to get mp3 information (id3v1 and id3v2 tags) using ruby-mp3info

how to install gem
    gem install ruby-mp3info
how to use
    require "mp3info"
    begin
      mp3=Mp3Info.open(file_full_path)
      mp3.tag.title
      puts mp3.tag.artist  
      puts mp3.tag.album
    rescue Exception => e
      puts e.inspect
    end
for more information you can see here
http://ruby-mp3info.rubyforge.org/

SHARE - How to get image information using RMagick

    require 'RMagick'
    include Magick
    begin
      image=Image.read(pic_full_path).first
      image.filename #filename
      image.filesize #filesize
      image.columns #width
      image.page.width #width
      image.rows #height
      image.page.height #height
    rescue Exception => e
      puts e.inspect
    end

Friday, February 11, 2011

SHARE - TruliaMap integration

One of my client want add TruliaMap to one page for each his community site.
Each community have info about country, state/province, city, zipcode/postal code.

We can use this info for search it on google map.

What you need to make it work are latitude, langitude and zipcode for the place we want to search

add this to your controller

require 'open-uri'
require 'openssl'

url = "http://maps.googleapis.com/maps/api/geocode/xml?address=#{((community.name.to_s+' ')+(community.city.name.to_s+' ' if community.city)+(community.state.code.to_s+' ' if community.state)+(community.zipcode.zip.to_s if community.zipcode)).gsub(' ', '+')}&sensor=true"
xml = open(url).read
hash = Hash.from_xml(xml)
if hash['GeocodeResponse'] and hash['GeocodeResponse']['result'] and hash['GeocodeResponse']['result']['geometry'] and hash['GeocodeResponse']['result']['geometry']['location']
@lat= hash['GeocodeResponse']['result']['geometry']['location']['lat'] if hash['GeocodeResponse']['result']['geometry']['location']['lat']
@lng= hash['GeocodeResponse']['result']['geometry']['location']['lng'] if hash['GeocodeResponse']['result']['geometry']['location']['lng']
end


add this to your view

<div class='show-safety'>
<br/>
<div id="truliamap" style="font-family:arial,verdana;text-align:center; width:350px; height:380px; background-color:B5CF5A;">
<!--<div id="truliamap_header" style="color:;font-weight:bold;font-size:14px;padding:1px;height:16px;">Distinctive Properties</div>-->
<p style="margin:0px;padding:0px;line-height:0px;">
<iframe style="border:none; padding:0px; width:340px; height:329px;" frameborder="0" width="340" height="329" scrolling="no"
src="http://truliamap.trulia.com/truliamapapi/?a=5fa9ac&u=fd63b6&sid=&cv=&v=map&d=source&lat=<%= @lat -%>&lng=<%= @lng -%>&s=large&r=&city=&state=&uid=&zip=<%= community.zipcode.zip.to_s -%>">your browser not compatible</iframe>
</p>
<div style="margin-left:5px;margin-right:5px;text-align:left;font-size:11px;">
<div style="float:left;width:100px;">
<a id="map_signup" href="http://www.trulia.com/tools/map/" target="_blank" style="color:;">Get your own TruliaMap</a></div>
<div id="map_logo" style="float:right;width:79px;cursor:pointer;">
<a href="http://www.trulia.com/" target="_blank">
<img name="mmlogo" id="mmlogo" style="padding:0px; margin:0px;border-width:0px;" src="http://static.trulia-cdn.com/images/map/mm/logo-grn.gif" border="0" alt="Real Estate Search">
</a>
</div>
</div>
</div>
</div>

You can generate the code here http://www.trulia.com/tools/map/

SHARE - How to calculate how much brighness from color

My client ask me about how to make text black or white color base on background color for that text.

example:

- background color black, color of text is white
- background color white, color of text is black
- background color yellow, color of text is black
- background color dark-purple, color of text is white

I think I need to calculate how much brighness from background color that used.

After googling I found this formula for calculating brighness.

brightness = sqrt( .241 R2 + .691 G2 + .068 B2 )

R = red color (decimal)
G = green color (decimal)
B = blue color (decimal)

example:
   
background color : EA0516, text color : FFFFFF
backgorund color : FFFF00, text color : 000000

on my model

class model

def self.calculate_brightness(color) #format color on hex without #, example FFFFFF
r = color[0..1].hex
g = color[2..3].hex
b = color[4..5].hex
value = Math.sqrt(r * r * 0.241 + g * g * 0.691 + b * b * 0.068)
(value < 130 ? 'FFFFFF' : '000000')
end

end

You can see more information here
http://alienryderflex.com/hsp.html
http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx

SHARE - How to embed font from swf file using Font Loader

You can download Fontloader [here](http://etcs.ru/pre/FontLoaderDemo/srcview/FontLoaderDemo.zip)

You can see demo [here](http://etcs.ru/pre/FontLoaderDemo/)

FontLoader can embed all your font automatically.

It better then my last post about embed font on flash.


on your action script

package{

import flash.net.URLLoader;
import flash.net.URLRequest;

import ru.etcs.utils.FontLoader;

public class Main extends Sprite {

private const _loader:FontLoader = new FontLoader();

public function Main(){

this._loader.addEventListener(Event.COMPLETE, this.handler_complete);
this._loader.load(new URLRequest("Arial.swf"), true);

}

private function handler_complete(event:Event):void {

if(this._loader.fonts.length>0){
trace('FONTS');
var fonts:Array = this._loader.fonts;
for each (var font:Font in fonts) {
trace('--------------------------------------');
trace(font);
trace(font.fontName);
trace('--------------------------------------');
}
}
}

}

}