Welcome Guest, Not a member yet? Register   Sign In
[beginner] creating thumbnails
#1

[eluser]rijobo[/eluser]
Hello,

I'm uploading pictures, but I also want to create thumbnails. I've found the image_lib class, nut I'm not sure how it works. Can someone help me with what I should add to also create a thumbnail in for example the folder uploads/thumbs and with the same filename.

I've got this in my controller:

Code:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'png|gif|jpg';
$config['max_size']  = '10000';
$upload_veld = 'foto';
$data['soort'] = $this->Productupload->SelectieSoort('soort', 'soort');
$data['steen'] = $this->Productupload->SelectieSoort('steen', 'steen');
$data['materiaal'] = $this->Productupload->SelectieSoort('materiaal', 'materiaal');

$this->load->library('upload', $config);
$this->load->library('form_validation');
$this->load->view('product_toevoegen', $data);
        
if($this->upload->do_upload($upload_veld)) {
$this->form_validation->set_rules('naam', 'Naam', 'required');
$this->form_validation->set_rules('omschrijving', 'Omschrijving', 'required');
$this->form_validation->set_rules('prijs', 'Prijs', 'required');
$this->form_validation->set_rules('afmeting', 'Afmeting', 'required');

$upload_data = $this->upload->data();

$product = array('naam'=>$this->input->post('naam'), 'omschrijving'=>$this->input->post('omschrijving'), 'foto'=>$upload_data['file_name'], 'soort'=>$this->input->post('soort'), 'steen'=>$this->input->post('steen'), 'prijs'=>$this->input->post('prijs'), 'afmeting'=>$this->input->post('afmeting'), 'materiaal'=>$this->input->post('materiaal'));
            
if ($this->form_validation->run() == FALSE)    {
$this->load->view('product_toevoegen');
}
else
{    
$this->Productupload->insertRow('product', $product);
$this->load->view('product_succes');
}
        
} else {
$data['error'] = $this->upload->display_errors();
}
#2

[eluser]jtkendall[/eluser]
First copy your originally uploaded file to your thumbs directory and then use the example in the user guide. http://ellislab.com/codeigniter/user-gui...e_lib.html
#3

[eluser]rijobo[/eluser]
Should I upload the image twice? One in the uploads folder and one in the thumbs folder en then resize it?
And if so, how can I upload one picture in 2 folders?

I did try this, but that didn't seem to work. Is this also a right way?

Code:
function index()
    {
        
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'png|gif|jpg';
        $config['max_size']  = '10000';
        $upload_veld = 'foto';
        $data['soort'] = $this->Productupload->SelectieSoort('soort', 'soort');
        $data['steen'] = $this->Productupload->SelectieSoort('steen', 'steen');
        $data['materiaal'] = $this->Productupload->SelectieSoort('materiaal', 'materiaal');
                        
        $this->load->library('upload', $config);
        $this->load->library('form_validation');
        $this->load->view('product_toevoegen', $data);
        
        if($this->upload->do_upload($upload_veld)) {
            $this->form_validation->set_rules('naam', 'Naam', 'required');
            $this->form_validation->set_rules('omschrijving', 'Omschrijving', 'required');
            $this->form_validation->set_rules('prijs', 'Prijs', 'required');
            $this->form_validation->set_rules('afmeting', 'Afmeting', 'required');
            $upload_data = $this->upload->data();
            $product = array('naam'=>$this->input->post('naam'), 'omschrijving'=>$this->input->post('omschrijving'), 'foto'=>$upload_data['file_name'], 'soort'=>$this->input->post('soort'), 'steen'=>$this->input->post('steen'), 'prijs'=>$this->input->post('prijs'), 'afmeting'=>$this->input->post('afmeting'), 'materiaal'=>$this->input->post('materiaal'));
            $configthumb['image_library'] = 'gd2';
            $configthumb['source_image'] = './uploads/'.$product['foto'];
            $configthumb['create_thumb'] = TRUE;
            $configthumb['maintain_ratio'] = TRUE;
            $configthumb['width'] = 75;
            $configthumb['height'] = 50;
            $configthumb['new_image'] = './uploads/thumbs/';
            
            $this->load->library('image_lib', $configthumb);
            $this->image_lib->resize();
            
                
            if ($this->form_validation->run() == FALSE)    {
                $this->load->view('product_toevoegen');
            }
            else
            {    
                $this->Productupload->insertRow('product', $product);
                $this->load->view('product_succes');
            }
        
        } else {
            $data['error'] = $this->upload->display_errors();
        }
    }
}
#4

[eluser]jtkendall[/eluser]
Ok, you're code looks fine, though you can remove the first set of "configthumb" variables as you don't use them before you set them again. In your second set of "configthumb" variables you have the new_image item set, however you need to specify the name to save it as. So if you want it to be the same name as the original you need to change it to $configthumb['new_image'] = './uploads/thumbs/'.$product['foto'];
#5

[eluser]rijobo[/eluser]
I've changed it, but it still doesn't upload my thumbnail. The normal picture is uploaded in my uploads folder, but my thumbs folder is empty.

I've also tried $configthumb['new_image'] = './uploads/thumbs/'.$upload_data['file_name'];

But this also didn't work. I don't see what I do wrong.
#6

[eluser]jtkendall[/eluser]
change "$this->image_lib->resize()" to

if(!$this->image_lib->resize()){
die($this->image_lib->display_errors());
}

That should tell you what's wrong.
#7

[eluser]rijobo[/eluser]
That's it thank you very much. It was a not writable directory.
I've got one more small question. The name of the original picture is hanger51.jpg. The name of the thumb is hanger51_thumb.jpg.
I've got a table with the name product and a column with the name foto. In this foto column I see the name of my original picture hanger51.jpg.
In my view I use this to see the picture:

Code:
<img name="&lt;?php $product['naam'];?&gt;" border="0" src="../../../uploads/&lt;?php echo $product['foto'];?&gt;">

What should I do when I want to see the thumbnail?
#8

[eluser]jtkendall[/eluser]
Changing your code to this should show the thumbnail instead of the original.

Code:
<img name="&lt;?php $product['naam'];?&gt;" border="0" src="../../../uploads/thumbs/&lt;?php echo str_replace('.', '_thumb.', $product['foto']);?&gt;">
#9

[eluser]rijobo[/eluser]
Great! Thanks it works!




Theme © iAndrew 2016 - Forum software by © MyBB