image manipulation - CodeIgniter Upload and Resize Problem -
i have spent days trying make work based on examples in documentation missing or stupid!
i have cms application users upload image display in fixed layout. not want limit file size of uploaded image rather "process" after arrives.
the image needs 615px wide of images uploaded directly digital cameras 2500x2000 , bigger critical.
i pieced code manual , image being uploaded folder within cms app. however, image not being resized.
if ever re-size, plan present image user cropping using jcrop (the final image has 615x275 , has cropped height after resizing) , use codeigniter ftp image site's amenities folder using original name.
i appreciate in matter!
here's code:
function do_feature_upload() { $imagename = $this->uri->segment(3); //echo $imagename; // file going placed $config['upload_path'] = "./uploads/".$_session['dbpropnumber']; $config['allowed_types'] = 'jpg|jpeg'; $config['max_size'] = '0'; $config['file_name'] = $imagename.'.jpg'; $config['overwrite'] = 'true'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $error['propertydropdown'] = $_session['propertydropdown']; $error['username'] = $_session['username']; $error['dbpropnumber'] = $_session['dbpropnumber']; $error['propertyname'] = $this->content->getpropertyname($_session['dbpropnumber']); $this->load->view('upload_amenityimage', $error); } else { $image_data = $this->upload->data(); $origwidth = $image_data['image_width']; $origheight = $image_data['image_height']; $newwidth = 615; $newheight = $newwidth*$origheight/$origwidth; $resize = array( 'image_library'=>'gd2', 'source_image'=>base_url().'uploads/'.$_session['dbpropnumber'].'/'.$imagename.'.jpg', 'new_image'=>base_url().'uploads/'.$_session['dbpropnumber'].'/'.$imagename.'1.jpg', 'create_thumb' => false, 'maintain_ratio'=>false, 'width'=>$newwidth, 'height'=>$newheight ); $this->load->library('image_lib',$resize); $this->image_lib->resize(); $data = array('upload_data' => $this->upload->data()); $data['propertydropdown'] = $_session['propertydropdown']; $data['username'] = $_session['username']; $data['dbpropnumber'] = $_session['dbpropnumber']; $data['propertyname'] = $this->content->getpropertyname($_session['dbpropnumber']); //present jcrop option after image resized // ftp final destination $this->load->view('upload_success', $data); } // end if } // end function
i'm not entirely sure what's going wrong code, here's model function wrote resizing images fit exact target height and target width. read through , see if can't figure out solution.
$this->prefix
property in class use don't have keep writing out directory of file. looks this:
$this->prefix = fcpath.'uploads'.directory_separator;
image resizer
/** * resizes image fit exact dimensions * * @param string filename * @param int target_width * @param int target_height * * @return array('success' ? null : 'error') */ function resizeimagetodimensions($filename, $target_width=700, $target_height=399) { $file_type = $this->getfiletype($this->prefix.$filename); if (!$file_type || $file_type != 'image') return array('success'=>false, 'error'=>"this file doesn't exist or isn't image"); $this->load->library('image_lib'); list($width, $height) = getimagesize($this->prefix.$filename); $current_ratio = $width/$height; $target_ratio = $target_width/$target_height; $config['source_image'] = $this->prefix.$filename; if ($current_ratio > $target_ratio) { //resize first height, maintain ratio $config['height'] = $target_height; $config['width'] = $target_height * $current_ratio; $this->image_lib->initialize($config); if (!$this->image_lib->resize()) return array('success'=>false, 'error'=>"there error while resizing image"); //then crop off width $config['width'] = $target_width; $config['maintain_ratio'] = false; $this->image_lib->initialize($config); if ($this->image_lib->crop()) return array('success'=>true); else return array('success'=>false, 'error'=>"there error while cropping image"); } else if ($current_ratio < $target_ratio) { //resize first width, maintain ratio $config['width'] = $target_width; $config['height'] = $target_width / $current_ratio; $this->image_lib->initialize($config); if (!$this->image_lib->resize()) return array('success'=>false, 'error'=>"there error while resizing image"); //then crop off height $config['height'] = $target_height; $config['maintain_ratio'] = false; $this->image_lib->initialize($config); if ($this->image_lib->crop()) return array('success'=>true); else return array('success'=>false, 'error'=>"there error while cropping image"); } else { $config['width'] = $target_width; $config['height'] = $target_height; $this->image_lib->initialize($config); if ($this->image_lib->resize()) return array('success'=>true); else return array('success'=>false, 'error'=>"there error while resizing image"); } }
Comments
Post a Comment