CodeIgniter Forums

Full Version: Image Crop not working.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]sb05[/eluser]
Hey guys, for some reason my image crop is not working. I've tried a bunch of stuff, and in the end I put the cropping methods in my controller and also in my model. Which is probably a bad idea, but I just wanted to make sure that the thing at least gets something. But Im stuck again. So hope you guys see the problem.

Here is my controller:

Code:
function addAd(){
        // set common properties
        $data['title'] = 'Add new ad';
        $data['action'] = site_url('ads/addAd');
        $data['link_back'] = anchor('ads','Back to list of ads',array('class'=>'back'));
        
        // set validation properties
        $this->_set_fields();
        $this->_set_rules();

        
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '10000';
            $config['max_width']  = '1000000';
            $config['max_height']  = '10000000';
            $config['upload_path'] = './ad_images';
            $config['x_axis'] = '100';
            $config['y_axis'] = '100';

            $this->load->library('image_lib', $config);
            
            $this->image_lib->initialize($config);

            if ( ! $this->image_lib->crop())
                    {
                        echo $this->image_lib->display_errors();
                    }

            

            $path_to_uploads='./ad_images';
            
            
        //    $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

                       if (!$this->upload->do_upload()){
                            $error = $this->upload->display_errors();
                            echo $error;
                        }else{
                            $upload_data=$this->upload->data();
                            $file_name=$upload_data['file_name'];
                            $full_file_path = $path_to_uploads.'/'.$file_name;
                        }
        
        // run validation
        
        if ($this->validation->run() == TRUE){
            $data['message'] = 'It is not going to the database';
        }else{
            // save data
            $ad = array('name' => $this->input->post('name'),
                            'image_url' => $full_file_path,
                            'link' => $this->input->post('link'),
                            'client_name' => $this->input->post('client_name'))

                         ;
            $ad_id = $this->adModel->save($ad);
            
            // set form input name="id"
            $this->validation->ad_id = $ad_id;
            
            // set user message
            $data['message'] = '<div class="success">add new ad success</div>';
        }
                $data['ads'] = $this->adModel->get_by_id($ad_id)->row();

        
        // load viewinclude 'person.php';
        
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('adEdit', $data);
            $this->load->view('footer_view', $data);    }

And my model:
Code:
function do_upload() {
    

        $this->gallery_path = realpath(APPPATH . '../ad_images');

        
        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->gallery_path,
            'max_size' => 200000,
            'maintain_ration' => true,
            'width' => 100000,
            'height' => 100000,
            'x_axis'=> 100,
            'y_axis'=> 100

            // $config['x_axis'] = '100';
            //$config['y_axis'] = '100';
        );
         $this->load->library('image_lib', $config);

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

            if ( ! $this->image_lib->crop())
                    {
                        echo $this->image_lib->display_errors();
                    }
                    
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('image_url')){
                $error = $this->upload->display_errors();
                echo $error;
            }
        
            $upload_data = $this->upload->data();
        
            $file_name=$upload_data['image_url'];
        $full_file_path = $path_to_uploads.'/'.$file_name;
    }
    
}

So basically the uploading works, but not the cropping and I also don't get an error that the cropping didn't work.

El Forum

[eluser]Mat-Moo[/eluser]
Try my image_moo library, in my sig Smile makes life much easier.

El Forum

[eluser]sb05[/eluser]
Ok, I will try it Smile

tnx! Hope it'll work

El Forum

[eluser]sb05[/eluser]
Ok, I tried the image_moo library, but I'm stuck at the same problem. The simple fact is, it's not picking up the methods, because I am also not getting the error messages on display. So I must be placing the code in the wrong place??
I put the code in my upload function, because I want the uploaded images to be cropped, right? But it's not working not the image_lib nor the image_moo library gets activated.

Here is my model:
Code:
function do_upload() {
    

        $this->gallery_path = realpath(APPPATH . '../ad_images');

        
        $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->gallery_path,
            'max_size' => 200000,
            'maintain_ration' => true,
            'width' => 100000,
            'height' => 100000,
            'x_axis'=> 100,
            'y_axis'=> 100

            // $config['x_axis'] = '100';
            //$config['y_axis'] = '100';
        );
        
                    
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('image_url')){
                $error = $this->upload->display_errors();
                echo $error;
            }
        
        $upload_data = $this->upload->data();
        $file_name=$upload_data['image_url'];
        $full_file_path = $path_to_uploads.'/'.$file_name;
        
        
        $this->load->library('image_lib', $config);

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

            if ( ! $this->image_lib->crop())
                    {
                        echo $this->image_lib->display_errors();
                    }
        
        $this->load->library("image_moo");
        
               if($this->image_moo->errors)
               print $this->image_moo->display_errors();
      
      
       $this->image_moo->load('image_url')
            ->resize(100,100)
            ->save_dynamic();
    }
    
}
?&gt;

And here is my Controller:
[code]
function addAd(){
        // set common properties
        $data['title'] = 'Add new ad';
        $data['action'] = site_url('ads/addAd');
        $data['link_back'] = anchor('ads','Back to list of ads',array('class'=>'back'));
        
        // set validation properties
        $this->_set_fields();
        $this->_set_rules();

        
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '10000';
            $config['max_width']  = '1000000';
            $config['max_height']  = '10000000';
            $config['upload_path'] = './ad_images';
            $config['x_axis'] = '100';
            $config['y_axis'] = '100';

          

            $path_to_uploads='./ad_images';
            
            
        //    $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

                       if (!$this->upload->do_upload()){
                            $error = $this->upload->display_errors();
                            echo $error;
                        }else{
                            $upload_data=$this->upload->data();
                            $file_name=$upload_data['file_name'];
                            $full_file_path = $path_to_uploads.'/'.$file_name;
                        }
      


            
        // run validation
        
        if ($this->validation->run() == TRUE){
            $data['message'] = 'It is not going to the database';
        }else{
            // save data
            $ad = array('name' => $this->input->post('name'),
                            'image_url' => $full_file_path,
                            'link' => $this->input->post('link'),
                            'client_name' => $this->input->post('client_name'))

                         ;
            $ad_id = $this->adModel->save($ad);
            
            // set form input name="id"
            $this->validation->ad_id = $ad_id;
            
            // set user message
            $data['message'] = '<div class="success">add new ad success</div>';
        }
                $data['ads'] = $this->adModel->get_by_id($ad_id)->row();

        
        // load viewinclude 'person.php';
        
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('adEdit', $data);
            $this->load->view('footer_view', $data);    }

Please help Sad

El Forum

[eluser]Mat-Moo[/eluser]
Code:
$this->load->library("image_moo");
      $this->image_moo->load($full_file_path)
            ->resize(100,100)
            ->save_dynamic();
      if($this->image_moo->errors) print $this->image_moo->display_errors();

You should read/write your code more carefully - this->image_moo->load('image_url')??!!

El Forum

[eluser]sb05[/eluser]
thanks a lot Mat-MooSmile
it's not that I should read/write my code more carefully, it's that I'm still new at PHP and Codeigniter, but I'm a step closer now. So the image crop worked, but a new problem has arisen.
Now when I upload my image I get a white page with the cropped image at the top left. But the same URL should have displayed my site again where the cropped image is in the left column as an Ad image.
So have I placed the code in the wrong place? First I tried to add the code in my do_upload function of my model. But that didnt work. But it did work in my controller.

Here is my new controller:
Code:
function addAd(){
        // set common properties
        $data['title'] = 'Add new ad';
        $data['action'] = site_url('ads/addAd');
        $data['link_back'] = anchor('ads','Back to list of ads',array('class'=>'back'));
        
        // set validation properties
        $this->_set_fields();
        $this->_set_rules();

        
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '10000';
            $config['max_width']  = '1000000';
            $config['max_height']  = '10000000';
            $config['upload_path'] = './ad_images';  
          

          

            $path_to_uploads='./ad_images';
            
            
            
            
        //    $config['upload_path'] = $path_to_uploads;
                $this->load->library('upload', $config);

                       if (!$this->upload->do_upload()){
                            $error = $this->upload->display_errors();
                            echo $error;
                        }else{
                            $upload_data=$this->upload->data();
                            $file_name=$upload_data['file_name'];
                            $full_file_path = $path_to_uploads.'/'.$file_name;
                        }
$this->load->library("image_moo");
      $this->image_moo->load($full_file_path)
            ->resize_crop(100,100)
            ->save_dynamic();
      if($this->image_moo->errors) print $this->image_moo->display_errors();

            
        // run validation
        
        if ($this->validation->run() == TRUE){
            $data['message'] = 'It is not going to the database';
        }else{
            // save data
            $ad = array('name' => $this->input->post('name'),
                            'image_url' => $full_file_path,
                            'link' => $this->input->post('link'),
                            'client_name' => $this->input->post('client_name'))

                         ;
            $ad_id = $this->adModel->save($ad);
            
            // set form input name="id"
            $this->validation->ad_id = $ad_id;
            
            // set user message
            $data['message'] = '<div class="success">add new ad success</div>';
        }
                $data['ads'] = $this->adModel->get_by_id($ad_id)->row();

        
        // load viewinclude 'person.php';
        
            $this->load->view('banner_view', $data);
            $this->load->view('left_column_view', $data);
            $this->load->view('right_column_view', $data);
            $this->load->view('adEdit', $data);
            $this->load->view('footer_view', $data);    }

So as you can see. My /addad url should have loaded some views, but now it only displays the cropped image.
Should I make a new variable that contains the new cropped image? Did I put the code in the right place?

El Forum

[eluser]Mat-Moo[/eluser]
Using "save_dynamic" will output the image straight to the browser. You should consider saving the image locally, then you can place it using one of your views.

El Forum

[eluser]sb05[/eluser]
thanks Moo! i will try it out.

El Forum

[eluser]sb05[/eluser]
Cool, it worked!

Code:
$this->load->library('upload', $config);

                           if (!$this->upload->do_upload()){
                            $error = $this->upload->display_errors();
                            echo $error;
                        }else{
                            $upload_data=$this->upload->data();
                            $file_name=$upload_data['file_name'];
                            $full_file_path = $path_to_uploads.'/'.$file_name;
                        }

                 $this->load->library("image_moo");
                  $this->image_moo->load($full_file_path)
                ->resize_crop(100,100)
                ->save($full_file_path, $overwrite=TRUE);
          
                          if($this->image_moo->errors)
                          print $this->image_moo->display_errors();

Thanks again Smile

El Forum

[eluser]Mat-Moo[/eluser]
Great, fyi you don't need the $overwrite=TRUE, just TRUE will do.
Pages: 1 2