Image render issue

I am having an issue displaying jpeg’s from a Yii view. It works using the standard CHtml way but when I use code like imagejpeg shown below the page return blank with failed image icon on the top left hand corner. The reason I want to make use of imagejpeg (actually I am trying to make use of image extensions but they give exactly the same error) is I want to resize images.

// this works

echo CHtml::image("images/test.jpg");

// this doesnt

$image = imagecreatefromjpeg((’/var/www/myapp/images/test.jpg’);

header("Content-type: image/jpeg");


This code is sitting in my view and the last line make the page fail. I am missing some setting somewhere as the code seems ok ?



CHtml::image renders html <img> tag with src and alt/title

imagejpeg() returns image binary stream… this is the main difference.

When you pass filename to imagejpeg() - it will try to save that data in file (and probably fail because the file is already opened by imagecreatefromjpeg()). I think you should call it without filename so the binary data will be returned to stdout.

This same code works perfectly in a standalone php script outside of Yii, its only when I call it from the Yii app in the view that it fails. That seems to indicate that image libraries are all correct, php is setup correctly, the code is correct, but something about executing it in the yii framework is causing it to fail ? Or the way it needs to be called in the yii framework should be different ?

In fact it seems if one just puts the line;

header("Content-type: image/jpeg");

inside a Yii view class then it fails ?

No, but maybe you should try to correct double opening braces:

$image = imagecreatefromjpeg((’/var/www/myapp/images/test.jpg’);