public function generateMaybeCroppedImage(Image_Backend $backend, $width, $height) { if ($width > 0 && $height > 0) { return $backend->croppedResize($width, $height); } elseif ($width > 0) { return $backend->resizeByWidth($width); } elseif ($height > 0) { return $backend->resizeByHeight($height); } else { return $backend; } }
/** * Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight. * * @param Image_Backend $backend * @param integer $height The height to set * @return Image_Backend * @deprecated 4.0 Use generateScaleHeight instead */ public function generateSetHeight(Image_Backend $backend, $height) { Deprecation::notice('4.0', 'Use generateScaleHeight instead'); return $backend->resizeByHeight($height); }
/** * Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight. * * @param Image_Backend $backend * @param integer $height The height to set * @return Image_Backend */ public function generateSetHeight(Image_Backend $backend, $height) { return $backend->resizeByHeight($height); }
/** * * * @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); } } }
/** * Resize this Image by height, keeping aspect ratio. Use in templates with $SetHeight. * * @param Image_Backend $backend * @param integer $height The height to set * @return Image_Backend * @deprecated 4.0 Generate methods are no longer applicable */ public function generateSetHeight(Image_Backend $backend, $height) { Deprecation::notice('4.0', 'Generate methods are no longer applicable'); return $backend->resizeByHeight($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); } } } }