K
K
Konstantin B.2016-02-12 20:45:20
PHP
Konstantin B., 2016-02-12 20:45:20

Duplicate protection in Laravel images?

There is a need to create protection against copying images, namely, so that it is impossible to upload the same picture twice.
What can be used as a key? Trite md5 hash pictures? And will it help if you rename the picture, change the extension?
I also look in the direction of determining the degree of similarity of the pictures, but I doubt something.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Defman21, 2016-02-12
@Defman21

> And will it help if you rename the picture, change the extension?
If the file name was not taken into account when generating the hash sum, then it will help.
www.stackoverflow.com/a/5055152

A
Andrzej Wielski, 2016-02-13
@wielski

To generate a temporary link to an image, write a helper, accept a link to the image in the function and save it in the session. Example:

namespace App\Helpers;

trait ProtectedImage {
   static function url($image, $path = 'images/'){
     $hash = md5(time() . $image . 'somestring');
     session(['image.'.$hash => $image]);
     return $hash;
   }
}

We create our route, according to which we will issue a picture, for example, from Storage:
Route::get('images/{filename}', function ($filename)
{
    $realfilename = session('image.'.$filename);
    if(empty($realfilename)) abort(404);
    $path = storage_path() . '/' . $realfilename;

    $file = File::get($path);
    $type = File::mimeType($path);

    $response = Response::make($file, 200);
    $response->header("Content-Type", $type);

    return $response;
});

When displaying a link to an image, respectively
How it works:
We pass the name of the image to the function in the helper, the helper saves it to the session with the md5 key, when the route /images/key is opened, we get the real name from the session, and issue the image.
This is just an implementation example. You can add whatever you like. Or, instead of sessions, use public and private key encryption. You decide :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question