[eluser]John Morton[/eluser]
I'm still fighting with it actually. The way I've currently been resizing images is using CI's built in framework and it's working except when I have a PNG (or GIF?) that has a transparency. Here's what I've got which lives in my model.
You'll see I've got my resizing happening in this one big function. I like the idea of using ResizeImage as in your post, but need some advice. Any advice is greatly appreciated. -John
Code:
function updateHomeImage($myImageFromForm)
{
$shrinkdimensions = 250;
$enlargedimensions = 500;
$config['upload_path'] = './uploads/home/original/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['max_width'] = '1000';
$config['max_height'] = '1000';
$config['encrypt_name'] = false;
$config['remove_spaces'] = true;
$this->load->library('upload', $config);
if ($this->upload->do_upload($myImageFromForm)){
foreach ($this->upload->data() as $item => $v){
# print_r("item=".$item." v=".$v."<br />");
# returns lots of info about the uploaded file which is stored in the data variable belowe
$data[$item] = $v;
}
# storing file details in toupdatedb to be inserted into database
$toupdatedb->primaryoriginal = $data['file_name'];
$this->orig_width = $data['image_width'];
$this->orig_height = $data['image_height'];
// Start image_lib to allow resizing
$this->load->library('image_lib');
$imgfile = $data['full_path'];
$imgpath = $data['file_path'];
$filename = $data['file_name'];
$config['image_library'] = 'GD2';
$config['source_image'] = $imgfile;
$config['maintain_ratio'] = TRUE;
$config['create_thumb'] = false;
# need to find base_path so of uploaded file to store resized images in sibling directories
$base_path = substr($imgpath, 0, strripos($imgpath, 'original/'));
#// Make Small resize
if (($this->orig_width >= $shrinkdimensions) || ($this->orig_height >= $shrinkdimensions))
{
//print_r("small sizeifier fired <br />");
# $this-ResizeImage(250, 250); <- didn't work, so commented out
$config['new_image'] = $base_path. 'small/'.$filename;
$config['width'] = $shrinkdimensions;
$config['height'] = $shrinkdimensions;
$config['master_dim'] = 'width'; // this sets the resizer to default to height when preserving the ratio
$this->image_lib->initialize($config);
if ($this->image_lib->resize())
{
//print_r("success small <br />");
$toupdatedb->primarysmall = $filename;
} else {
$toupdatedb->primarysmall = NULL;
//print_r("small sizeifier DIDN'T fire <br />");
};
// Make Large resize
if (($this->orig_width >= $enlargedimensions) || ($this->orig_height >= $enlargedimensions))
{
//print_r("large sizeifier fired");
$config['new_image'] = $base_path. 'large/'.$filename;
$config['width'] = $enlargedimensions;
$config['height'] = $enlargedimensions;
$this->image_lib->initialize($config);
if ($this->image_lib->resize())
{
//print_r("success large /n <br />");
$toupdatedb->primarylarge = $filename;
} else {
$toupdatedb->primarylarge = NULL;
//print_r("large sizeifier DIDN'T fire <br />");
}
$this->db->update('home', $toupdatedb);
return $data;
} else {
return FALSE;
}
}
# not sure how to call the following functions to make them work with updateHomeImage using img_lib above
function ResizeImage($newWidth, $newHeight)
{
$this->newWidth = $newWidth;
$this->newHeight = $newHeight;
$this->dest_image = imagecreatetruecolor($this->newWidth, $this->newHeight);
$this->ImageSetAlpha();
imagecopyresampled($this->dest_image, $this->src_image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, imagesx($this->src_image), imagesy($this->src_image));
$this->src_image = $this->dest_image;
}
function ImageSetAlpha ()
{
$background = imagecolorallocate($this->dest_image, 0, 0, 0);
ImageColorTransparent($this->dest_image, $background);
imagealphablending($this->dest_image, false);
}
It seems like I need a Resize function that returns a reference to the new resized file so I can store it in my database. Does this mean I need to have a resize function that not only takes the x and y dimensions, but a reference to the file that needs to be resized already sitting on my server?