gpt4 book ai didi

php - CodeIgniter 图像在项目中不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 21:28:15 25 4
gpt4 key购买 nike

我在我的根文件夹中名为 tester.php 的文件中有一段代码,在 CI View 文件中有一个副本。根文件夹中的文件 tester.php 可以正常工作,但 View 中的代码(或模型、 Controller ,无论实际 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

但是当在 Controller 、 View 或模型中使用相同的代码时,它只会产生 enter image description here

更新 2:

Controller :

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";

编辑 3:

我试图删除文件以检查它是否不起作用。但据我所知:

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

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

车库模型文件:

<?



class Garage_model extends CI_model {




}

最佳答案

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

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

}

这个“ View ”文件位于 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 计划:

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

关于php - CodeIgniter 图像在项目中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33169990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com