private function createAndUploadImageDimensionMap(Image $image, $dimensionsKeyname)
 {
     $imd = $this->getDimensionByKeyname($dimensionsKeyname);
     $srcPth = $this->imagesRoot . $image->FilePath;
     if (file_exists($srcPth)) {
         $tempPath = tempnam(sys_get_temp_dir(), 'img_');
         if ($dimensionsKeyname == 'original') {
             $s3Path = $srcPth;
             $pts = getimagesize($s3Path);
             $width = $pts[0];
             $height = $pts[1];
             $newDims = array('x' => $width, 'y' => $height);
             $s3Path = $srcPth;
         } else {
             //need to redimension this image, using MagickWand
             $magick_wand = NewMagickWand();
             MagickReadImage($magick_wand, $srcPth);
             $width = MagickGetImageWidth($magick_wand);
             $height = MagickGetImageHeight($magick_wand);
             $newDims = $this->getNewDimensionsPreservingAspectRatio($width, $height, $imd->Width, $imd->Height);
             MagickScaleImage($magick_wand, $newDims['x'], $newDims['y']);
             MagickWriteImage($magick_wand, $tempPath);
             $s3Path = $tempPath;
         }
         $headers = array();
         //This holds the http headers for our S3 object.
         switch ($image->ImageType) {
             case 'GIF':
                 $headers['Content-Type'] = 'image/gif';
                 break;
             case 'JPG':
                 $headers['Content-Type'] = 'image/jpeg';
                 break;
             case 'PNG':
                 $headers['Content-Type'] = 'image/png';
                 break;
             default:
                 throw new Exception("Unrecognized type {$image->getType()}");
                 break;
         }
         //A "Far Future" expiration - maximizing the chance that the user's web browser will use
         //a cached version rather than requesting the file from Cloudfront.
         //Also set to public (as recommended by Google Speed Tracer), so that caching will work when
         //using SSL as well.  Even though Cloudfont doesn't support ssl today, someday it will
         //and we will be prepared!
         $headers['Cache-Control'] = 'public, max-age=315360000';
         //10 years
         $imDimMap = new ImageDimensionsMap();
         $imDimMap->ImageId = $image->Id;
         $imDimMap->ImageDimensionsId = $imd->Id;
         $imDimMap->Width = $newDims['x'];
         $imDimMap->Height = $newDims['y'];
         $imDimMap->Version = $image->Version;
         $imDimMap->save();
         //upload the new file to S3
         try {
             $acl = S3Service::ACL_PUBLIC_READ;
             if (!file_exists($tempPath)) {
                 throw new Exception("{$tempPath} dosn't exist");
             }
             $res = $this->s3->putObject(S3Service::inputFile($s3Path, true), $this->bucket, $this->getPath($image, $imDimMap, $imd), $acl, array(), $headers);
             if ($res) {
                 unlink($tempPath);
             } else {
                 //something's wrong.  Fail silently and just leave the old version if it exists.
                 //Don't throw an exception or raise a failure, because there's a user on the other side
                 //of this request waiting to see this image.
                 $imDimMap->Version = $imDimMap->Version - 1;
                 $imDimMap->save();
             }
         } catch (Exception $e) {
             //something's wrong.  Fail silently and just leave the old version if it exist.
             //Don't throw an exception or raise a failure, because there's a user on the other side
             //of this request waiting to see this image.
             $imDimMap->Version = $imDimMap->Version - 1;
             $imDimMap->save();
         }
     }
     return $imDimMap;
 }