图像保护在codeigniter [英] image protection in codeigniter

查看:80
本文介绍了图像保护在codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控制器中使用以下代码:

I am using the following code in my controller:

//function to protect images from being accessed directly.
function getImage($img_id){


      //code to authenticate user goes here then...
$url = $this->data['base_url'].'system/application/images/c/thumbs/';

$filepath = $url.$img_id;

    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);
    }



    }

在我看来,要获取图像,我使用了以下代码:

In my view to fetch the images, I have used the following code:

<img src='<?php echo base_url(); ?>index.php/Controller/getImage/obama.jpg' width="100px"> 

现在,即使它进入控制器,它不显示图像。 Obama文件在正确的目录中。我不知道为什么。

Now even though it is going into the controller, it is not displaying the image. The Obama file is in the correct directory. I have no idea why.

推荐答案

您的问题是,您正在输出文本到浏览器,然后图像。浏览器将尝试渲染图像,但会被破坏。

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);
}

此外,如果你想做一个 echo 您不能发送说这是一个jpeg图像。

Also, if you want to do an echo you can't send an header saying "hey, this is a jpeg image".

这篇关于图像保护在codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆