Welcome Guest, Not a member yet? Register   Sign In
Caching Images
#1

[eluser]markanderson993[/eluser]
I am creating an image system where a user uploads an image and can view it again for instance as a profile picture. What I've written so far is a coded procedure that follow these steps.

1. User Upload (image is selected of user's computer) submit button activates a handle script

2. The image is encoded and resized 4 times according to various needs and stored in a special folder uniquely generated when the user successfully signed up. Also a unique reference name is assigned to each image and stored in a database.

( At this point there are 4 images in a folder called something like 9812uy98313981238923 and located in an upload folder in the root directory)

3. Now when a user wants to see a picture (lets say a profile picture) I print out an image tag <img> and for the source I send it off to what I call the serving function. Here it is:

Code:
&lt;?php

class Serve extends Controller {

    function Serve()
    {
        parent::Controller();
    }
    
    function index()
    {
        redirect('');
    }
    
    function image()
    {
            
        $ref_name = $this->uri->segment(3);
        
        if (!isset($ref_name)) redirect('');
    
        #Get Location Information (Reference Name, Upload Folder)
    
        $this->db->where('ref_name',$ref_name);
        $get_image_dir_row = $this->db->get('image_dir');
        
        if ($get_image_dir_row->num_rows() == 1)
        {
            
            $image_dir_row = $get_image_dir_row->row();
            
            $upload_folder = $image_dir_row->upload_folder;
            
            # Get Image Info (Raw name, Extension, File Type)
            
            $this->db->where('image_id',$image_dir_row->image_id);
            $get_image_info_row = $this->db->get('image_info');
            $image_info_row = $get_image_info_row->row();
            
            $raw_name = $image_info_row->raw_name;
            $ext = $image_info_row->file_ext;    
            $file_type = $image_info_row->file_type;
            
          // Do access checks here...
        
          // If access is allowed, figure out where the real image is from the image id...
          $actual_path_to_image = '../uploads/'.$upload_folder.'/'.$raw_name . $ext;
        
          // Output the image...
          header('Content-Type: '.$file_type);
          echo(file_get_contents($actual_path_to_image));
          
          $this->output->cache(1);
          
      } else {
          redirect('');
      }
      
    }

}

?&gt;
(so what this is doing is using a reference name to retrieve the actual file name in the user's upload folder)

How can I make it that this controller is still activated when a user wants to see their image but cache the images retrieved so that if the image is viewed more than once it is already loaded and ready to go?

I appreciate any help, thank you!

- Mark




Theme © iAndrew 2016 - Forum software by © MyBB