Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I modify this image manipulation function done in codeigniter to be more efficient
    text
    copied!<p>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:</p> <ol> <li>checks to see if the image was uploaded</li> <li>add details about it (tags, name, details) to the database</li> <li>if the variable $orientation was set, rotate the image</li> <li>if the image is wider than 600px, resize it</li> <li>create a thumbnail</li> </ol> <p>I think the inefficiencies come from having steps 3,4,5 all separate. Is there any way to consolidate them? Thanks!</p> <pre><code> function insert() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'jpg'; $config['max_size'] = '5000'; $config['max_width'] = '4096'; $config['max_height'] = '4096'; $this-&gt;load-&gt;library('upload', $config); if (!$this-&gt;upload-&gt;do_upload()) { $data = array('error' =&gt; $this-&gt;upload-&gt;display_errors()); $data['title'] = "Add Photo | Mark The Dark"; $this-&gt;load-&gt;view('photo_add_view', $data); } else { //get uploaded image info $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data()); //clean the data $data = $this-&gt;input-&gt;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-&gt;dx_auth-&gt;get_user_id(); //insert it in the database $this-&gt;db-&gt;insert('photos', $_POST); $photo_id = $this-&gt;db-&gt;insert_id(); //add stuff to tags table /* $tags_array = preg_split('/[\s,;]+/', $tags); foreach($tags_array as $tag) { if($tag != "" || $tag != null) $this-&gt;db-&gt;insert('tags', array('id' =&gt; $photo_id, 'word' =&gt; $tag)); }*/ //CXtags /*$tags_array = preg_split('/[\s,;]+/', $tags); foreach($tags_array as $tag) { if($tag == "" || $tag == null) {unset($tags_array[$tag]);} } */ $tags_array = $this-&gt;CXTags-&gt;comma_to_array($tags); foreach($tags_array as $tag) {$tags_array[$tag] = $this-&gt;CXTags-&gt;make_safe_tag($tag);} $topass = array( 'table' =&gt; 'photos', 'tags' =&gt; $tags_array, 'row_id' =&gt; $photo_id, 'user_id' =&gt; $_POST['author'] ); $this-&gt;CXTags-&gt;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-&gt;load-&gt;library('image_lib', $config); $this-&gt;image_lib-&gt;initialize($config); if(!$this-&gt;image_lib-&gt;rotate()) { echo $this-&gt;image_lib-&gt;display_errors(); } } $this-&gt;load-&gt;library('image_lib'); if ($width &gt; 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-&gt;image_lib-&gt;initialize($config); $this-&gt;load-&gt;library('image_lib', $config); if(!$this-&gt;image_lib-&gt;resize()) { echo $this-&gt;image_lib-&gt;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-&gt;image_lib-&gt;initialize($config); $this-&gt;load-&gt;library('image_lib', $config); $this-&gt;image_lib-&gt;resize(); redirect('photo/show/' . $photo_id); } //redirect('photo/photo_add/'); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload