예제 #1
0
 /**
  * Resizes the image base on the width provided.
  * This resize maintain's the image aspect ratio.
  *
  * @param Image $image Image to process
  *
  * @return Image
  */
 protected function processImage(Image $image)
 {
     $this->_throwExceptionIfNoWidthIsSet();
     $width = $this->get("width");
     $maxHeight = $this->has("max-height") ? $this->get("max-height") : 0;
     $imageResource = $image->createImageResource();
     list($oldWidth, $oldHeight) = getimagesize($image->getFullPath());
     //////////////////////////////////////////////////////////////////////
     // For Aspect ratio
     $newHeight = 0;
     if ($maxHeight == 0) {
         // if the height is not specified calculate the relative height
         $newHeight = $width * $oldHeight / $oldWidth;
     }
     //////////////////////////////////////////////////////////////////////
     if ($maxHeight > 0) {
         $photoIsPortrait = $oldHeight > $oldWidth;
         if ($photoIsPortrait) {
             $width = $maxHeight * $oldWidth / $oldHeight;
             $newHeight = $maxHeight;
         } else {
             // This is a landscape photo
             $newHeight = $width * ($oldHeight / $oldWidth);
         }
     }
     $newImageResource = imagecreatetruecolor($width, $newHeight);
     imagecopyresampled($newImageResource, $imageResource, 0, 0, 0, 0, $width, $newHeight, $oldWidth, $oldHeight);
     $image->createImage($newImageResource);
 }
예제 #2
0
 /**
  * Converts image to grayscale
  *
  * @param Image $image Image to process
  *
  * @return void
  */
 protected function processImage(Image $image)
 {
     $imageResource = $image->createImageResource();
     list($width, $height) = getimagesize($image->getFullPath());
     for ($i = 0; $i < $width; $i++) {
         for ($j = 0; $j < $height; $j++) {
             // get the rgb value for current pixel
             $rgb = imagecolorat($imageResource, $i, $j);
             // extract each value for r, g, b
             $rr = $rgb >> 16 & 0xff;
             $gg = $rgb >> 8 & 0xff;
             $bb = $rgb & 0xff;
             // get the Value from the RGB value
             $g = round(($rr + $gg + $bb) / 3);
             // grayscale values have r=g=b=g
             $val = imagecolorallocate($imageResource, $g, $g, $g);
             // set the gray value
             imagesetpixel($imageResource, $i, $j, $val);
         }
     }
     $image->createImage($imageResource);
 }
예제 #3
0
 /**
  * Formats the file by applying the file name strategy defined
  *
  * @param Image $image Image object being processed
  *
  * @return void
  */
 private function _formatFile(Image $image)
 {
     $imageFileFormatter = new ImageFileFormatter($image);
     $newFileName = $imageFileFormatter->generate($this->_format, []);
     // Rename the final file to its designated format
     $imageName = $image->getImageName();
     if ($imageName !== $newFileName) {
         $this->getFileSystem()->rename($imageName, $newFileName);
     }
     $image->setImageName($newFileName);
 }
예제 #4
0
 /**
  * Get the physical dimension of the image
  *
  * @return array
  */
 private function _getImageDimension()
 {
     $imagePath = $this->_image->getFullPath();
     // Use the original width and height of the image
     return getimagesize($imagePath);
 }