Welcome Guest, Not a member yet? Register   Sign In
Latavish's Multiple Image Upload with Thumbnail Generation
#21

[eluser]Unknown[/eluser]
[quote author="Santiag0" date="1240604670"]The original code not work for me, but here is my simplified working adaptation

Code:
class multiupload extends Controller
{

function index()
{
            $config['upload_path'] = './content/img/photos';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']    = '2048'; //2 meg
            $config['encrypt_name']    = TRUE;
            $this->load->library('upload', $config);
            $this->load->library('image_lib');
            //Upload error flag
            $error = FALSE;

            foreach($_FILES as $key => $value)
            {
                if( !empty($value['name']))
                {
                    if ( $this->upload->do_upload($key) )
                    {
                        $uploaded = $this->upload->data();

                        //Creat Thumbnail
                        $config['image_library'] = 'GD2';
                        $config['source_image'] = $uploaded['full_path'];
                        $config['create_thumb'] = TRUE;
                        $config['thumb_marker'] = '_tn';
                        $config['master_dim'] = 'width';
                        $config['quality'] = 75;
                        $config['maintain_ratio'] = TRUE;
                        $config['width'] = 175;
                        $config['height'] = 175;

                        $this->image_lib->clear();
                        $this->image_lib->initialize($config);
                        $this->image_lib->resize();

                        $imagename = $uploaded['file_name'].'_tn'.$uploaded['file_ext'];
                        $timestamp = time();

                        //Add Pic Info To Database
                        $this->db->set('file', $imagename);
                        //$this->db->set('date', $timestamp);

                        //Insert Info Into Database
                        $this->db->insert('photos');
                    }
                    else
                    {
                        $error = TRUE;
                    }
                }
            }
//If we have some error...
if($error) $this->session->set_flashdata('notice', '<div class="error icon"><ol>'.$this->upload->display_errors('<li>','</li>').'</ol></div>');

else $this->session->set_flashdata('notice', '<p class="success icon">¬°Success!</p>');

//Call the view
$this->load->view('upload_photos');
}
}
View file

Code:
&lt;?php
echo $this->session->flashdata('notice');
echo '<h3>Upload multiple photos</h3>';
echo form_open_multipart('multiupload');
?&gt;
  &lt;input type="file" name="p1" size="20" /&gt;
    &lt;input type="file" name="p2" size="20" /&gt;
    &lt;input type="file" name="p3" size="20" /&gt;
<button type="submit" name="send-photos" id="send-photos">Send</button>
&lt;/form&gt;

Enjoy and say 'thaks' if you find handy Wink

Thanks![/quote]

This code works great BUT must be correct the
Code:
$imagename = $uploaded['file_name'].'_tn'.$uploaded['file_ext'];
variable because produce duplicate extension (image.jpg_tn.jpg) in the saved image name.

The little fix is:
Code:
$imagename = substr($uploaded['file_name'], 0, -4)."_tn".$uploaded['file_ext'];
i search for a better way to do the last code but im very very tired now so... if someone knows is time to share :-)

Bye.


Messages In This Thread
Latavish's Multiple Image Upload with Thumbnail Generation - by El Forum - 11-19-2009, 12:03 AM



Theme © iAndrew 2016 - Forum software by © MyBB