/** * 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); }
/** * 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); }
/** * 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); }