Jump to content

Using a class that returns an image, how to display the image? Code Igniter


Recommended Posts

Hey Everyone,

 

I'm new to code igniter literally just downloaded it and messing about with it. I know I'm diving head first into the deep end here but all I want to test out is the inclusion of external classes, like a photo manipulation class.

 

For example currently I have a class that Equalises a photo and manipulates it, it does it's business here:

Class Equalise{
        // Main output function which reads the file and runs it through histogram equalization for the various color channels.
        function outputimage($filename) {

                $reds = array();
                $blues = array();
                $greens = array();
                
                $freqr = array();
                $freqb = array();
                $freqg = array();
                
                $info = getimagesize($filename);
                
                $width = $info[0];
                $height = $info[1];
                $totalpixels = $width * $height;
                
                
                $img = imagecreatefromjpeg($filename);
                
                if ($img) {
                        Equalise::buildHistograms($img, $width, $height, $reds, $greens, $blues);
                        Equalise::buildFrequencies($reds, $greens, $blues, $freqr, $freqg, $freqb);
                }
                
                $alpha = (float)(255.0 / (float)$totalpixels);
                $newimg = @imagecreatetruecolor($width, $height);
                $color = imagecolorallocate($newimg, 255, 255, 255);
                
                for ($i = 0; $i < $height; $i++) {
                        for ($j = 0; $j < $width; $j++) {
                                $rgb = imagecolorat($img, $j, $i);
                                $r = ($rgb >> 16) & 0xFF;
                                $g = ($rgb >>  & 0xFF;
                                $b = $rgb & 0xFF;
                                
                                $adjR = (int)((float)$freqr[$r] * $alpha);
                                $adjG = (int)((float)$freqg[$g] * $alpha);
                                $adjB = (int)((float)$freqb[$b] * $alpha);
                                
                                $color = imagecolorallocate($newimg, $adjR, $adjG, $adjB); 
                                imagesetpixel($newimg, $j, $i, $color);
                        }
                }
                // This is where the Image comes out.
                header('Content-Type: image/jpg');
                imagejpeg($newimg, NULL, 100);
                imagedestroy($newimg);
        }

 

It returns the image then destroys it but after it's been loaded into the browser.

 

I've been messing and added it into the application/libraries, as it said in the CI documentation, and in my Test controller added this code:

 

 

<?php
class Test extends Controller{

function index(){
header('Content-Type: image/jpg');
$this->load->library('Equalise');
$this->equalise->outputimage($_SERVER['DOCUMENT_ROOT']'/3.jpg');

}



}

?>

 

This takes 3.jpg and runs it through the class. But I just does't seem to work I can't get an image to come out at all.

Anyone have any idea's?

Thanks,

 

Jordan

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.