Need To Show Image On View And Edit Page

Hi Guys,

I am new in yii. I am uploading image in add product section and image is successfully uploaded in image folder but i am trying to show image in view and edit section. Image name is same as product name when it’s store in image folder so I m using below code to show the image. please check and give me the correct solution

<?php echo CHtml::encode($data->getAttributeLabel(‘Image’)); ?>:</b>

<?php echo CHtml::image("F:\\xampp\htdocs\\testdrive\images\\". $data->name); ?>

Looking forward for your quick response…

Your image should use a URL, not a file system path.

You’ll want something like:

<?php echo CHtml::image(Yii::app()->baseUrl . '/images/' . $data->name . '.ext'); ?>

Assuming that the file extension is not included in the name attribute already.

You shouldn’t use absolute path to the image.

Use path relative to webroot.

In your case, i think it’s /testdrive/images

Also you may want to use this var:


Thanks for your quick response. yes I got my mistake. can you please provide me a solution so that during view image I can resize it.

You mean output it at a smaller size, like a thumbnail?

The easiest way is to simply restrict the width or height in the image tag, but the server’s still sending the full size image, so this may be too slow for you. It’s also not that ‘clean’ a solution:

<?php echo CHtml::image(Yii::app()->baseUrl . '/images/' . $data->name . '.ext',

        'My Image Name',

        array('style'=>'height:80px;')); ?>

If you want to create separate thumbnail images, look at this extension:


<?php echo CHtml::image(‘url’, array(‘style’=>‘width:100px; height:100px’)); ?>

ops… Keith was faster, only a minute, but definitively faster

Ninja’d :P

Thanks for your support and guidance. I am checking these extension. If I’ll have any issue I’ll get back to you :)

Thanks for your quick response and support…

You can also use directly <img> tag in view file as,

<img src="<?php echo Yii::app()->request->baseUrl; ?>/images/<?php echo $data->name ?>"  height=" " width=" "  />

This is just an example, better to use Yii CHtml Image tag…

Yeah, he leaved me behind too.

I think he knows how to travel in time :D