Which method would be less resource intensive for controlling access to images with PHP: image data URI's or image.php file?

Hello,
I would like to control access to images based on the connected user (this is required for security reasons). I found two solutions:

  1. An image.php file returning an image resource:
    However this requires passing the requested file as GET variable (not literal filename), loading additional classes and executing code to determine whether the user can access the requested file. I suspect this can get resource intensive when a page has 20-50 images.

  2. Data URI’s:
    This would require generating a data URI string for every image on the page.

Frankly the second method seems logically better, as it would all be done from one place, subjected to the access permissions already inforced. I am only worried about the additional resources this might require.

Please advice on which method would be more preferable, and add any additional info you believe would be useful.

Thank you for your time :slight_smile:

UPDATE: I believe I have found the solution.

Instead of having to generate the base64 image string every time, it can be generated one upon upload, stored in the database and served to the client upon request.

1 Like

I’d venture to say, keeping the files in the database is an overall bad solution, simply because it won’t scale well. This would also have nothing to do with what data URIs were meant to do. Be warned, the size of data URIs should be limited, i.e. it should only be used for icon graphics or quite small images, not for regular images.

Since you shouldn’t use data URIs for larger images, Point 1 is the only method left and the correct one for serving up “secure” images.

Scott

1 Like

Without knowing more about the particular page I would use Server sided scripting to limit the displayed images:

// restricted to "admin"
if ( 'admin' === $user ){
  echo '<img src="/images/admin-001.jpg" alt="Admin" />';
  echo '<img src="/images/admin-002.jpg" alt="Admin" />';
  echo '<img src="/images/admin-003.jpg" alt="Admin" />';
}

// common to everybody
  echo '<img src="/images/user-001.jpg" alt="User" />';
  echo '<img src="/images/user-002.jpg" alt="User" />';
  echo '<img src="/images/user-003.jpg" alt="User" />';

Can you supply more info’ and possibly a web-page?

Edit:
removed three back ticks and replaced with <pre> block formatting (more aesthetic) :slight_smile:

1 Like

You have a point. I’ll go with the image.php file.

Thank you all for helping.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.