RSS

PHP Script: Automatisierte Thumbnails via Amazon Alexa Generator Klasse

Fr, Aug 29, 2008

Allgemeines, PHP, Programmierung

Da hier in letzter Zeit etwas wenig zum Thema PHP und Co. von mir beigetragen wurde, möchte ich mal etwas dagegen tun. Ich weiß nicht, ob ihr schon die Amazon Webservices kennengelernt habt. Jeder Entwickler sollte jedoch dort öfter einen Blick riskieren. Die diversen Services, die dort angeboten und je nach Verbrauch abgerechnet werden, sind durchaus hin und wieder – für eigene Projekte – Überlegungen wert. So gibt es dort in Zusammenarbeit mit dem Alexa Service die Möglichkeit auf eine Thumbnail-Datenbank zurückzugreifen, die automatisch Screenshots von div. Webseiten zur Verfügung stellen kann. Es gibt dort auch einen Beispielskript für PHP, der jedoch nicht wirklich objektorientiert ist, und sich als solches in objektorientierte Projekte nur schwer einglieder lässt.

Deswegen habe ich mal eine kleine Klasse daraus gestrickt, die ich hiermit der weiten Welt und den 2-3 PHP Entwicklern nicht vorenthalten will. Voraussetzung ist natürlich ein Account bei Amazon AWS:

<?

class AWSThumbnail {

const THUMBNAIL_SIZE_SMALL = "Small";
const THUMBNAIL_SIZE_BIG = "Large";

private $HTTPRequestTimeout = 5;

private $accessID, $secretKey, $lastImageUrl, $defaultImageUrl = null;

/**
* Construct a new AWSThumbnail Instance
*
* @param string $accessID
* @param string $secretKey
*/

public function __construct($accessID, $secretKey) {
$this->accessID = $accessID;
$this->secretKey = $secretKey;
}

private function getSecretKey() {
return $this->secretKey;
}

private function getAccessID() {
return $this->accessID;
}

public function setDefaultImageUrl($url) {
$this->defaultImageUrl = $url;
}

public function getDefaultImageUrl() {
return $this->defaultImageUrl;
}

private function setLastImageUrl($url) {
$this->lastImageUrl = $url;
}

public function getLastImageUrl() {
return $this->lastImageUrl;
}

public function createThumbnail($url, $size = self::THUMBNAIL_SIZE_SMALL) {
$ts = $this->generateTimestamp();

$request_url =  "http://ast.amazonaws.com/xino/?"
. "Service=".           "AlexaSiteThumbnail"
. "&amp;amp;Action=".           "Thumbnail"
. "&amp;amp;AWSAccessKeyId=".   $this->getAccessID()
. "&amp;amp;Timestamp=" .       urlencode($ts)
. "&amp;amp;Signature=" .       urlencode ($this->calculateRFC2104HMAC("AlexaSiteThumbnail" . "Thumbnail" . $ts, $this->getSecretKey()))
. "&amp;amp;Size=" .            $size
. "&amp;amp;Url=" .             $url;

$result = $this->getHTTPResult($request_url);

#echo nl2br(htmlentities($result));

$response_doc = new DOMDocument();
$response_doc->loadXML($result);

$thumbnail = $response_doc->getElementsByTagName("Thumbnail")->item(0);
if ($thumbnail != NULL) {
if ($thumbnail->getAttribute("Exists") == "true") {
$this->setLastImageUrl($thumbnail->firstChild->nodeValue);
return true;
}
else {
if ($this->getDefaultImageUrl()) {
$this->setLastImageUrl($this->getDefaultImageUrl());
}
else {
$this->setLastImageUrl($thumbnail->firstChild->nodeValue);
}
return false;
}
}

throw new Exception('Error while creating the image...');
}

private function generateTimestamp () {
return gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
}

private function calculateRFC2104HMAC ($data, $key) {
return base64_encode (
pack("H*", sha1((str_pad($key, 64, chr(0x00))
^(str_repeat(chr(0x5c), 64))) .
pack("H*", sha1((str_pad($key, 64, chr(0x00))
^(str_repeat(chr(0x36), 64))) . $data))))
);
}

private function getHTTPResult($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->HTTPRequestTimeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception("HTTP Request failed.");
}
curl_close($ch);
return $result;
}
}

AWSThumbnail.class.php

Die ganze Klasse gibt’s auch hier zum Download. Falls ihr den Code verwendet, wäre ich dankbar um eine kleine Anmerkung und einen Link zu diesem Blog. :) Ich hoffe es gefällt…

Popularity: 22% [?]

Gleich bookmarken:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkArena
  • Live
  • MisterWong.DE
  • BlinkList
  • Furl
  • Spurl
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Werbung:


, , , , , , , ,

This post was written by:

Ronny - who has written 346 posts on Ronny’s Blog.


Contact the author

Leave a Reply

You can add images to your comment by clicking here.