ImageMagick Rounded Corners - CSS

I had a series of five 100x75 images, concatenated into a single image, and I wanted to display them as individual images with rounded corners.

To round the corners, I used the following ImageMagick command:

convert -size 100x75 xc:white -fill "#ffe" -draw "roundrectangle 0,0,100,75 10,10" -transparent "#ffe" mask.gif

It creates an image, dimensions of 100x75 pixels, with a white canvas. A rounded rectangle is drawn on it, filled with #ffe (any color will do). Next, the rectangle dimensions are given, as well as the rounding settings. Finally, the transparent option is used to indicate that the fill color is actually transparent. Be sure to use an image format (.gif or .png) that supports transparency.

A class was used to assign the mask, and an id was used to position the background image.


.dvPhone  
{
        float:left;
        width:100px;
        height:75px;
        margin:7px;
}
.dvPhoneNumber
{
        height:75px;
        width:100px;
}
#dvOffice
{
        background:transparent url(bg.jpg) no-repeat 0 0;
}
.round
{
        background:transparent url(mask.gif) no-repeat;
        width:100px;
        height:75px;
}


<div class="dvPhone">
<div id="dvOffice" class="dvPhoneNumber">
<div class="round">
<label>Office</label>
<input type="text" name="sOfficePhone" id="sOfficePhone" value="0000000000" />
</div>
</div>
</div>

Tested with IE7, FF3.

Related: http://web-notes.wirehopper.com/2009/06/07/smoother-imagemagick-rounded-rectangles

Powerful Tools to Quickly Skin an Application

The link above allows you to upload images, which can be compiled into a banner. The banner can then be run through robots-design. That will extract the colors and apply them to the CSS.

Free .PNG and .JPG Banner Builder

Allows you to upload up to 10 images.

Assembles them into a banner 100px high. Adjusts height, retains aspect ratio.

Produces both a .PNG and .JPG.

Generic Class Wrapper - 2

The code in the previous post was converted to use Zend Framework’s Db component for database access. One of the first things I noticed was that the code got smaller.

There are many ways to use Zend_Db, for this implementation, this approach fits well. Note the use of Zend_Config_Ini as well.

object.class.php changed, and item.class.php was converted to store the properties in an object, rather than an array. classes/db.class.php was deleted.

classes/object.class.php

<?php

require_once 'Zend/Config/Ini.php';
require_once 'Zend/Db.php';

Class Object
{
        private $db;

        public function __construct()
        {
                $Config = new Zend_Config_Ini('ini.php','database');
                $this->db = Zend_Db::factory($Config->database);
        }

        protected function load($sTable,$sIdName,$sId)
        {
                $this->db->setFetchMode(Zend_Db::FETCH_OBJ);
                $oResult = $this->db->fetchRow('SELECT * FROM `'.$sTable.'` WHERE `'.$sIdName.'` = ?', $this->db->quote($sId));
                return $oResult;
        }

        protected function save($sTable,$sIdName,$oArgs)
        {
                $sId=$oArgs->{$sIdName};
                unset($oArgs->{$sIdName});
                $aArgs=get_object_vars($oArgs);
                foreach ($aArgs as $k => $v)
                        $aArgs[$k]=$this->db->quote($v);
                if ($this->load($sTable,$sIdName,$sId))
                        $this->db->update($sTable,$aArgs,$sIdName.'='.$this->db->quote($sId));
                else
                        $this->db->insert($sTable,$aArgs);
                return $this->load($sTable,$sIdName,$sId);
        }

        protected function remove($sTable,$sIdName,$sId)
        {
                return $this->db->delete($sTable,$sIdName.'='.$db->quote($sId));
        }

        public function __destruct()
        {
                $this->db->closeConnection();
        }
}
?>

ini.php

<?php /*
[database]
database.adapter=pdo_mysql
database.params.host=localhost
database.params.dbname=dbname
database.params.username=username
database.params.password=password
*/ ?>

Generic Class Wrapper

The objective of this wrapper is to provide a streamlined interface to data stored in a database.

Notes

  • Object properties are stored in an array. This makes managing them must easier. Upon instantiation, the array is loaded with empty strings.
  • Magic methods are used to set and return properties.
  • The properties have a one-to-one, exact mapping to the underlying database.
  • A generic object exists beneath the specific object. It accepts a table name, identifier column name, and an array of values.

item.class.php

<?php
/*mysql> show full columns from items;
+---------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field   | Type             | Collation       | Null | Key | Default | Extra          | Privileges                      | Comment |
+---------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| item_id | int(10) unsigned | NULL            |      | PRI | NULL    | auto_increment | select,insert,update,references |         |
| name    | varchar(64)      | utf8_unicode_ci |      |     |         |                | select,insert,update,references |         |
| url     | varchar(128)     | utf8_unicode_ci |      | MUL |         |                | select,insert,update,references |         |
| text    | text             | utf8_unicode_ci |      |     |         |                | select,insert,update,references |         |
+---------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
4 rows in set (0.02 sec)
*/

require_once 'obj.class.php';
Class Item Extends Obj
{
        private $aData;

        public function __construct()
        {
                $this->aData=array_fill_keys(array('item_id','name','url','text'),'');
                parent::__construct();
        }

        public function __set($name, $value)
        {
                $this->aData[$name] = $value;
        }

        public function __get($name)
        {
                if (array_key_exists($name, $this->aData))
                        return $this->aData[$name];
        }

        public function load($id)
        {
                $this->aData=parent::load('items','item_id',$id);
        }

        public function save()
        {
                return parent::save('items','item_id',$this->aData);
        }
}

?>

obj.class.php

<?php
require_once 'db.class.php';

Class Obj
{
        private $db;

        public function __construct()
        {
                $this->db=Database::singleton();
        }

        protected function load($sTable,$sIdName,$sId)
        {
                $aResult=false;
                $sQuery='SELECT * FROM `'.$sTable.'` WHERE `'.$sIdName.'`=\''.$this->db->SQLescape($sId).'\'';
                $this->db->query($sQuery);
                $aResult=$this->db->fetch_assoc();
                $this->db->free_result();
                return $aResult;
        }

        protected function save($sTable,$sIdName,$aArgs)
        {
                $sWhere=' WHERE `'.$sIdName.'`=\''.$this->db->SQLescape($aArgs[$sIdName]).'\'';
                unset($aArgs[$sIdName]);
                $sSet=$this->db->sPair($aArgs);
                $sQuery='SELECT * FROM `'.$sTable.'`'.$sWhere;
                $this->db->query($sQuery);
                if ($this->db->num_rows()>0)
                        $sQuery='UPDATE `'.$sTable.'` SET '.$sSet.$sWhere;
                else
                        $sQuery='INSERT INTO `'.$sTable.'` SET '.$sSet;
                $this->db->free_result();
                return $this->db->query($sQuery);
        }

        protected function remove($sTable,$sIdName,$sId)
        {
                $sQuery='DELETE FROM `'.$sTable.'` WHERE `'.$sIdName.'`=\''.$this->db->SQLescape($sId).'\'';
                return $this->db->query($sQuery);
        }

        public function __destruct()
        {
        }
}
?>

A search function will be added.

db.class.php

<?php
Class Database
{
        private static $instance;

        private $rLink;
        private $db;
        private $ini;
        private $rResult;

        public function __construct()
        {
                $this->ini = parse_ini_file('config.ini.php','true');
                $this->rLink = mysql_connect
                                ($this->ini['db']['host'],
                                 $this->ini['db']['user'],
                                 $this->ini['db']['password']);
                if ($this->rLink === false)
                        trigger_error(mysql_error());
                $this->db = mysql_select_db ($this->ini['db']['database'],$this->rLink);
                if ($this->db === false)
                        trigger_error(mysql_error());
                return true;
        }

        public static function singleton()
        {
                if (!isset(self::$instance)) {
                        $c = __CLASS__;
                        self::$instance = new $c;
                }
                return self::$instance;
        }

        public function SQLescape($s)
        {
                return mysql_real_escape_string($s,$this->rLink);
        }

        public function host_information()
        {
                return mysql_get_host_info($this->rLink);
        }

        public function query($sQuery)
        {
                $this->rResult=mysql_query($sQuery,$this->rLink);
                if ($this->rResult === false)
                        trigger_error(mysql_error());
                return $this->rResult;
        }

        public function fetch_assoc()
        {
                return mysql_fetch_assoc($this->rResult);
        }

        public function sPair($aPair)
        {
                $sReturn='';
                foreach ($aPair as $k => $v)
                        $sReturn .= "`$k`='".$this->SQLescape($v).'\', ';
                $sReturn=substr($sReturn,0,-2);
                return $sReturn;
        }

        public function sWhere($aWhere)
        {
                $sReturn='';
                foreach ($aWhere as $k => $v)
                        $sReturn .= " `$k`='".$this->SQLescape($v).'\' AND ';
                return substr($sReturn,0,-4);
        }

        public function sRegExp($aRegExp)
        {
                $sReturn='';
                foreach ($aRegExp as $k => $v)
                        $sReturn .= " `$k` REGEXP '".$this->SQLescape($v).'\' AND ';
                return substr($sReturn,0,-4);
        }

        public function num_rows()
        {
                return mysql_num_rows($this->rResult);
        }

        public function free_result()
        {
                mysql_free_result($this->rResult);
        }

        public function __destruct()
        {
                if (isset($this->rLink))
                        if ($this->rLink !== false)
                                mysql_close($this->rLink);
        }

        public function __clone()
        {
                trigger_error('Clone is not allowed.', E_USER_ERROR);
        }

}
?>