Zebra_Image::resize PHP Method

resize() public method

include the Zebra_Image library require 'path/to/Zebra_Image.php'; instantiate the class $img = new Zebra_Image(); a source image $img->source_path = 'path/to/source.ext'; path to where should the resulting image be saved note that by simply setting a different extension to the file will instruct the script to create an image of that particular type $img->target_path = 'path/to/target.ext'; apply a "sharpen" filter to the resulting images $img->sharpen_images = true; resize the image to exactly 150x150 pixels, without altering aspect ratio, by using the CROP_CENTER method $img->resize(150, 150, ZEBRA_IMAGE_CROP_CENTER);
public resize ( integer $width, integer $height, integer $method = ZEBRA_IMAGE_CROP_CENTER, hexadecimal $background_color = '#FFFFFF' ) : boolean
$width integer The width to resize the image to. If set to 0, the width will be automatically adjusted, depending on the value of the height argument so that the image preserves its aspect ratio. If {@link preserve_aspect_ratio} is set to TRUE and both this and the height arguments are values greater than 0, the image will be resized to the exact required width and height and the aspect ratio will be preserved - (also see the description for the method argument below on how can this be done). If {@link preserve_aspect_ratio} is set to FALSE, the image will be resized to the required width and the aspect ratio will be ignored. If both width and height are set to 0, a copy of the source image will be created ({@link jpeg_quality} and {@link png_compression} will still apply). If either width or height are set to 0, the script will consider the value of the {@link preserve_aspect_ratio} to bet set to TRUE regardless of its actual value! @param integer $height The height to resize the image to. If set to 0, the height will be automatically adjusted, depending on the value of the width argument so that the image preserves its aspect ratio. If {@link preserve_aspect_ratio} is set to TRUE and both this and the width arguments are values greater than 0, the image will be resized to the exact required width and height and the aspect ratio will be preserved - (also see the description for the method argument below on how can this be done). If {@link preserve_aspect_ratio} is set to FALSE, the image will be resized to the required height and the aspect ratio will be ignored. If both width and height are set to 0, a copy of the source image will be created ({@link jpeg_quality} and {@link png_compression} will still apply). If either height or width are set to 0, the script will consider the value of the {@link preserve_aspect_ratio} to bet set to TRUE regardless of its actual value! @param int $method (Optional) Method to use when resizing images to exact width and height while preserving aspect ratio. If the {@link preserve_aspect_ratio} property is set to TRUE and both the width and height arguments are values greater than 0, the image will be resized to the exact given width and height and the aspect ratio will be preserved by using on of the following methods: - ZEBRA_IMAGE_BOXED - the image will be scalled so that it will fit in a box with the given width and height (both width/height will be smaller or equal to the required width/height) and then it will be centered both horizontally and vertically. The blank area will be filled with the color specified by the bgcolor argument. (the blank area will be filled only if the image is not transparent!) - ZEBRA_IMAGE_NOT_BOXED - the image will be scalled so that it could fit in a box with the given width and height but will not be enclosed in a box with given width and height. The new width/ height will be both smaller or equal to the required width/height - ZEBRA_IMAGE_CROP_TOPLEFT - ZEBRA_IMAGE_CROP_TOPCENTER - ZEBRA_IMAGE_CROP_TOPRIGHT - ZEBRA_IMAGE_CROP_MIDDLELEFT - ZEBRA_IMAGE_CROP_CENTER - ZEBRA_IMAGE_CROP_MIDDLERIGHT - ZEBRA_IMAGE_CROP_BOTTOMLEFT - ZEBRA_IMAGE_CROP_BOTTOMCENTER - ZEBRA_IMAGE_CROP_BOTTOMRIGHT For the methods involving crop, first the image is scaled so that both its sides are equal or greater than the respective sizes of the bounding box; next, a region of required width and height will be cropped from indicated region of the resulted image. Default is ZEBRA_IMAGE_CROP_CENTER @param hexadecimal $background_color (Optional) The hexadecimal color (like "#FFFFFF" or "#FFF") of the blank area. See the method argument. When set to -1 the script will preserve transparency for transparent GIF and PNG images. For non-transparent images the background will be white in this case. Default is #FFFFFF. @return boolean Returns TRUE on success or FALSE on error. If FALSE is returned, check the {@link error} property to see what went wrong
$height integer
$method integer
$background_color hexadecimal
return boolean
    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;
    }

Usage Example

function laborator_img($url, $width = 0, $height = 0, $crop = FALSE)
{
    $upload_dir = wp_upload_dir();
    $post_thumbnail_url = '';
    $wpurl = site_url();
    $baseurl = $upload_dir['baseurl'];
    # Get Predefined Image Size
    if (is_string($width)) {
        $image_size = LaboratorImageSizes::get_img_size($width);
        extract($image_size);
    }
    # Get from post ID
    if (is_numeric($url)) {
        $post_thumbnail_id = get_post_thumbnail_id($url);
        if ($post_thumbnail_id) {
            $post_thumbnail_url = wp_get_attachment_url($post_thumbnail_id);
        } else {
            return '';
        }
    } else {
        $post_thumbnail_url = $url;
    }
    # Verify if its on this server
    if (strpos($post_thumbnail_url, $wpurl) != -1) {
        $relative_path = str_replace($wpurl, '', $post_thumbnail_url);
        $relative_path = ltrim($relative_path, '/');
        $absolute_path = ABSPATH . $relative_path;
        $basename = basename($absolute_path);
        # New Image Name
        $thumbnail_name = 'labimg_' . ($width ? "{$width}_" : '') . ($height ? "{$height}_" : '') . ($crop ? "1_" : '') . $basename;
        $thumbnail_path = dirname($absolute_path) . '/' . $thumbnail_name;
        $thumbnail_url = dirname($post_thumbnail_url) . '/' . $thumbnail_name;
        # Check if cached
        if (file_exists($thumbnail_path)) {
            return $thumbnail_url;
        }
        # Create File
        if (file_exists($absolute_path)) {
            # Generate Img
            $img = new Zebra_Image();
            $img->source_path = $absolute_path;
            $img->target_path = $thumbnail_path;
            $img->enlarge_smaller_images = TRUE;
            $img->preserve_aspect_ratio = TRUE;
            if ($crop) {
                $img->resize($width, $height, ZEBRA_IMAGE_CROP_CENTER, '#FFF');
            } else {
                $img->resize($width, $height, ZEBRA_IMAGE_NOT_BOXED, '#FFF');
            }
            return $thumbnail_url;
        }
    }
    return '';
}
All Usage Examples Of Zebra_Image::resize