CodeIgniter Forums
Using a class that returns an image, how to display the image? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using a class that returns an image, how to display the image? (/showthread.php?tid=29260)



Using a class that returns an image, how to display the image? - El Forum - 04-04-2010

[eluser]Jordz[/eluser]
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:
Code:
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 >> 8) & 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:


Code:
&lt;?php
class Test extends Controller{

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

}



}

?&gt;

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


Using a class that returns an image, how to display the image? - El Forum - 04-04-2010

[eluser]Mischievous[/eluser]
Hey Jordan, welcome to the community. The controllers just handles the request. You need to pass the info to a "View" file or echo the image out.

Code:
&lt;?php
class Test extends Controller{

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

}
?&gt;

Also, make sure your "outputimage" function is returning the image before its destroyed.