Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

First off, I know it's better practice to use plugins separately than to integrate them in functions.php but this is for a theme framework that will be deployed to multiple sites and it will be easier to have it packaged as part of the theme.

I am having trouble using the following code from the plugin located here. My PHP knowledge is limited, I have tried copying and pasting the code directly but it does not work, but works fine when used as a plugin.

class Filosofo_Custom_Image_Sizes {

public function __construct()
{
    add_filter('image_downsize', array(&$this, 'filter_image_downsize'), 99, 3);

}

/**
 * Callback for the "image_downsize" filter.
 *
 * @param bool $ignore A value meant to discard unfiltered info returned from this filter.
 * @param int $attachment_id The ID of the attachment for which we want a certain size.
 * @param string $size_name The name of the size desired.
 */
public function filter_image_downsize($ignore = false, $attachment_id = 0, $size_name = 'thumbnail')
{
    global $_wp_additional_image_sizes;

    $attachment_id = (int) $attachment_id;
    $size_name = trim($size_name);

    $meta = wp_get_attachment_metadata($attachment_id);

    /* the requested size does not yet exist for this attachment */
    if (
        empty( $meta['sizes'] ) ||
        empty( $meta['sizes'][$size_name] )
    ) {
        // let's first see if this is a registered size
        if ( isset( $_wp_additional_image_sizes[$size_name] ) ) {
            $height = (int) $_wp_additional_image_sizes[$size_name]['height'];
            $width = (int) $_wp_additional_image_sizes[$size_name]['width'];
            $crop = (bool) $_wp_additional_image_sizes[$size_name]['crop'];

        // if not, see if name is of form [width]x[height] and use that to crop
        } else if ( preg_match('#^(\d+)x(\d+)$#', $size_name, $matches) ) {
            $height = (int) $matches[2];
            $width = (int) $matches[1];
            $crop = true;
        }

        if ( ! empty( $height ) && ! empty( $width ) ) {
            $resized_path = $this->_generate_attachment($attachment_id, $width, $height, $crop);
            $fullsize_url = wp_get_attachment_url($attachment_id);

            $file_name = basename($resized_path);

            $new_url = str_replace(basename($fullsize_url), $file_name, $fullsize_url);

            if ( ! empty( $resized_path ) ) {
                $meta['sizes'][$size_name] = array(
                    'file' => $file_name,
                    'width' => $width,
                    'height' => $height,
                );

                wp_update_attachment_metadata($attachment_id, $meta);

                return array(
                    $new_url,
                    $width,
                    $height,
                    true
                );
            }
        }
    }

    return false;
}

/**
 * Creates a cropped version of an image for a given attachment ID.
 *
 * @param int $attachment_id The attachment for which to generate a cropped image.
 * @param int $width The width of the cropped image in pixels.
 * @param int $height The height of the cropped image in pixels.
 * @param bool $crop Whether to crop the generated image.
 * @return string The full path to the cropped image.  Empty if failed.
 */
private function _generate_attachment($attachment_id = 0, $width = 0, $height = 0, $crop = true)
{
    $attachment_id = (int) $attachment_id;
    $width = (int) $width;
    $height = (int) $height;
    $crop = (bool) $crop;

    $original_path = get_attached_file($attachment_id);

    // fix a WP bug up to 2.9.2
    if ( ! function_exists('wp_load_image') ) {
        require_once ABSPATH . 'wp-admin/includes/image.php';
    }

    $resized_path = @image_resize($original_path, $width, $height, $crop);

    if ( 
        ! is_wp_error($resized_path) && 
        ! is_array($resized_path)
    ) {
        return $resized_path;

    // perhaps this image already exists.  If so, return it.
    } else {
        $orig_info = pathinfo($original_path);
        $suffix = "{$width}x{$height}";
        $dir = $orig_info['dirname'];
        $ext = $orig_info['extension'];
        $name = basename($original_path, ".{$ext}"); 
        $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
        if ( file_exists( $destfilename ) ) {
            return $destfilename;
        }
    }

    return '';
}
}

function initialize_custom_image_sizes()
{
new Filosofo_Custom_Image_Sizes;
}

add_action('plugins_loaded', 'initialize_custom_image_sizes');

I'm aware I can achieve the same results using TimThumb but I would much prefer to use the built in functionality of WordPress

Here's a link to the plugin in question:-

http://wordpress.org/extend/plugins/custom-image-sizes/

Thanks in advance!

Steve

share|improve this question

2 Answers 2

up vote 2 down vote accepted
add_action('plugins_loaded', 'initialize_custom_image_sizes');

plugins_loaded is fired off way before functions.php is included. Change that hook to init.

http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_a_Typical_Request

share|improve this answer
    
This worked. I used add_action('init', 'initialize_custom_image_sizes'); Tried voting you up but I need more reputation. Thanks a lot for your help, highly appreciated! –  Steve Mar 29 '12 at 20:31
    
@Steve Great to hear that. Don't forget to accept an answer. –  soulseekah Mar 29 '12 at 20:33
    
Maybe instead of init you could use setup_theme or after_setup_theme since it is used in a theme. –  xaedes Mar 29 '12 at 21:35
    
Anything after the functions.php file is included works. It depends on what hooks will be required by the file. setup_theme and after_setup_theme can miss out on some important actions and filters like wp and query. Everything depends on the context. –  soulseekah Mar 29 '12 at 21:55
    
Just to confirm setup_theme and after_setup_theme both work fine, but as this function will be used on the fly, init sounds like the best candidate. Thanks for the responses guys, this has helped me understand WordPress hooks even better! –  Steve Mar 29 '12 at 22:44

That action will never be fired when it is no plugin :

add_action('plugins_loaded', 'initialize_custom_image_sizes');

Instead just manually call initialize_custom_image_sizes

share|improve this answer
    
It will be fired, but it will be too late for functions.php to know. –  soulseekah Mar 29 '12 at 20:20
    
Where would I call this? Sorry to sound dim! Thanks for the fast answer. Would add_action('the_post', 'initialize_custom_image_sizes'); work? –  Steve Mar 29 '12 at 20:24
    
@Soulseekah Yeah, you are right. Your solution with changing the hook to init is much better than just calling the function. –  xaedes Mar 29 '12 at 21:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.