Beispiel #1
0
 /**
  * 缩略(放大)图片
  * @param int $thumbWidth 缩放的最大宽度
  * @param int $thumbHeight 缩放的最大高度
  * @param bool $magnify 是否允许放大图片
  * @return bool 成功返回true,失败返回false
  */
 public function resize($thumbWidth = 0, $thumbHeight = 0, $magnify = true)
 {
     $width = $this->getWidth();
     $height = $this->getHeight();
     do {
         //定宽缩放
         if ($thumbHeight === 0) {
             $thumbHeight = $thumbWidth / $width * $height;
             break;
         }
         //定高缩放
         if ($thumbWidth === 0) {
             $thumbWidth = $thumbHeight / $height * $width;
             break;
         }
         //等比例缩放
         if ($width / $thumbWidth > $height / $thumbHeight) {
             $ratio = $thumbWidth / $width;
         } else {
             $ratio = $thumbHeight / $height;
         }
         $thumbWidth = $width * $ratio;
         $thumbHeight = $height * $ratio;
     } while (0);
     //由于图片是等比率的,所以只需判断一条边
     if (!$magnify && $thumbWidth >= $width) {
         return true;
     }
     MagickSetImageCompressionQuality(self::$resource, 90);
     //1-100值越大,越清晰
     return MagickScaleImage(self::$resource, $thumbWidth, $thumbHeight);
 }
 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;
 }
Beispiel #3
0
function zoomImg($src, $dest, $width, $height)
{
    $srcImgRes = NewMagickWand();
    if (!MagickReadImage($srcImgRes, $src)) {
        exit("Error MagickReadImage {$src}\n");
    }
    MagickScaleImage($srcImgRes, $width, $height);
    MagickWriteImage($srcImgRes, $dest);
}
Beispiel #4
0
 function createThumb($objWidth, $objHeight, $nmw = "")
 {
     $srcImage = $this->src_image_name;
     if (!IsMagickWand($nmw)) {
         $nmw = NewMagickWand();
         MagickReadImage($nmw, $srcImage);
     }
     $srcImageWidth = MagickGetImageWidth($nmw);
     $srcImageHeight = MagickGetImageHeight($nmw);
     if ($objWidth == 0 || $objHeight == 0) {
         $objWidth = $srcImageWidth;
         $objHeight = $srcImageHeight;
     }
     if ($objWidth < $objHeight) {
         $mu = $srcImageWidth / $objWidth;
         $objHeight = ceil($srcImageHeight / $mu);
     } else {
         $mu = $srcImageHeight / $objHeight;
         $objWidth = ceil($srcImageWidth / $mu);
     }
     MagickScaleImage($nmw, $objWidth, $objHeight);
     $ndw = NewDrawingWand();
     DrawComposite($ndw, MW_AddCompositeOp, 0, 0, $objWidth, $objHeight, $nmw);
     $res = NewMagickWand();
     MagickNewImage($res, $objWidth, $objHeight);
     MagickDrawImage($res, $ndw);
     MagickSetImageFormat($res, MagickGetImageFormat($nmw));
     return $res;
 }