gluck
(Kostya Molchanov)
December 4, 2009, 9:02am
1
Goog day!
I have some problems with display an images. Images stored in database.
i have method of model-class
public function imageShow()
{
header("Content-type: image/png");
echo $this->image;
}
and i use it in views like this:
<?php echo CHtml::image($model->imageShow()); ?>
but my browser give me something like this: Image couldn’t display because of has an error. This error just when i sending a header
header("Content-type: image/png");
, but without content-type display just binary data of image.
Does anyone know how to display images from database?
I’m sure this won’t work, because CHtml::image() takes the image url as the first parameter but your imageShow() method returns nothing, just sends the binary data to the output.
You should create an action that sends the image and point to that action in CHtml::image() methods first parameter.
So something like this:
Controller
class MyImageController extends CController
{
...
public function actionDisplayImg()
{
$model = $this->loadModel();
Yii::app()->request->sendFile('myimage.png', $model->image, 'image/png');
}
...
}
View
...
<?php CHtml::image($this->createUrl('myImage/displayImg')); ?>
...
gluck
(Kostya Molchanov)
December 4, 2009, 9:42am
3
I’m sure this won’t work, because CHtml::image() takes the image url as the first parameter but your imageShow() method returns nothing, just sends the binary data to the output.
You should create an action that sends the image and point to that action in CHtml::image() methods first parameter.
So something like this:
Controller
class MyImageController extends CController
{
...
public function actionDisplayImg()
{
$model = $this->loadModel();
Yii::app()->request->sendFile('myimage.png', $model->image, 'image/png');
}
...
}
View
...
<?php CHtml::image($this->createUrl('myImage/displayImg')); ?>
...
Ooo, it’s nice decision. Thank you very much. Your post help me a lot.
mavs1971
(Mvillalonga)
October 13, 2014, 7:22pm
4
It does not show image, help!!!! i have tries every post bau i can not display a image save tro database
why store images in database?
it make ur app to have a very lower performance, instead save images name and address in db