CHtml::image() is helper for generate HTML img tag in Yii framework.

1.Class Reference

public static string image(string $src, 
                           string $alt='', 
                           array $htmlOptions=array ( ))

  • $src: string of image path
  • $alt: html alt tag content, for altenative text display when img lost.
  • $htmlOptions: array of HTML attribute of img

2.Example of Chtml::image()

In the following examples, we assume that image files are located in YII_APP_BASE_PATH/img/ . For example, you have a yii application called : blog, and add all images in blog/img/ folder. Yii have a variable called Yii::app()->request->baseUrl point to application root folder, you can use this variable for base path.
 - Image only:
echo CHtml::image(Yii::app()->request->baseUrl.'/img/image.png'); 
- Image with alt attribute:
 
echo CHtml::image(Yii::app()->request->baseUrl.'/img/image.png',
                                          "this is alt tag of image"); 
Result:
<img src="..." alt="this is alt tag of image">
- Add width and height attribute for image:
echo CHtml::image(Yii::app()->request->baseUrl.'/img/image.png',
      "this is alt tag of image",
      array("width"=>"50px" ,"height"=>"50px"));

Result:
<img alt="this is alt tag of image" height="50px" src="..." width="50px" />
You can also add another html attributes like class, id,etc for image in the similar way to adding width and height attribute for image above.