maria
maria

Reputation: 159

CodeIgniter Image won't work in Project

I have a piece of code in a file called tester.php that is in my root folder, and one copy of it in a CI view file. The file tester.php in root folder works as it should, but the code in the view ( or model, controller , no matter where in the acual CI) wont work.

using CI 2.2.0

why is this?

result on the root tester.php:

enter image description here

result in CI:

enter image description here

full code:

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

UPDATE

There is no errors, and the imageurl does work, so its not the image link.

THE ISSUE: when calling the tester.php from the root folder, it works perfect: enter image description here

but when using the same code in a controller, view or model, it only produces enter image description here

UPDATE 2:

Controller:

class antibot_interface_controller extends CI_Controller {



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


}

View:

    <?


$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:

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

EDIT3:

I tried to remove files to check if its not working. but as far i can see it is:

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

when that model is there, the image is not showing. when i remove that model, it will work fine.

the garage_model file:

<?



class Garage_model extends CI_model {




}

Upvotes: 3

Views: 891

Answers (1)

DFriend
DFriend

Reputation: 8964

This demonstrates one possible way to do this using a typical MVC file pattern using Codeigniter. (Tested with Codeigniter 3.0.2 and PHP 5.6.14)

The following 'controller' is at 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);
  }

}

This 'view' file is at 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);

Run this with yourURL/imagetest

PLAN B:

Is a revision on my first answer. I added some error checking and abandoned the use of a "view" for the moment. I'm pretty sure the problem is related to output buffering so be sure to keep the line ob_clean(); just before the header is sent. I consistently get the missing image placeholder if this is omitted.

Use this code for a new version of 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.';
    }
  }
}

Put this file in the folder root/application/controllers/

Go to http://www.textbasedmafiagame.com/imagetest in your browser.

Upvotes: 1

Related Questions