No site can do without generating thumbnail images. On the Internet you can find a million articles on this topic. Perhaps this solution will be useful to someone.
Requirements:- Images are stored on remote servers. We only have links to these images.
- The miniature should be formed of any given size at the moment of direct access to it.
- Pest protection should be provided.
- The thumbnail should be stored on Amazon S3 and be accessible through the subdomain of the main site. The number of buckets on S3 and subdomains is unlimited
Implementation:Suppose that the name of our site is
domain.com1) On Amazon S3 we create 4 baketas with the names
ic1.domain.com ,
ic2.domain.com ,
ic3.domain.com ,
ic4.domain.com (the names of subdomains can be different, in our case this is an abbreviation for image cache)
')
2) Go to the settings of each batch and tick the box next to
Enable website hosting3) In the
Edit Redirection Rules field we set the rules for redirects from non-existent pages:
<RoutingRules> <RoutingRule> <Condition> <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals> </Condition> <Redirect> <HostName>domain.com</HostName> <ReplaceKeyPrefixWith>s3/thumbs/</ReplaceKeyPrefixWith> </Redirect> </RoutingRule> <RoutingRule> <Condition> <HttpErrorCodeReturnedEquals>403</HttpErrorCodeReturnedEquals> </Condition> <Redirect> <HostName>domain.com</HostName> <ReplaceKeyPrefixWith>s3/thumbs/</ReplaceKeyPrefixWith> </Redirect> </RoutingRule> </RoutingRules>
4) Go to the domain name control panel
domain.com and create subdomains similar to the names of buckets. In CNAME, we register
Endpoint from the settings of the bake.
What we just did:Created 4 subdomains on which thumbnails will be stored. Each miniature will have an address of the type:
ic1.domain.com/miniatura.jpgAs soon as the first call is made to this file, Amazon will see that there is no such file in the ic1.domain.com
batch and will redirect the user to the path you specified, which is concatenated from
HostName +
ReplaceKeyPrefixWith :
domain.com/s3/thumb/miniatura.jpgOn the server, we retrieve a request through a router or .htaccess file, create a thumbnail of the transferred file, send it to Amazon S3 in the desired batch and display the thumbnail to the first user.
Other users will receive it directly from Amazon S3.
Let's go further.It is still not clear exactly how to pass the parameters for minimization and the link to the image itself.
What we have:
Of course, the link of the form:
ic1.domain.com/?Img_url=http://blablabla.com/photo15.jpg&Thumb_w=100&Thumb_h=100
does not suit us at all, because firstly, Amazon will not understand us, and secondly - there is no protection against pests.
Options for how to create such links may be a million. We decided to do without intermediate tables or records and looked in the direction of
RC4 encryption . It sounds scary. But let's see what happened:
ic2.domain.com/e2/PUuxR1p~D~Jgl5PrnPMLh4OA0sO899rjZgzgWFU_.jpg
Pretty nice link. At least not much worse than md5 hash. But much more informative. If you decrypt the crypt, we get:
blablabla.com/photo15:100@100
What we need - all the data on the photo, the size and plus to everything, without knowing the password from the crypt - no one will be able to generate "extra miniatures"
The question remains for small - to implement a function that will accept a link to the image, dimensions and return the link to the thumbnail (well, of course, all the other scripts).
Following the link to github some implementation of the task:
github.com/wolflingorg/s3thumbStructure:CS3Thumb.php - main class
S3.php - class for working with Amazon S3
CRC4Crypt.php - class for encryption in RC4
CThumb.php - class for creating thumbnails
How to use: $Thumb = new CS3Thumb($backets, $accessKey, $secretKey, $cryptpsw = 'password');
Where:
$ backets - array with names of buckets (they are subdomains)
$ accessKey and
$ secretKey - access to Amazon
$ cryptpsw - password to
encrypt links
In order to get a link to the image, use: $Thumb -> url("http://blablabla.com/photo15.jpg", 100, 100);
To create a thumbnail, move it to S3 and display it to the first user: $Thumb -> process("e2/PUuxR1p~D~Jgl5PrnPMLh4OA0sO899rjZgzgWFU_.jpg");
where
e2 / PUuxR1p ~ D ~ Jgl5PrnPMLh4OA0sO899rjZgzgWFU_.jpg - what will be in the link after domain.com/s3/thumbs/(*), after the redirect from Amazon
I hope the article will be useful. Thank.