A8C_Files::image_resize PHP Method

image_resize() public method

Image resizing service. Takes place of image_downsize().
See also: image_downsize()
public image_resize ( boolean $ignore, integer $id, array | string $size ) : boolean | array
$ignore boolean Unused.
$id integer Attachment ID for image.
$size array | string Optional, default is 'medium'. Size of image, either array or string.
return boolean | array False on failure, array on success.
    function image_resize($ignore, $id, $size)
    {
        global $_wp_additional_image_sizes, $post;
        $content_width = isset($GLOBALS['content_width']) ? $GLOBALS['content_width'] : null;
        $crop = false;
        $args = array();
        // For resize requests coming from an image's attachment page, override
        // the supplied $size and use the user-defined $content_width if the
        // theme-defined $content_width has been manually passed in.
        if (is_attachment() && $id === $post->ID) {
            if (is_array($size) && !empty($size) && isset($GLOBALS['content_width']) && $size[0] == $GLOBALS['content_width']) {
                $size = array($content_width, $content_width);
            }
        }
        if ('tellyworth' == $size) {
            // 'full' is reserved because some themes use it (see image_constrain_size_for_editor)
            $_max_w = 4096;
            $_max_h = 4096;
        } elseif ('thumbnail' == $size) {
            $_max_w = get_option('thumbnail_size_w');
            $_max_h = get_option('thumbnail_size_h');
            if (!$_max_w && !$_max_h) {
                $_max_w = 128;
                $_max_h = 96;
            }
            if (get_option('thumbnail_crop')) {
                $crop = true;
            }
        } elseif ('medium' == $size) {
            $_max_w = get_option('medium_size_w');
            $_max_h = get_option('medium_size_h');
            if (!$_max_w && !$_max_h) {
                $_max_w = 300;
                $_max_h = 300;
            }
        } elseif ('large' == $size) {
            $_max_w = get_option('large_size_w');
            $_max_h = get_option('large_size_h');
        } elseif (is_array($size)) {
            $_max_w = $w = $size[0];
            $_max_h = $h = $size[1];
        } elseif (!empty($_wp_additional_image_sizes[$size])) {
            $_max_w = $w = $_wp_additional_image_sizes[$size]['width'];
            $_max_h = $h = $_wp_additional_image_sizes[$size]['height'];
            $crop = $_wp_additional_image_sizes[$size]['crop'];
        } elseif ($content_width > 0) {
            $_max_w = $content_width;
            $_max_h = 0;
        } else {
            $_max_w = 1024;
            $_max_h = 0;
        }
        // Constrain default image sizes to the theme's content width, if available.
        if ($content_width > 0 && in_array($size, array('thumbnail', 'medium', 'large'))) {
            $_max_w = min($_max_w, $content_width);
        }
        $resized = false;
        $img_url = wp_get_attachment_url($id);
        /**
         * Filter the original image Photon-compatible parameters before changes are 
         *
         * @param array|string $args Array of Photon-compatible arguments.
         * @param string $image_url Image URL.
         */
        $args = apply_filters('vip_go_image_resize_pre_args', $args, $image_url);
        if (!$crop) {
            $imagedata = wp_get_attachment_metadata($id);
            if (!empty($imagedata['width']) || !empty($imagedata['height'])) {
                $h = $imagedata['height'];
                $w = $imagedata['width'];
                list($w, $h) = wp_constrain_dimensions($w, $h, $_max_w, $_max_h);
                if ($w < $imagedata['width'] || $h < $imagedata['height']) {
                    $resized = true;
                }
            } else {
                $w = $_max_w;
                $h = $_max_h;
            }
        }
        if ($crop) {
            $constrain = false;
            $imagedata = wp_get_attachment_metadata($id);
            if ($imagedata) {
                $w = $imagedata['width'];
                $h = $imagedata['height'];
            }
            if (empty($w)) {
                $w = $_max_w;
            }
            if (empty($h)) {
                $h = $_max_h;
            }
            // If the image width is bigger than the allowed max, scale it to match
            if ($w >= $_max_w) {
                $w = $_max_w;
            } else {
                $constrain = true;
            }
            // If the image height is bigger than the allowed max, scale it to match
            if ($h >= $_max_h) {
                $h = $_max_h;
            } else {
                $constrain = true;
            }
            if ($constrain) {
                list($w, $h) = wp_constrain_dimensions($w, $h, $_max_w, $_max_h);
            }
            $args['w'] = $w;
            $args['h'] = $h;
            $args['crop'] = '1';
            $resized = true;
        } elseif ('full' != $size) {
            $args['w'] = $w;
            $resized = true;
        }
        if (is_array($args)) {
            // Convert values that are arrays into strings
            foreach ($args as $arg => $value) {
                if (is_array($value)) {
                    $args[$arg] = implode(',', $value);
                }
            }
            // Encode values
            // See http://core.trac.wordpress.org/ticket/17923
            $args = rawurlencode_deep($args);
        }
        $img_url = add_query_arg($args, $img_url);
        return array($img_url, $w, $h, $resized);
    }