/** * Crop the image * * @param Image $image Image to process * * @return void */ protected function processImage(Image $image) { $this->_image = $image; $finalCropWidth = $this->get("width"); $finalCropHeight = $this->get("height"); $this->_ratioMultiplier = $this->_getRatioMultiplier($finalCropWidth, $finalCropHeight); // Get orientation using the ratio multiplier // compute a dummy width and height $this->_orientation = $this->_getOrientation(); list($imageWidth, $imageHeight) = $this->_getImageDimension(); $this->_sourceImageWidth = $imageWidth; $this->_sourceImageHeight = $imageHeight; //////////////////////////////////////////////////////// // Auto Cropping // If there are no width and height details provided // Do auto cropping using the dimensions of the image list($width, $height, $x, $y) = $this->_autoCropSourceImage(); //////////////////////////////////////////////////////// // If cropped dimensions are provided if ($this->has("crop-box-width")) { list($width, $height, $x, $y) = $this->_cropUsingBox(); } ////////////////////////////////////////////////////////////////// $sourceImage = $this->_image->createImageResource(); $croppedImage = imagecreatetruecolor($finalCropWidth, $finalCropHeight); imagecopyresampled($croppedImage, $sourceImage, 0, 0, $x, $y, $finalCropWidth, $finalCropHeight, $width, $height); $this->_image->createImage($croppedImage); }
/** * 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); }