CodeIgniter Image在Project中不起作用

时间:2015-10-16 11:59:53

标签: php codeigniter

我在一个名为tester.php的文件中有一段代码,它位于我的根文件夹中,并且在CI视图文件中有一个副本。根文件夹中的文件tester.php可以正常工作,但视图中的代码(或模型,控制器,无论在CI中的哪个位置)都不会工作。

使用CI 2.2.0

为什么会这样?

结果在根tester.php:

enter image description here

导致CI:

enter image description here

完整代码:

$imageurl = "http://www.example.net/images/images/ABImage_clock_4.jpg";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
$data = curl_exec($ch);
curl_close($ch);

$im = imagecreatefromstring ($data);

$bg = imagecolorallocate($im, rand(1,255), rand(1,255), rand(1,255));
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// output text.
$font = 'arial.ttf';

for ($i = 0; $i < 70; $i++) {
    imagesetthickness($im, rand(1, 3));
    $bg = imagecolorallocate($im, rand(1,255), rand(1,255), rand(1,255));

    imagearc(
        $im,rand(1, 300), // x-coordinate of the center.
        rand(1, 300), // y-coordinate of the center.
        rand(1, 300), // The arc width.
        rand(1, 300), // The arc height.
        rand(1, 300), // The arc start angle, in degrees.
        rand(1, 300), // The arc end angle, in degrees.
        $bg // A color identifier.
    );
}

header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

更新

没有错误,并且imageurl确实有效,因此它不是图像链接。

问题: 从根文件夹调用tester.php时,它完美无缺:enter image description here

但在控制器,视图或模型中使用相同的代码时,它只生成enter image description here

更新2:

控制器:

class antibot_interface_controller extends CI_Controller {



    function getImage() {
        $this->load->view('tester');
    }


}

查看:

    <?


$imageurl = "http://www.textbasedmafiagame.com/images/images/ABImage_clock_4.jpg";



$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

$im = imagecreatefromstring ($data);

$bg = imagecolorallocate($im, rand(1,255), rand(1,255), rand(1,255));
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);

// set background colour.

// output text.
$font = 'arial.ttf';
// imagettftext($im, 35, 0, 10, 55, $color, $font, 'ABCD');

for ($i = 0; $i < 70; $i++) {
    imagesetthickness($im, rand(1, 3));
    $bg = imagecolorallocate($im, rand(1,255), rand(1,255), rand(1,255));



    imagearc(
        $im,rand(1, 300), // x-coordinate of the center.
        rand(1, 300), // y-coordinate of the center.
        rand(1, 300), // The arc width.
        rand(1, 300), // The arc height.
        rand(1, 300), // The arc start angle, in degrees.
        rand(1, 300), // The arc end angle, in degrees.

        $bg // A color identifier.
    );
}


header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

路线:

$route['antibot/antibot_image'] = "antibot/antibot_interface_controller/getImage";

EDIT3:

我尝试删除文件以检查它是否无效。但据我所知,它是:

$ autoload ['model'] = array('cartheft / garage_model');

当那个模型存在时,图像不显示。 当我删除该模型时,它将正常工作。

garage_model文件:

<?



class Garage_model extends CI_model {




}

1 个答案:

答案 0 :(得分:1)

这演示了一种使用Codeigniter使用典型MVC文件模式执行此操作的可能方法。 (使用Codeigniter 3.0.2和PHP 5.6.14测试)

以下&#39;控制器&#39;在root / application / controllers / imagetest.php     

if(!defined('BASEPATH')){ exit('No direct script access allowed'); }

class Imagetest extends CI_Controller
{
  private $imageurl;
  public function __construct()
  {
    parent::__construct();
    //Using an actual image I know exists
    $this->imageurl = "http://www.rapidsriders.org/images/PAC_logos/ACA_PAC_blue_sm.jpg";
  }

  public function index()
  {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->imageurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);

    $im = imagecreatefromstring($data);

    $bg = imagecolorallocate($im, rand(1, 255), rand(1, 255), rand(1, 255));
    $white = imagecolorallocate($im, 255, 255, 255);
    $black = imagecolorallocate($im, 0, 0, 0);

    // output text.
    $font = 'arial.ttf';

    for($i = 0; $i < 70; $i++)
    {
      imagesetthickness($im, rand(1, 3));
      $bg = imagecolorallocate($im, rand(1, 255), rand(1, 255), rand(1, 255));

      imagearc(
        $im, rand(1, 300), // x-coordinate of the center.
        rand(1, 300), // y-coordinate of the center.
        rand(1, 300), // The arc width.
        rand(1, 300), // The arc height.
        rand(1, 300), // The arc start angle, in degrees.
        rand(1, 300), // The arc end angle, in degrees.
        $bg // A color identifier.
      );
    }
    //pass the image resource to the view file 
    $image['im']= $im;
    $this->load->view('imageview', $image);
  }

}

这个观点&#39;文件位于root / application / views / imageview.php

<?php
//Because Codeigniter uses output buffering when you load a view file
//and because the GD functions are sensitive to the buffer state 
//we need to make sure the output buffer is clear. 
//Otherwise imagepng() will probably fail.
ob_clean(); 
//note use of Codeigniter output class to set header.
$this->output->set_content_type('image/png');
//This would work too, but why not utilize Codeigniter's libraries
//header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

使用yourURL/imagetest

运行此功能

计划B:

是我第一个答案的修订版。 我添加了一些错误检查,并放弃使用&#34; view&#34;暂时。 我非常确定问题与输出缓冲有关,因此请确保在发送标头之前保留行ob_clean();。如果省略,我会一直得到丢失的图像占位符。

将此代码用于 imagetest.php 的新版本     

if(!defined('BASEPATH')){
  exit('No direct script access allowed');
}

class Imagetest extends CI_Controller
{
  private $imageurl;

  public function __construct()
  {
    parent::__construct();
    $this->imageurl = "http://www.textbasedmafiagame.com/images/images/ABImage_clock_4.jpg";
  }

  public function index()
  {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->imageurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    $data = curl_exec($ch);
    if($data === FALSE){
      exit('Curl error: '.curl_error($ch));
    }
    curl_close($ch);

    $im = imagecreatefromstring($data);// or die('bad url:'.$this->imageurl);

    if ($im !== false) {
      for($i = 0; $i < 70; $i++){
        imagesetthickness($im, rand(1, 3));
        $bg = imagecolorallocate($im, rand(1, 255), rand(1, 255), rand(1, 255));

        imagearc(
          $im, rand(1, 300), rand(1, 300), rand(1, 300), 
          rand(1, 300), rand(1, 300), rand(1, 300), $bg);
      }
      ob_clean(); //without this the GD functions (like imagepng) tend to fail using CI
      header("Content-type: image/png");
      imagepng($im, NULL, 0, NULL);
      imagedestroy($im);
    }
    else{
      echo 'imagecreatefromstring() error.';
    }
  }
}

将此文件放在root/application/controllers/

文件夹中

在浏览器中转到http://www.textbasedmafiagame.com/imagetest