Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // This class receives a page URL from the user then searches
- // the destination for potential thumbnails via Embedly.
- // The thumbnails (if found) are then returned as base64 in a JSON response.
- class GetThumbnails{
- private $embedlyPrivateKey = '<HIDDEN - YOU CAN GET ONE FOR FREE FROM EMBED.LY>';
- private $initialPageUrl;
- // Misc info about code execution for debugging purposes.
- private $debugInfoArray = array();
- private $imageFlavour; // jpeg | x-icon
- private $imagesToKeep;
- private $maxWidth;
- private $maxHeight;
- public function __construct(){
- $this->imagesToKeep = 3;
- $this->maxWidth = 70;
- $this->maxHeight = 55;
- // *No security for now! (regex later).
- if(isset($_GET['pageUrl']) && $_GET['pageUrl'] != ''){
- $this->initialPageUrl = $_GET['pageUrl'];
- }else{
- // STATIC URL FOR DEMO ONLY - replace for different results.
- $this->initialPageUrl = 'http://www.microsoft.com';
- }
- $imageUrlArray = $this->getImagesByPageUrl($this->initialPageUrl);
- $thumbnailedImages = $this->thumbnailImagesViaEmbedly($imageUrlArray['imageUrlArray']);
- // FOLLOWING OUTPUT IS FOR DEMO ONLY
- echo '<pre>';
- echo '<u>The JSON array returned to the user:</u><br><br>';
- print_r(array(
- 'debugInfoArray'=>$this->debugInfoArray,
- 'finalPageUrl'=>$imageUrlArray['finalPageUrl'],
- 'imageUrlArray'=>$imageUrlArray['imageUrlArray'],
- 'thumbnailedImages'=>$thumbnailedImages,
- 'thumbnailDimensions'=>array(
- 'width'=>$this->maxWidth,
- 'height'=>$this->maxHeight
- )
- ));
- echo '<br><u>Sample images:</u><br>';
- if(count($imageUrlArray['imageUrlArray']) == 0){
- echo '<br>No images found.';
- }else{
- for($i=0; $i<count($imageUrlArray['imageUrlArray']); $i++){
- echo '<br>Image ' . ($i+1) . ' before being cropped and thumbed:<br><br>';
- echo '<img src="' . $imageUrlArray['imageUrlArray'][$i] . '" /><br>';
- echo '<br>Image ' . ($i+1) . ' after being cropped and thumbed:<br><br>';
- echo '<img src="' . $thumbnailedImages[$i] . '" /><br>';
- }
- }
- echo '</pre>';
- // END DEMO ONLY
- // Return all data to user via JSON.
- // echo json_encode(array(
- // 'debugInfoArray'=>$this->debugInfoArray,
- // 'finalPageUrl'=>$imageUrlArray['finalPageUrl'],
- // 'imageUrlArray'=>$imageUrlArray['imageUrlArray'],
- // 'thumbnailedImages'=>$thumbnailedImages,
- // 'thumbnailDimensions'=>array(
- // 'width'=>$this->maxWidth,
- // 'height'=>$this->maxHeight
- // )
- // ));
- }
- // This function accepts a page URL.
- // Embedly uses this URL to find images on that page. (see: http://embed.ly/docs/api/extract)
- // Returns array of found image URLs.
- private function getImagesByPageUrl($pageURL){
- $imageUrlArray = array();
- // Follow any redirects from original URL before requesting images.
- $finalPageUrl = $this->cURL($pageURL)['finalPageUrl'];
- $curlResponse = $this->cURL('http://api.embed.ly/1/extract?key='.$this->embedlyPrivateKey.'&url='.urlencode($finalPageUrl));
- $responseCode = $curlResponse['responseCode'];
- $embedlyJSON = json_decode($curlResponse['embedlyJSON'], true);
- if($responseCode == '200'){
- if(!empty($embedlyJSON['images'])){
- $this->imageFlavour = 'jpeg';
- $this->debugInfo("Embedly found ".count($embedlyJSON['images'])." images.");
- if(count($embedlyJSON['images']) >= $this->imagesToKeep){
- $this->debugInfo("Keeping first ".$this->imagesToKeep."..");
- }
- foreach($embedlyJSON['images'] as $embedlyImageURL){
- array_push($imageUrlArray, $embedlyImageURL['url']);
- }
- }else{
- $this->debugInfo("Embedly failed to find images.");
- $this->debugInfo("Searching for favicon..");
- if(!empty($embedlyJSON['favicon_url'])){
- $this->imageFlavour = 'x-icon';
- $this->debugInfo("Embedly found a favicon.");
- array_push($imageUrlArray, $embedlyJSON['favicon_url']);
- }else{
- $this->debugInfo("Embedly failed to find favicon..");
- $this->debugInfo("Default thumbnail will be used..");
- }
- }
- if(count($imageUrlArray) > $this->imagesToKeep){
- $imageUrlArray = array_slice($imageUrlArray, 0, $this->imagesToKeep, true);
- }
- }else{
- $this->debugInfo("Page response was \"" . $responseCode . "\"..");
- }
- return array(
- 'responseCode'=>$responseCode,
- 'finalPageUrl'=>$finalPageUrl,
- 'imageUrlArray'=>$imageUrlArray
- );
- }
- // This function creates thumbnails and returns them in base64.
- // Image URLs are sent to Embedly along with desired dimensions for cropping. (see: http://embed.ly/docs/api/display/endpoints/1/display/crop)
- // Responses are base64 encoded before being returned to user via JSON (Instead of using JavaScript which would reveal Embedly private key).
- private function thumbnailImagesViaEmbedly($imageUrlArray){
- $imageSourceBase64Array = array();
- if(!empty($imageUrlArray)){
- if($this->imageFlavour == 'jpeg'){
- foreach($imageUrlArray as $selectedimageSource){
- $thumbnailedImageUrl = $this->cURL(
- 'https://i.embed.ly/1/display/crop?key='.
- $this->embedlyPrivateKey.
- '&url='.urlencode($selectedimageSource).
- '&height='.$this->maxHeight.
- '&width='.$this->maxWidth)['embedlyJSON'];
- array_push($imageSourceBase64Array, 'data:image/'.$this->imageFlavour.';base64,'.base64_encode($thumbnailedImageUrl));
- }
- }elseif ($this->imageFlavour == 'x-icon'){
- // Thumbnails (x-icon) are not cropped, so no need to send back to Embedly first.
- array_push($imageSourceBase64Array, 'data:image/'.$this->imageFlavour.';base64,'.base64_encode($this->cURL($imageUrlArray[0])['embedlyJSON']));
- // Make sure favicon remains square.
- $this->maxWidth = $this->maxHeight;
- }
- }else{
- $this->debugInfo("No URLs provided for cropping - No images were cropped..");
- }
- return $imageSourceBase64Array;
- }
- // Function accepts URL as parameter.
- // Returns array of:
- // JSON from Embedly.
- // Response code. (200, 404 etc.).
- // Redirect URL (if supplied URL is redirected).
- private function cURL($pageURL){
- $ch = curl_init($pageURL);
- curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36');
- // Return page as string only.
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // Disabled to avoid some errors.
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // Follow all redirected URLs.
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
- $embedlyJSON = curl_exec($ch);
- $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $finalPageUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
- curl_close($ch);
- return array(
- 'embedlyJSON'=>$embedlyJSON,
- 'responseCode'=>$responseCode,
- 'finalPageUrl'=>$finalPageUrl
- );
- }
- private function debugInfo($info){
- array_push($this->debugInfoArray, $info);
- }
- // END CLASS GetThumbnails()
- }
- $thumbnails = new GetThumbnails;
- ?>
Add Comment
Please, Sign In to add comment