[英]Display image via php file with content type image Yii
<?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/')."' />";
}
}
?>
它無法在test / index或test / displayimg上顯示圖像。 有幫助嗎?
我認為在調用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));
}
好吧,你可以調用視圖並從動作中傳遞一些參數並在那里顯示它們
例如(未測試)
<?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,
));
}
}
?>
並且在view
你可以做點什么
echo CHtml::image(filepath, 'image', array('class' => 'banner'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.