It is a CI bug and it looks like nobody has found a solution yet. You’ll have to dive into the CI Image lib code and rework the GD part that make the transparency working. Sorry I can’t help much more.
I had this same problem solved in PHP a while ago. Can you send me a link to a sample image that fails to work. If it keeps transparency in my code I’ll port the hack to CI library.
Your solution looks exactly like something I need. Pardon the newbie question, but where do these functions you’ve posted live? Are they in a model or controller?
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
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'];
// 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; }
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?
I believe I have the 24bit PNG transparency issue solved. It requires first creating a 100% transparent color, and then filling the new image before the imagecopyresampled.
I have made a custom library that replaces the image_lib->image_process_gd function and adds the relevant code. Just put it in your system/application/libraries directory and it should be good to go.
I appreciate the reply. I’m in the middle of another project right now, but will try to implement your fix as soon as I get back on my CI project. I’ll post about my results then. Thanks.