【问题标题】:CodeIgniter Image won't work in ProjectCodeIgniter Image 在项目中不起作用
【发布时间】:2023-04-04 20:37:02
【问题描述】:

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

使用 CI 2.2.0

这是为什么?

根 tester.php 上的结果:

CI 的结果:

完整代码:

$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 时,它完美运行:

但是当在控制器、视图或模型中使用相同的代码时,它只会产生

更新 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 {




}

【问题讨论】:

  • 在 CI 中显示你的代码。
  • 您的错误日志中有什么内容吗?
  • @NiranjanNRaju 相同的代码。
  • @user2959229 没有错误:/
  • 如果 PHP 吐出任何错误消息,这将导致您在浏览器中看到“损坏的图像”图标 - 但您不会看到它们,因为浏览器无法将它们显示为“as”一个图像。在浏览器的网络面板中查看服务器为图像 URL 返回的数据 - 您应该能够在那里看到任何潜在的错误消息。

标签: php codeigniter


【解决方案1】:

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

以下“控制器”位于 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);
  }

}

这个“视图”文件位于根目录/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 计划:

是对我的第一个答案的修订。 我添加了一些错误检查并暂时放弃使用“视图”。 我很确定问题与输出缓冲有关,因此请确保在发送标头之前保留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

【讨论】:

  • 运行:textbasedmafiagame.com/imagetest 仍然相同。这是什么原因造成的?
  • 在虚拟 WAMP 堆栈和生产服务器上均适用于 2.1.4。我会在我的虚拟堆栈上试验你修改后的代码,看看会发生什么。
  • @maria,您的更新 2 代码对我有用。我不得不使用稍微不同的路由$route['antibot/antibot_image'] = "antibot_interface_controller/getImage" ; 你的路由在我看来是错误的,但如果你进入正确的页面(无论是否加载图像),那么显然没问题。
  • @maria,当您从我的回答中运行代码时,您只会得到“损坏的图像”占位符?
  • 这是正确的@DFriend 我从你的答案中运行代码并得到损坏的图像:/
猜你喜欢
  • 2016-11-08
  • 2017-06-30
  • 2022-06-23
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
相关资源
最近更新 更多