Easy creation of thumbnails and storing them on Amazon S3

No one website can't do without generating the thumbnail images. In the Internet you can find a million articles on the subject. Maybe someone will be useful this decision.

Requirements:
the
    the
  • Images are stored on remote servers. We have only the links to these images
  • the
  • Thumbnail should be generated of any given size at the time of direct appeal to it
  • the
  • Needs to be protected from pests
  • the
  • Thumbnail should be stored on Amazon S3 and be available on a subdomain of the main site. The number of buckets in S3, respectively, unlimited subdomains


Implementation:
Assume that the name of our website domain.com

1) Amazon S3 create bucket 4 with the names of ic1.domain.com, ic2.domain.com, ic3.domain.com, ic4.domain.com (names of subdomains can be different, in our case the reduction from an image cache)

2) Go to settings each bucket and put a checkmark in the Enable website hosting

3) In the Edit Redirection Rules prescribe rules for redirects to non-existent pages:

the
<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 control panel of the domain name domain.com and create sub-domains, similar to the names of the buckets. In CNAME prescription Endpoint from the settings of the bucket.

What we just did:
Created 4 subdomain, which will be stored the thumbnail. Each thumbnail will have an address like: ic1.domain.com/miniatura.jpg

As soon as this file will be the first call, Amazon will see that this file bucket ic1.domain.com and will redirect the user to the specified path that concatenates of the HostName + ReplaceKeyPrefixWith: domain.com/s3/thumb/miniatura.jpg

Caught on the server via a router or .htaccess file the request, create a thumbnail of the given file, send it to an Amazon S3 bucket and need to display the thumbnail of the first user.

Other users will get it directly from Amazon S3.

Went on.
It is still unclear how to pass parameters to minimize and the link to the picture itself.
What we have:
the
Of course, the link: ic1.domain.com/?Img_url=http://blablabla.com/photo15.jpg&Thumb_w=100&Thumb_h=100 is absolutely not good enough, because first Amazon we do not understand, and secondly- there is no protection from pests.

Variants how to create such links can be in the millions. We decided to do without the intermediate tables or records and looked in the direction of RC4 encryption. Sounds scary. But let's see what happened:
ic2.domain.com/e2/PUuxR1p~D~Jgl5PrnPMLh4OA0sO899rjZgzgWFU_.jpg
Pretty sweet link. At least not much worse than an md5 hash. But much more informative. If the decoding of the crypts, get:
blablabla.com/photo15:100@100
What we need – all the data on photos, dimensions, and plus, not knowing the password from the crypt – no one will be able to generate "extra thumbnail"

The question is simple – implement a function which will accept a reference to the image size and return a reference to the miniature (and of course all the other scripts).

The link on github some implementation tasks: github.com/wolflingorg/s3thumb

Structure:
CS3Thumb.php is the main class
S3.php – a class for working with Amazon S3
CRC4Crypt.php – a class for RC4 encryption in
CThumb.php – a class for creating thumbnails
How to use:
the
$Thumb = new CS3Thumb($backets, $accessKey, $secretKey, $cryptpsw = 'password');

Where:
$backets — an array with the names of the buckets (they are subdomains)
$accessKey and $secretKey – access to the Amazon
$cryptpsw – the password for the encryption links

in order to get the image url use:
the
$Thumb -> url("http://blablabla.com/photo15.jpg", 100, 100);

to create thumbnails, move it to S3 and display the first user:
the
$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 redirect from the Amazon

I hope the article will be useful. Thank you.
Article based on information from habrahabr.ru

Популярные сообщения из этого блога

Approval of WSUS updates: import, export, copy

Kaspersky Security Center — the fight for automation

The Hilbert curve vs. Z-order