up

Blogs

HTML number formatting with PHP

Here is my HTML number formatter. Sometimes the browsers wrap the numbers along thousand separator or decimal pont, but this function prevents this event.

function html_number_format($number, $decimals = 2, $dec_point = '.', $thousands_sep = ',') {
  if ($number === FALSE) {
    return '---';
  }
  return '<span style="white-space: nowrap;">'.number_format($number, $decimals, $dec_point, $thousands_sep).'</span>';
}

Deploy rack applciaton

Make config.ru with following content:

require 'rubygems'
require 'sinatra'
require './my_sinatra_application.rb'

run Sinatra::Application

You have to run bundle command. This will be install all necessary gems that listed in Gemfile.

You find debug log in default apache 2 error log files. This is /var/log/apache2/error.log in my case.

RMagick install on Ubuntu 10.10 and 10.04

This small tutorial work on Ubuntu Hardy (8.04), Lucid 10.04 and Maverick (10.10)

Install Imagemagick and GraphicsMagick

apt-get install imagemagick graphicsmagick

Install Image Magick development files

apt-get install libmagickcore-dev libmagickwand-dev
gem install rmagick

On Hardy:
Install imagemagick from source and after it install development files:

Blog categories:  Linux  Unix admin

How to use Padrino with mongodb

Padrino is a ruby framework built upon the excellent Sinatra Microframework.


$ padrino g project blog -d mongomapper -t rspec -s jquery -e haml -c compass
create
create config.ru
create public/favicon.ico
create config/apps.rb
create config/boot.rb
create app
create app/app.rb
create app/controllers
create app/helpers
create .components
create Gemfile
Applying 'mongomapper' (orm)...
apply orms/mongomapper
inject Gemfile

Blog categories:  Web development
Blog tags:  ruby  padrino

How to install Phusion Passenger for ruby 1.9.2

There are some easy steps to install Phusion Passenger for ruby 1.9.2 and apache2 on Ubuntu Linux.

1. Install rvm system wide

sudo bash < <( curl -L http://bit.ly/rvm-install-system-wide )

2. Install ruby 1.9.2

rvm install 1.9.2

3. add new line in /etc/bash.bashrc

[[ -s /usr/local/lib/rvm ]] && . /usr/local/lib/rvm

4. install passenger

sudo -i # you need root shell
rvm use 1.9.2-p0@global # or your version, use: rvm list

How can you generate thumbnail with php?

You can read about two popular thumbnail generating methods in this article. The first method requires GD library. If you select the other way you need Imagick which is a native php extension to create and modify images using the ImageMagick API. Let's see the code!

PHP GD2 vs Imagick


http://bitprison.net/system/files/gd.jpeg


//include('tmp/Timer.php');

function gd_thumbnail($filename, $width = 200, $height = 200) {
$source = imageCreateFromString(file_get_contents($filename));
$x = imageSX($source);
$y = imageSY($source);

if ($x > $y) { // landscape orientation
$height = intval($y/($x/$width));
} else { // square or portrait orientation
$width = intval($x/($y/$height));
}

$destination = ImageCreateTrueColor($width,$height);