Modify codeigniter image class to allow setting of background color

I am using the codeigniter image class to resize and crop images to specific sizes. If the source image has a different aspect ratio that prevents it from being set to the size I want I pad the image adding space until it fits. The problem is I need to set this color to white. The test image is black and I suspect the color it chooses is arbitrary. I am thinking the core image library needs to be modified but would love a non hacking way. Please help. Here is my code for reference. Obviously I am passing parameters to it, it should be pretty obvious.

$cache_bust=$this->input->get('cache_bust');
		if (!file_exists($new_filename) || $cache_bust) {
			copy($row['photo'],$new_filename);	
			$this->CI->load->library('image_lib'); 
			if ($row['width']>$device_params['width'] || $row['height']>$device_params['height']) {
				$config['image_library'] = 'gd2';
				$config['source_image']	= $new_filename;
				$config['maintain_ratio'] = TRUE;
				$config['quality'] = '40%';	
				if ($scale) {
					$device_params['height'] = ($scale / 100) * $device_params['height'];
					$device_params['width'] = ($scale / 100) * $device_params['width'];
				}
				$config['height'] = $device_params['height'];
				$config['width'] = $device_params['width'];
				$config['master_dim'] = 'height';
				if ($row['width']<$row['height']) {
					$config['rotation_angle'] = '270';
					$config['master_dim'] = 'height';
					if (strtolower(substr($new_filename,-3))=='jpg') {
						$this->CI->image_lib->rotate();
					}
				} else {
					
				}

				$this->CI->image_lib->initialize($config); 

				$this->CI->image_lib->resize();	
				$this->CI->image_lib->clear();
				unset($config);
			} 

			if ($device_params['crop']) {
				
			  	$config['image_library'] = 'gd2';
			  	//$config['library_path'] = '/usr/bin/';
			  	$config['source_image']	= $new_filename;
				$size = getimagesize($new_filename);
				//var_dump($size);
				$x_axis = $size[0]/2-($device_params['width']/2);
				$y_axis = $size[1]/2-($device_params['height']/2);
				//echo $x_axis.'-'.$y_axis;
				//die();
				$config['maintain_ratio'] = FALSE;
				$config['height'] = $device_params['height'];
				$config['width'] = $device_params['width'];
				$config['y_axis'] = $y_axis;
				$config['x_axis'] = $x_axis;
				$config['master_dim'] = 'auto';
				$this->CI->image_lib->initialize($config); 
				if ( ! $this->CI->image_lib->crop())
				{
				    $data['output']['status'] = 0;
				    $data['output']['message'] = $this->CI->image_lib->display_errors();
				}

			}

Open in new window

skioneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Reading this page does not lead me to believe there is a totally "non-hack" solution.
http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

I think you would need to add a parameter to the config array, and add a method to the image_lib class.  Perhaps there is an easy way to extend image_lib, but I'm not familiar with how CodeIgniter handles stuff like that.

Here is my teaching example showing how to put an image on a transparent background.  Using this, I think you could style the background of the image div with any color you wanted.

<?php // RAY_image_thumbnail.php
error_reporting(E_ALL);


// RESIZE AN IMAGE TO FIT INSIDE A DEFINED TRANSPARENT SPACE
// USE CASE: <img src="RAY_image_thumbnail.php?img=RAY_image_thumbnail.jpg&w=150&h=100" />


// ACQUIRE THE ARGUMENTS - MAY NEED SOME SANITY TESTS?
$thumb_w   = $_GET["w"];
$thumb_h   = $_GET["h"];
$image_url = $_GET["img"];

// CREATE THE THUMBNAIL IMAGE RESOURCE AND FILL IN TRANSPARENT
$thumb = imagecreatetruecolor($thumb_w, $thumb_h);
imagesavealpha($thumb, TRUE);
$empty = imagecolorallocatealpha($thumb,0x00,0x00,0x00,127);
imagefill($thumb, 0, 0, $empty);

// GET ORIGINAL IMAGE DIMENSIONS
$array = getimagesize($image_url);
if ($array)
{
    list($image_w, $image_h) = $array;
}
else
{
    trigger_error("NO IMAGE $image_url", E_USER_ERROR);
}

// ACQUIRE THE ORIGINAL IMAGE
$image_ext = trim(strtoupper(end(explode('.', $image_url))));
switch(strtoupper($image_ext))
{
    case 'JPG' :
    case 'JPEG' :
        $image = imagecreatefromjpeg($image_url);
        break;

    case 'PNG' :
        $image = imagecreatefrompng($image_url);
        break;

    default : trigger_error("UNKNOWN IMAGE TYPE: $image_url", E_USER_ERROR);
}

// GET THE LESSER OF THE RATIO OF THUMBNAIL H OR W DIMENSIONS
$ratio_w = ($thumb_w / $image_w);
$ratio_h = ($thumb_h / $image_h);
$ratio   = ($ratio_w < $ratio_h) ? $ratio_w : $ratio_h;

// COMPUTE THUMBNAIL IMAGE DIMENSIONS
$thumb_w_resize = $image_w * $ratio;
$thumb_h_resize = $image_h * $ratio;

// COMPUTE THUMBNAIL IMAGE CENTERING OFFSETS
$thumb_w_offset = ($thumb_w - $thumb_w_resize) / 2.0;
$thumb_h_offset = ($thumb_h - $thumb_h_resize) / 2.0;

// COPY THE IMAGE TO THE CENTER OF THE THUMBNAIL
imagecopyresampled
( $thumb
, $image
, $thumb_w_offset
, $thumb_h_offset
, 0
, 0
, $thumb_w_resize
, $thumb_h_resize
, $image_w
, $image_h
)
;

// SHOW THE NEW THUMB IMAGE
header('Content-type: image/png');
imagepng($thumb);

// RELEASE THE MEMORY USED BY THE RESOURCES
imagedestroy($thumb);
imagedestroy($image);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skioneAuthor Commented:
Thanks but I really need a CI solution
0
Ray PaseurCommented:
I don't believe there is a CI solution.  There are no pink unicorns, either.  :-)

According to this, 2.1.4 is the current version:
http://ellislab.com/codeigniter/user-guide/installation/downloads.html

Please read the man page here:
http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

That's why I wrote that I think you would need to add a parameter to the config array, and add a method to the image_lib class.  I think you will have to extend the image_lib class.  The code I posted above will give you a head-start on the programming you must write in the method(s) you create to extend the core CI image_lib class.

Others have faced this issue, too.  Here are some online resources that appear to be helpful.  I have not evaluated them for accuracy, but they will probably lead you in the right direction.
http://ellislab.com/forums/viewthread/194468/#916405
https://github.com/tmimori/codeigniter-image_lib
http://jenssegers.be/blog/31/codeigniter-resizing-and-cropping-images-on-the-fly
0
skioneAuthor Commented:
Wasn;t what I used but it would work
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.