Welcome Guest, Not a member yet? Register   Sign In
Image up/resize good practice
#1

[eluser]Felipe Deitos[/eluser]
Hi guys!
I am doing my first site in CI to a company.
CI is the best thing i ever worked with... i am trying to keep everything as clean as possible and do everything with good practices.
I am developing my first project in CI to a company.
I got a lot of thing already done, the one i like to discuss here is the news system...

1) First thing i want to know if my db structure is a good way of doing this...
I store the new in one and in other table as many images i want...

Ex:
(table)news
-id
-title
-content

(table)news_imgs
-id
-new_id
-img

This is a good table structure to store how many pictures i want in one new?
There are better ways to do this?

2) Second thing is my code... i used the image manipulation for the first time, is working everything fine but as i said above i want to do the best i can...

Controller:
Code:
public function image_upload()
{
  //if the user is admin Just to u guys know
  $this->is_admin();
  
  $this->load->model('dicas_model');
  
  //Se o post for verdadeiro inicia a função do model
  if($this->input->post('inserir'))
  {
  
   $data = $this->dicas_model->image_upload();
  
   if($data['status'])
   {
    echo 'Yei everythings fine';
   }
   else
   {
    echo $data['error'];
   }
  }
  
}

Model: (yes i make 3 pictures from one hehehe)
Code:
public function image_upload()
{
  $config = array(
   'allowed_types' => 'gif|jpg|png',
   'upload_path' => $this->image_folder,
   'max_size' => 4096
  );
    
  $this->load->library('upload', $config);
  
  //Se deu tudo certo inserindo a imagem
  if ($this->upload->do_upload('imagem'))
  {
   //Todas informações da imagem
   $image_data = $this->upload->data();
  
   $this->load->library('image_lib');
  
   //resize imagem
   $config = array(
    'image_library' => 'gd2',
    'source_image' => $image_data['full_path'], //contém o caminho completo da imagem
    'maintain_ratio' => TRUE,
    'width' => 1024,
    'height' => 768
   );
  
   $this->image_lib->initialize($config);
   $this->image_lib->resize();
   $this->image_lib->clear();
  
   //thumb imagem
   $config = array(
    'image_library' => 'gd2',
    'source_image' => $image_data['full_path'],
    'new_image' => $image_data['file_path'] ."thumb_". $image_data['file_name'],
    'maintain_ratio' => TRUE,
    'master_dim' => 'width',
    'width' => 256,
    'height' => 192
   );
  
   $this->image_lib->initialize($config);
   $this->image_lib->resize();
   $this->image_lib->clear();
  
   //crop imagem 128x128
   $config = array(
    'image_library' => 'gd2',
    'source_image' => $image_data['file_path'] ."thumb_". $image_data['file_name'],
    'new_image' => $image_data['file_path'] ."crop_". $image_data['file_name'],
    'maintain_ratio' => FALSE,
    'width' => 128,
    'height' => 128,
    'x_axis' => 0,
    'y_axis' => 0
   );
  
   $this->image_lib->initialize($config);
   $this->image_lib->crop();
   $this->image_lib->clear();
  
   //Insere id, id da dica e nome da imagem principal no ba
   $registro = array(
    'dica_id' => $this->input->post('dica_id'),
    'img' => $image_data['file_name']
   );
  
   $data['status'] = $this->db->insert('dicas_imgs', $registro);
   return $data;
  }
  else
  {
   $data['status'] = false;
   $data['error'] = $this->upload->display_errors();
   return $data;
  }
}


Hope u guys can understand me and have something to make my coding better...
Just need some tips, need to know if i am on the right way to be a good CI developer hehehehe
And by the way sorry bout my english cuz i am from Brasil

Cheeers everyone!
#2

[eluser]yacman[/eluser]
You are on the right track. Storing the image files to disk, catalog their locations and correctly utilizing CI's built in libraries.
#3

[eluser]Felipe Deitos[/eluser]
Really nice to know that i am on the right way, i forgot to put the construct of my model yesterday...
so i am posting now... if anyone like the code, feel free to use it

here goes the contruct of the model:
Code:
var $image_folder;

function __construct()
    {
        // Chama o construct do model
        parent::__construct();
  
  $this->image_folder = realpath(APPPATH . '../assets/images/dicas');
    }




Theme © iAndrew 2016 - Forum software by © MyBB