Upload image to module folder in assets

edited May 2014 in Modules
Hi, I am looking to upload images within a certain module to that specific folder within assets/images/{module}

When the user uploads the image within the admin screen, I want this path to be auto selected - rather than the using having to find it from the drop down menu (its easy to forget to do so).

Here is my code, but for some reason its not quite working:

function form_fields($values = array()) { function form_fields($values = array()) { $fields = parent::form_fields($values); // to limit the image folder to just the projects folder for selection $fields['image']['class'] = 'asset_select images/partners'; //$fields['description']['class'] = 'wysiwyg'; // put all project images into a projects subfolder $fields['image_upload']['upload_path'] = assets_server_path('partners', 'images'); // fix the preview by adding projects in front of the image path since we are saving it in a subfolder if (!empty($values['image'])) { $fields['image_upload']['before_html'] = '<div class="img_display"><img src="'.img_path('partners/'.$values['image']).'" style="float: right;"/></div>'; } return $fields; } function on_after_post($values) { $CI =& get_instance(); $CI->load->library('image_lib'); // create the thumbnail if an image is uploaded if (!empty($CI->upload)) { $data = $CI->upload->data(); if (!empty($data['full_path'])) { $thumb_img = assets_server_path('partners/'.$this->thumb_name($data['file_name']), 'images'); // resize to proper dimensions $config = array(); $config['source_image'] = $data['full_path']; $config['create_thumb'] = FALSE; //$config['new_image'] = $thumb_img; $config['width'] = 240; $config['height'] = 140; $config['master_dim'] = 'auto'; $config['maintain_ratio'] = TRUE; $CI->image_lib->clear(); $CI->image_lib->initialize($config); if (!$CI->image_lib->resize()) { $this->add_error($CI->image_lib->display_errors()); } // create thumb $config = array(); $config['source_image'] = $data['full_path']; $config['create_thumb'] = TRUE; $config['new_image'] = $thumb_img; $config['width'] = 300; $config['height'] = 183; $config['master_dim'] = 'auto'; $config['maintain_ratio'] = TRUE; $CI->image_lib->clear(); $CI->image_lib->initialize($config); if (!$CI->image_lib->resize()) { $this->add_error($CI->image_lib->display_errors()); } } } return $values; } // delete the images as well function on_before_delete($where) { $id = $this->_determine_key_field_value($where); $data = $this->find_by_key($id); $files[] = assets_server_path('partners/'.$data->image, 'images'); $files[] = assets_server_path('partners/'.$this->thumb_name($data->image), 'images'); foreach($files as $file) { if (file_exists($file)) { @unlink($file); } } } function thumb_name($image) { return preg_replace('#(.+)(\.jpg|\.png)#U', '$1_thumb$2', $image); } }

Any suggestions? Thanks!

Comments

  • edited 9:59AM
    Instead of:
    $fields['image']['class'] = 'asset_select images/partners';

    Try:
    $fields['image']['folder'] = 'images/partners';
  • edited 9:59AM
    Solved it! Thank you
Sign In or Register to comment.