/** * * * @param Image_Backend $backend * @param integer $width Width to crop to * @param integer $height Height to crop to * @return Image_Backend */ public function generateCroppedFocusedImage(Image_Backend $backend, $width, $height, $cropAxis, $cropOffset) { if ($cropAxis == 'x') { //Generate image return $backend->resizeByHeight($height)->crop(0, $cropOffset, $width, $height); } else { if ($cropAxis == 'y') { //Generate image return $backend->resizeByWidth($width)->crop($cropOffset, 0, $width, $height); } else { //Generate image without cropping return $backend->resize($width, $height); } } }
/** * Generate a resized copy of this image with the given width & height. * Use in templates with $ResizedImage. * * @param Image_Backend $backend * @param integer $width Width to resize to * @param integer $height Height to resize to * @return Image_Backend */ public function generateResizedImage(Image_Backend $backend, $width, $height) { if (!$backend) { user_error("Image::generateFormattedImage - generateResizedImage is being called by legacy code" . " or Image::\$backend is not set.", E_USER_WARNING); } else { return $backend->resize($width, $height); } }
/** * Generate a resized copy of this image with the given width & height, cropping to maintain aspect ratio and focus point. * Use in templates with $CroppedFocusedImage * * @param Image_Backend $backend * @param integer $width Width to crop to * @param integer $height Height to crop to * @return Image_Backend */ public function generateCroppedFocusedImage(Image_Backend $backend, $width, $height) { $width = round($width); $height = round($height); $top = 0; $left = 0; $originalWidth = $this->owner->width; $originalHeight = $this->owner->height; if ($this->owner->width > 0 && $this->owner->height > 0) { //Can't divide by zero //Which is over by more? $widthRatio = $originalWidth / $width; $heightRatio = $originalHeight / $height; //Calculate offset required if ($widthRatio > $heightRatio) { //Left and/or right of image will be lost //target center in px $croppedCenterX = floor($width / 2); //X axis focus point of scaled image in px $focusFactorX = ($this->owner->FocusX + 1) / 2; //i.e .333 = one third along $scaledImageWidth = floor($originalWidth / $heightRatio); $focusX = floor($focusFactorX * $scaledImageWidth); //Calculate difference beetween focus point and center $focusOffsetX = $focusX - $croppedCenterX; //Reduce offset if necessary so image remains filled $xRemainder = $scaledImageWidth - $focusX; $croppedXRemainder = $width - $croppedCenterX; if ($xRemainder < $croppedXRemainder) { $focusOffsetX -= $croppedXRemainder - $xRemainder; } if ($focusOffsetX < 0) { $focusOffsetX = 0; } //Set horizontal crop start point $left = $focusOffsetX; //Generate image return $backend->resizeByHeight($height)->crop($top, $left, $width, $height); } else { if ($widthRatio < $heightRatio) { //Top and/or bottom of image will be lost //Container center in px $croppedCenterY = floor($height / 2); //Focus point of resize image in px $focusFactorY = ($this->owner->FocusY + 1) / 2; // zero is bottom of image, 1 is top $scaledImageHeight = floor($originalHeight / $widthRatio); $focusY = $scaledImageHeight - floor($focusFactorY * $scaledImageHeight); //Calculate difference beetween focus point and center $focusOffsetY = $focusY - $croppedCenterY; //Reduce offset if necessary so image remains filled $yRemainder = $scaledImageHeight - $focusY; $croppedYRemainder = $height - $croppedCenterY; if ($yRemainder < $croppedYRemainder) { $focusOffsetY -= $croppedYRemainder - $yRemainder; } if ($focusOffsetY < 0) { $focusOffsetY = 0; } //Set vertical crop start point $top = $focusOffsetY; //Generate image return $backend->resizeByWidth($width)->crop($top, $left, $width, $height); } else { //Generate image without cropping return $backend->resize($width, $height); } } } }