I am using the following code in my controller:
//function to protect images from being accessed directly.
function getImage($img_id){
//code to auth
Your problem is that you are outputing text to the browser, and then the image. The browser will try to render the image but it will be corrupted.
header("Content-type: image/jpeg");
if(file_exists($filepath)){
echo "we are here";
$img_handle = imagecreatefromjpeg($filepath) or die("");
echo $img_handle;
ImageJpeg($img_handle);
}
Also, if you want to do an echo you can't send an header saying "hey, this is a jpeg image".