print "Thumbnailing '{$bucketIn}' to '{$bucketOut}'\n"; // Create the S3 access object $s3 = new AmazonS3(); // Get object list from input bucket $objectsIn = getBucketObjects($s3, $bucketIn); // Process each object. Generate thumbnails only for images foreach ($objectsIn as $objectIn) { $key = $objectIn->Key; print "Processing item '{$key}':\n"; if (substr(guessType($key), 0, 6) == "image/") { $startTime = microtime(true); $dataIn = $s3->get_object($bucketIn, $key); $endTime = microtime(true); $contentType = guessType($key); printf("\tDownloaded from S3 in %.2f seconds.\n", $endTime - $startTime); $startTime = microtime(true); $dataOut = thumbnailImage($dataIn->body, $contentType); $endTime = microtime(true); printf("\tGenerated thumbnail in %.2f seconds.\n", $endTime - $startTime); $startTime = microtime(true); if (uploadObject($s3, $bucketOut, $key, $dataOut, AmazonS3::ACL_PUBLIC, $contentType)) { $endTime = microtime(true); printf("\tUploaded thumbnail to S3 in %.2f seconds.\n", $endTime - $startTime); } else { print "\tCould not upload thumbnail.\n"; } } else { print "\tSkipping - not an image\n"; } print "\n"; }
$message = pullMessage($sqs, $imageQueueURL); if ($message != null) { // Extract message detail $messageDetail = $message['MessageDetail']; $receiptHandle = (string) $message['ReceiptHandle']; $imageURLs = $messageDetail['Data']; print "Processing message with " . count($imageURLs) . " images:\n"; // Do the work $s3ImageKeys = array(); foreach ($imageURLs as $imageURL) { // Fetch the image print " Fetch image '{$imageURL}'\n"; $image = file_get_contents($imageURL); print " Retrieved " . strlen($image) . " byte image\n"; // Create a thumbnail $imageThumb = thumbnailImage($image, 'image/png'); // Store the image in S3 $key = 'image_' . md5($imageURL) . '.png'; if (uploadObject($s3, $bucket, $key, $imageThumb)) { print " Stored image in S3 using key '{$key}'\n"; $s3ImageKeys[] = $key; } } // If the images were processed, pass them to the image renderer if (count($imageURLs) == count($s3ImageKeys)) { // Form message to pass page along to image renderer $origin = $messageDetail['Origin']; $history = $messageDetail['History']; $pageTitle = $messageDetail['PageTitle']; $history[] = 'Processed by ' . $argv[0] . ' at ' . date('c'); $message = json_encode(array('Action' => 'RenderImages', 'Origin' => $origin, 'Data' => $s3ImageKeys, 'History' => $history, 'PageTitle' => $pageTitle));
function addCloudListItem($sdb, $s3, $city, $state, $date, $price, $category, $title, $description, $imagePath) { // Form key $key = md5($city . $state . $date . $price . $category . $title); // Fetch image, store original and thumbnail in S3 if ($imagePath !== null) { // Get original $imageIn = file_get_contents($imagePath); $imageMem = ImageCreateFromString($imageIn); // Render as-is to JPEG and read in $fileOut = tempnam("/tmp", "aws") . ".aws"; $ret = ImageJPEG($imageMem, $fileOut, 100); $imageOut = file_get_contents($fileOut); // Create thumbnail $thumbOut = thumbnailImage($imageOut, "image/jpg"); // Store original and thumbnail in S3 $imageKey = $key . '.jpg'; $thumbKey = $key . '_thumb.jpg'; if (!uploadObject($s3, CL_BUCKET, $imageKey, $imageOut, AmazonS3::ACL_PUBLIC, "image/jpeg") || !uploadObject($s3, CL_BUCKET, $thumbKey, $thumbOut, AmazonS3::ACL_PUBLIC, "image/jpeg")) { return false; } $imageURL = $s3->get_object_url(CL_BUCKET, $imageKey, "60 seconds"); $thumbURL = $s3->get_object_url(CL_BUCKET, $thumbKey, "60 seconds"); } else { $imageURL = null; $thumbURL = null; } // Store the description in S3 if (uploadObject($s3, CL_BUCKET, $key, $description, AmazonS3::ACL_PUBLIC)) { $descriptionURL = $s3->get_object_url(CL_BUCKET, $key, "60 seconds"); } else { return false; } // Form attributes $attrs = array('City' => $city, 'State' => $state, 'Date' => $date, 'Price' => $price, 'Category' => $category, 'Title' => $title, 'Description' => $descriptionURL); if ($imageURL !== null) { $attrs['Image'] = $imageURL; $attrs['Thumb'] = $thumbURL; } // Insert item $res = $sdb->put_attributes(CL_ITEM_DOMAIN, $key, $attrs, true); return $res->isOK(); }
<?php function thumbnailImage($imagePath) { $im = new Imagick(); $im->setBackgroundColor(new ImagickPixel('transparent')); $im->readImage(realpath($imagePath)); $dim = $im->getImageGeometry(); if (!isset($_GET['wh'])) { $wh = 100; } $wh = intval($_GET['wh']); if ($wh <= 0) { $wh = 100; } // $aspect=floatval($dim['width'])/floatval($dim['height']); // $inverse_aspect=floatval($dim['height'])/floatval($dim['width']); $width = $wh; $height = $wh; $im->setImageFormat("png32"); $im->thumbnailImage($width, $height, true, true); header("Content-Type: image/png"); echo $im; } if (isset($_GET['src'])) { try { @thumbnailImage(urldecode($_GET['src'])); } catch (Exception $e) { } }