views:

1573

answers:

2

I think this function isn't as efficient as it should be. I'd appreciate some suggestions on how I can structure it to be faster and take less memory. This is what the code does:

  1. checks to see if the image was uploaded
  2. add details about it (tags, name, details) to the database
  3. if the variable $orientation was set, rotate the image
  4. if the image is wider than 600px, resize it
  5. create a thumbnail

I think the inefficiencies come from having steps 3,4,5 all separate. Is there any way to consolidate them? Thanks!

 function insert()
  {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg';
    $config['max_size'] = '5000';
    $config['max_width']  = '4096';
    $config['max_height']  = '4096';

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

    if (!$this->upload->do_upload())
    {
      $data = array('error' => $this->upload->display_errors());

      $data['title'] = "Add Photo | Mark The Dark";
      $this->load->view('photo_add_view', $data);
    }   
    else
    {
      //get uploaded image info
      $data = array('upload_data' => $this->upload->data());

      //clean the data
      $data = $this->input->xss_clean($data);

      //get orientation info and erase it from POST variable
      //$orientation = $_POST['orientation'];
      //unset($_POST['orientation']);

      //grab the tags
      $tags = $_POST['tags'];
      unset($_POST['tags']);

      //add in some other stuff
      $_POST['time'] = date('YmdHis');
      $_POST['author'] = $this->dx_auth->get_user_id();

      //insert it in the database
      $this->db->insert('photos', $_POST);
      $photo_id = $this->db->insert_id();

      //add stuff to tags table
      /*
      $tags_array = preg_split('/[\s,;]+/', $tags);
      foreach($tags_array as $tag)
      {
        if($tag != "" || $tag != null)
          $this->db->insert('tags', array('id' => $photo_id, 'word' => $tag));
      }*/

      //CXtags
      /*$tags_array = preg_split('/[\s,;]+/', $tags);
      foreach($tags_array as $tag)
      {
        if($tag == "" || $tag == null)
        {unset($tags_array[$tag]);}
      }
      */
      $tags_array = $this->CXTags->comma_to_array($tags);
      foreach($tags_array as $tag)
      {$tags_array[$tag] = $this->CXTags->make_safe_tag($tag);}
      $topass = array(
        'table' => 'photos',
        'tags' => $tags_array,
        'row_id' => $photo_id,
        'user_id' => $_POST['author']
      );
      $this->CXTags->add_tags($topass);

      //rename the file to the id of the record in the database
      rename("./uploads/" . $data['upload_data']['file_name'], "./uploads/" . $photo_id . ".jpg");

      list($width, $height, $type, $attr) = getimagesize("./uploads/" . $photo_id . '.jpg');


      if (($orientation == 1) || ($orientation == 2))
      {
        //echo $orientation;
        //rotate image
        $config['image_library'] = 'GD2';
        $config['source_image'] = './uploads/' . $photo_id . '.jpg';
        if ($orientation == 1)
        {
          $config['rotation_angle'] = 270;
        }
        elseif ($orientation == 2)
        {
          $config['rotation_angle'] = 90;
        }
        $this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        if(!$this->image_lib->rotate())
        {
          echo $this->image_lib->display_errors();
        }
      }


      $this->load->library('image_lib');
      if ($width > 600)
      {
        //resize image
        $config['image_library'] = 'GD2';
        $config['source_image'] = './uploads/' . $photo_id . '.jpg';
        $config['new_image'] = './uploads/photos/' . $photo_id . '.jpg';
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 600;//180
        $config['height'] = 480;
        $config['master_dim'] = 'width';
        $this->image_lib->initialize($config);
        $this->load->library('image_lib', $config);
        if(!$this->image_lib->resize())
        {
          echo $this->image_lib->display_errors();
        }
      }
      else
      {

        $source = './uploads/' . $photo_id . '.jpg';
        $destination = './uploads/photos/' . $photo_id . '.jpg';
        rename($source, $destination);
      /*//buggy php???
        $result = copy($source, $destination);
        echo "HO" . $result;
        */

      }

      //create thumbnail
      $config['image_library'] = 'GD2';
      $config['source_image'] = './uploads/photos/' . $photo_id . '.jpg';
      $config['new_image'] = './uploads/thumbnails/' . $photo_id . '.jpg';
      $config['create_thumb'] = TRUE;
      $config['thumb_marker'] = '_thumb';
      $config['maintain_ratio'] = TRUE;
      $config['width'] = 180;//180
      $config['height'] = 100;
      $config['master_dim'] = 'width';
      $this->image_lib->initialize($config);
      $this->load->library('image_lib', $config);
      $this->image_lib->resize();

      redirect('photo/show/' . $photo_id);

    }

    //redirect('photo/photo_add/');
  }
A: 

You could store your configs elsewhere per codeigniter documentation, that would tighten the code up. I don't know how you could chain those events.

MrChrister
+1  A: 

For all the work required to do your image manipulation using the objects that code igniter exposes for you, you may want to look into doing this yourself strictly with the gd functions in php. I have not used code igniter, but I've done a lot of stuff with image manipulation in php, and it's not terribly difficult.

Just by looking at your code, and I'm assuming this is the code igniter way, I see you calling image_lib->initialize() and load->library() for each individual manipulation. I'd have a look inside those methods and the rotate() and resize() methods you're using and see if they're creating and destroying the image resource with each manipulation. If you use the gd library, you can reuse the same image resource for each step and then just write it to file when you're ready (re-use the same image resource to create the thumbnail). This would probably make a huge performance gain in both execution speed and memory used.

GD Functions in PHP

Rich