display image


I have a user profile where i want the image of the user to be displayed along with its information. The information is displayed but not the image. The output is as follows

ID: 141

Member Login:

First Name:

Middle Name:

Last Name:

Gender: 0

Date of birth: 2011-10-17

Upload Photo2:

I want to display the thumbnail of the user. Any ideas how to go about it?


Actually, you need to store path to the image and point to that path in img src attribute. Store images for example in webroot.images directory.

Can u plz suggest how to go about it. I tried all the tuts but was unable to get the desired result.


What is stored in your database? If it is the URL of the image (assuming the field is named photo2):

<?php echo CHtml::image($model->photo2); ?> 

If is the image it self… then you need to be trickier… you will have to call a controller’s action and push the mime type and the contents of the field

// on your view

<?php echo CHtml::image($this->createUrl('controller/action',array('id'=>$model->id)); ?> 

// on the controller's action

$id = Yii::app()->request->getParam('id');

$model = MyModel::model()->findByPk($id);

// whatever the type of the image is

header('content-type: image/jpeg');

// whatever the field name is

echo $model->photo2;

Thanks the photo is displayed but can u plz suggest how to resize it and display the same as the thumbnail.


Take a look at this topic http://www.yiiframework.com/forum/index.php?/topic/24475-generating-thumbnail-of-image-and-using-it/page__p__118585__fromsearch__1#entry118585

I do not know if you wish to thumbnail display or to create a thumbnail of the picture… here both ways:

creating a thumbnail:


display with original as thumbnail -one of many on the web:



I want to display the thumbnail of the uploaded image. I have used the image helper yii extension and given write permission to the images folder under protected. I am getting the following errors.

getimagesize(C:/xampp/htdocs/profile) [<a href=‘function.getimagesize’>function.getimagesize</a>]: failed to open stream: Permission denied


1058 *

1059 * This function will throw exceptions for invalid images / mime-types

1060 *

1061 */

1062 protected function determineFormat ()

1063 {

1064 if ($this->isDataStream === true)

1065 {

1066 $this->format = ‘STRING’;

1067 return;

1068 }


1070 $formatInfo = getimagesize($this->fileName);


1072 // non-image files will return false

1073 if ($formatInfo === false)

1074 {

1075 if ($this->remoteImage)

1076 {

1077 $this->triggerError('Could not determine format of remote image: ’ . $this->fileName);

1078 }

1079 else

1080 {

1081 $this->triggerError('File is not a valid image: ’ . $this->fileName);

1082 }

Any ideas plzzzz.


getimagesize(C:/xampp/htdocs/profile) [<a href=‘function.getimagesize’>function.getimagesize</a>]: failed to open stream: Permission denied

Are you sure you have permissions set? make sure is read+write and that the path is the correct one

I finally can avoid too much PHP just for an <img>.

As my test upload form was for the current user avatar, i have that in my controller


class UploadController extends Controller


    function actionIndex()


        $dir = Yii::getPathOfAlias('webroot.uploads');

        $uploaded = false;

        $model=new Upload();



        $curentuser = Yii::app()->user->name;   




        $uploaded = $file->saveAs($dir.'/'.$curentuser.'_avatar.jpg');





        $this->render('index', array(

        'model' => $model,

        'uploaded' => $uploaded,

        'dir' => $dir,




So i can display the image in the view with just

<img src="http://localhost/blog/uploads/<?php echo Yii::app()->user->name; ?>_avatar.jpg" />

That’s really basic usage but it’s a start point.