Display image via php file with Yii image content type

<?php class TestController extends CController { public function actionIndex() { $filename = "test.jpg"; $path=Yii::getPathOfAlias('webroot.protected.images') . '/'; $file=$path.$filename; if (file_exists($file)) { $img=getimagesize($file); header('Content-Type: '.$img['mime']); readfile($file); exit; } } public function actionDisplayimg() { echo "<img src='".CController::createUrl('test/')."' />"; } } ?> 

It cannot display the image on either test / index, or test / displayimg. Any help?

+4
source share
2 answers

I think you should set the correct headers and content type to invoke the readfile.

 public function actionIndex() { $filename = "test.jpg"; $path=Yii::getPathOfAlias('webroot.protected.images') . '/'; $file=$path.$filename; if (file_exists($file)) { $request = Yii::app()->getRequest(); $request->sendFile(basename($file),file_get_contents($file)); } 
+3
source

well you can call the view and pass it some arguments from the action and display them there

e.g. (NOT TESTED)

 <?php class TestController extends CController { public function actionIndex() { $filename = "test.jpg"; $path=Yii::getPathOfAlias('webroot.protected.images') . '/'; $file=$path.$filename; if (file_exists($file)) { $this->render('view',array( 'filepath'=>$file, )); } } ?> 

and in view you can do something

 echo CHtml::image(filepath, 'image', array('class' => 'banner')); 
0
source

Source: https://habr.com/ru/post/1390967/


All Articles