CodeIgniter Forums
Code Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Code Question (/showthread.php?tid=65503)



Code Question - wolfgang1983 - 06-20-2016

Hello, I have a model function that I have been working on every thing seems to be working fine

I would like to no should I make a if statement for my return base_url() at the bottom of page.

If so what would you recommend?

PHP Code:
<?php

class Model_image_resize extends CI_Model {

    public function 
__construct() {
        
parent::__construct();
        
$this->load->library('image_lib');
    }

    public function 
resize($image$width$height) {
        
$old_image_path FCPATH 'images/';
        
$new_image_path FCPATH 'images/cache/';
        
$old_image  $old_image_path $image;
        
$img '';
        
$extension  pathinfo($old_imagePATHINFO_EXTENSION);
        
$old_image_convert substr($image0 strrpos($image'.')) . '-' $width 'x' $height '.' $extension;
        
$new_image $new_image_path $old_image_convert;

        if (
file_exists($old_image)) {

         
  $config['image_library'] = 'gd2';
         
  $config['source_image'] = $old_image;
         
  $config['create_thumb'] = FALSE;
         
  $config['maintain_ratio'] = FALSE;
         
  $config['width'] = $width;
         
  $config['height'] = $height;
         
  $config['new_image'] = $new_image;

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

     
             $files explode($new_image_path$new_image);

     
             foreach ($files as $file) {
     
               $img  $file;
     
             }
     
              
           
} else {

 
              @unlink($new_image);

 
          }
 
          
           
return base_url('images/cache') . '/' $img;
    }




RE: Code Question - PaulD - 06-20-2016

Hi,

You should check it has been set before generating a URL with it.

I would have made this into a helper. If called I would return simply either TRUE or FALSE if resized image was created. I would also have put the location of the image either as a config item, or an option in the method.

Within this helper I would also have another function called something like 'show_image', which checks that the image exists, and returns a url to it. But, not the base_url bit, that part is for the view to add. If it did not exist, it could then call the function above to create the image as a private function.

I also always follow the naming convention of adding _model, _view, _helper to the end of my class names. So your model here would be Image_resize_model.php
However, that is all opinion, as naming conventions are not enforced, and the beauty of CI is you can do it however you want.

Good job though,

Paul.