/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class Upload_image { private $ci; private $upload_path; private $copies; private $allowed_types; private $max_size; private $input; { $this->ci =& get_instance(); $this->ci->load->library('upload'); $this->ci->load->library('image_lib'); { $this->initialize($props); } } { 'allowed_types' => 'jpg|jpeg|gif|png', 'max_size' => '2000', 'input' => 'picture', 'upload_path' => '' ); foreach ($defaults as $key => $val) { } } function upload() { $config['upload_path'] = $this->upload_path; $config['allowed_types'] = $this->allowed_types; $config['max_size'] = $this->max_size; $this->ci->upload->initialize($config); if ($this->ci->upload->do_upload($this->input)) { $upload = $this->ci->upload->data(); // Set image name while (true) { } // Creating image copies { foreach ($this->copies as $copy) { 'source_path' => $upload['file_path'], 'copy' => $copy); $this->resize($resize_config); } } return $img_name; } return false; } function resize($init) { 'new_image' => $init['copy']['upload_path'].$init['file_name']); list($img['image_width'], $img['image_height'], $img['type'], $img['attr']) = getimagesize($config['source_image']); if ($init['copy']['crop']) { // Resize to make smaller edge = selected size if ($img['image_width'] > $img['image_height']) { $config['height'] = $init['copy']['height']; $ratio = $init['copy']['height'] / $img['image_height']; $config['width'] = $img['image_width'] * $ratio; } else { $config['width'] = $init['copy']['width']; $ratio = $init['copy']['width'] / $img['image_width']; $config['height'] = $img['image_height'] * $ratio; } $this->ci->image_lib->initialize($config); $this->ci->image_lib->resize(); $config['width'] = $init['copy']['width']; $config['height'] = $init['copy']['height']; $config['maintain_ratio'] = false; $config['source_image'] = $config['new_image']; $this->ci->image_lib->initialize($config); $this->ci->image_lib->crop(); } else { // Width adjustment if ($img['image_width'] > $init['copy']['width']) { $config['width'] = $init['copy']['width']; $ratio = $init['copy']['width'] / $img['image_width']; $config['height'] = $img['image_height'] * $ratio; } // Height adjustment if ($config['height'] > $init['copy']['height']) { $config['height'] = $init['copy']['height']; $ratio = $init['copy']['height'] / $img['image_height']; $config['width'] = $img['image_width'] * $ratio; } $this->ci->image_lib->initialize($config); $this->ci->image_lib->resize(); } } } // End of file // Using $this->load->library('upload_image'); $upload_path = './uploads/property/'; $copies[] = array('upload_path' => $upload_path.'mid/', 'width' => 90, 'height' => 75, 'crop' => false); $copies[] = array('upload_path' => $upload_path.'small/', 'width' => 50, 'height' => 50, 'crop' => true); $config = array('upload_path' => $upload_path.'original/', 'input' => 'picture', 'copies' => $copies); $this->upload_image->initialize($config); $result = $this->upload_image->upload(); echo $result ? 'Image '.$result.' was uploaded successfully' : 'Image was not uploaded';