public function resize($width = 0, $height = 0, $method = ZEBRA_IMAGE_CROP_CENTER, $background_color = '#FFFFFF')
{
// if image resource was successfully created
if ($this->_create_from_source()) {
// if either width or height is to be adjusted automatically
// set a flag telling the script that, even if $preserve_aspect_ratio is set to false
// treat everything as if it was set to true
if ($width == 0 || $height == 0) {
$auto_preserve_aspect_ratio = true;
}
// if aspect ratio needs to be preserved
if ($this->preserve_aspect_ratio || isset($auto_preserve_aspect_ratio)) {
// if height is given and width is to be computed accordingly
if ($width == 0 && $height > 0) {
// get the original image's aspect ratio
$aspect_ratio = $this->source_width / $this->source_height;
// the target image's height is as given as argument to the method
$target_height = $height;
// compute the target image's width, preserving the aspect ratio
$target_width = round($height * $aspect_ratio);
// if width is given and height is to be computed accordingly
} elseif ($width > 0 && $height == 0) {
// get the original image's aspect ratio
$aspect_ratio = $this->source_height / $this->source_width;
// the target image's width is as given as argument to the method
$target_width = $width;
// compute the target image's height, preserving the aspect ratio
$target_height = round($width * $aspect_ratio);
// if both width and height are given and ZEBRA_IMAGE_BOXED or ZEBRA_IMAGE_NOT_BOXED methods are to be used
} elseif ($width > 0 && $height > 0 && ($method == 0 || $method == 1)) {
// compute the horizontal and vertical aspect ratios
$vertical_aspect_ratio = $height / $this->source_height;
$horizontal_aspect_ratio = $width / $this->source_width;
// if the image's newly computed height would be inside the bounding box
if (round($horizontal_aspect_ratio * $this->source_height < $height)) {
// the target image's width is as given as argument to the method
$target_width = $width;
// compute the target image's height so that the image will stay inside the bounding box
$target_height = round($horizontal_aspect_ratio * $this->source_height);
// otherwise
} else {
// the target image's height is as given as argument to the method
$target_height = $height;
// compute the target image's width so that the image will stay inside the bounding box
$target_width = round($vertical_aspect_ratio * $this->source_width);
}
// if both width and height are given and image is to be cropped in order to get to the required size
} elseif ($width > 0 && $height > 0 && $method > 1 && $method < 11) {
// compute the horizontal and vertical aspect ratios
$vertical_aspect_ratio = $this->source_height / $height;
$horizontal_aspect_ratio = $this->source_width / $width;
// we'll use one of the two
$aspect_ratio = $vertical_aspect_ratio < $horizontal_aspect_ratio ? $vertical_aspect_ratio : $horizontal_aspect_ratio;
// compute the target image's width, preserving the aspect ratio
$target_width = round($this->source_width / $aspect_ratio);
// compute the target image's height, preserving the aspect ratio
$target_height = round($this->source_height / $aspect_ratio);
// for any other case
} else {
// we will create a copy of the source image
$target_width = $this->source_width;
$target_height = $this->source_height;
}
// if aspect ratio does not need to be preserved
} else {
// compute the target image's width
$target_width = $width > 0 ? $width : $this->source_width;
// compute the target image's height
$target_height = $height > 0 ? $height : $this->source_height;
}
// if
if ($this->enlarge_smaller_images || ($width > 0 && $height > 0 ? $this->source_width > $width || $this->source_height > $height : $this->source_width > $target_width || $this->source_height > $target_height)) {
// if
if (($this->preserve_aspect_ratio || isset($auto_preserve_aspect_ratio)) && ($width > 0 && $height > 0) && ($method > 1 && $method < 11)) {
// prepare the target image
$target_identifier = $this->_prepare_image($target_width, $target_height, $background_color);
imagecopyresampled($target_identifier, $this->source_identifier, 0, 0, 0, 0, $target_width, $target_height, $this->source_width, $this->source_height);
// do the crop according to the required method
switch ($method) {
// if image needs to be cropped from the top-left corner
case ZEBRA_IMAGE_CROP_TOPLEFT:
// crop accordingly
return $this->crop(0, 0, $width, $height, $target_identifier);
break;
// if image needs to be cropped from the top-center
// if image needs to be cropped from the top-center
case ZEBRA_IMAGE_CROP_TOPCENTER:
// crop accordingly
return $this->crop(floor(($target_width - $width) / 2), 0, floor(($target_width - $width) / 2) + $width, $height, $target_identifier);
break;
// if image needs to be cropped from the top-right corner
// if image needs to be cropped from the top-right corner
case ZEBRA_IMAGE_CROP_TOPRIGHT:
// crop accordingly
return $this->crop($target_width - $width, 0, $target_width, $height, $target_identifier);
break;
// if image needs to be cropped from the middle-left
// if image needs to be cropped from the middle-left
case ZEBRA_IMAGE_CROP_MIDDLELEFT:
// crop accordingly
return $this->crop(0, floor(($target_height - $height) / 2), $width, floor(($target_height - $height) / 2) + $height, $target_identifier);
break;
// if image needs to be cropped from the center of the image
// if image needs to be cropped from the center of the image
case ZEBRA_IMAGE_CROP_CENTER:
// crop accordingly
return $this->crop(floor(($target_width - $width) / 2), floor(($target_height - $height) / 2), floor(($target_width - $width) / 2) + $width, floor(($target_height - $height) / 2) + $height, $target_identifier);
break;
// if image needs to be cropped from the middle-right
// if image needs to be cropped from the middle-right
case ZEBRA_IMAGE_CROP_MIDDLERIGHT:
// crop accordingly
return $this->crop($target_width - $width, floor(($target_height - $height) / 2), $target_width, floor(($target_height - $height) / 2) + $height, $target_identifier);
break;
// if image needs to be cropped from the bottom-left corner
// if image needs to be cropped from the bottom-left corner
case ZEBRA_IMAGE_CROP_BOTTOMLEFT:
// crop accordingly
return $this->crop(0, $target_height - $height, $width, $target_height, $target_identifier);
break;
// if image needs to be cropped from the bottom-center
// if image needs to be cropped from the bottom-center
case ZEBRA_IMAGE_CROP_BOTTOMCENTER:
// crop accordingly
return $this->crop(floor(($target_width - $width) / 2), $target_height - $height, floor(($target_width - $width) / 2) + $width, $target_height, $target_identifier);
break;
// if image needs to be cropped from the bottom-right corner
// if image needs to be cropped from the bottom-right corner
case ZEBRA_IMAGE_CROP_BOTTOMRIGHT:
// crop accordingly
return $this->crop($target_width - $width, $target_height - $height, $target_width, $target_height, $target_identifier);
break;
}
// if aspect ratio doesn't need to be preserved or
// it needs to be preserved and method is ZEBRA_IMAGE_BOXED or ZEBRA_IMAGE_NOT_BOXED
} else {
// prepare the target image
$target_identifier = $this->_prepare_image($width > 0 && $height > 0 && $method != ZEBRA_IMAGE_NOT_BOXED ? $width : $target_width, $width > 0 && $height > 0 && $method != ZEBRA_IMAGE_NOT_BOXED ? $height : $target_height, $background_color);
imagecopyresampled($target_identifier, $this->source_identifier, $width > 0 && $height > 0 && $method != ZEBRA_IMAGE_NOT_BOXED ? ($width - $target_width) / 2 : 0, $width > 0 && $height > 0 && $method != ZEBRA_IMAGE_NOT_BOXED ? ($height - $target_height) / 2 : 0, 0, 0, $target_width, $target_height, $this->source_width, $this->source_height);
// if script gets this far, write the image to disk
return $this->_write_image($target_identifier);
}
// if we get here it means that
// smaller images than the given width/height are to be left untouched
// therefore, we save the image as it is
} else {
return $this->_write_image($this->source_identifier);
}
}
// if script gets this far return false
// note that we do not set the error level as it has been already set
// by the _create_from_source() method earlier
return false;
}