function testBackgroundFillOnTransparentImage()
 {
     $path = dirname(__FILE__) . '/images/transparent.png';
     // check the iamge is transparent
     $this->assertImageAlphaAtPoint($path, array(0, 0), 127);
     $image = new WP_Thumb($path, 'width=400&height=100&crop=1&background_fill=auto&cache=0&return=path');
     $file = $image->returnImage();
     $this->assertImageAlphaAtPoint($file, array(0, 0), 127);
 }
 function testResizeRemote404Image()
 {
     // Image is 275x95
     $url = 'http://www.google.com/intl/en_com/images/srpr/logoawd3w.png';
     $image = new WP_Thumb($url, "crop=1&return=path&width=80&height=80&cache=0");
     $file = $image->returnImage();
     $this->assertEquals($url, $file);
     $this->assertTrue($image->errored());
 }
 function testCropFromTopCenterWithResize()
 {
     // 20x10 image
     $path = dirname(__FILE__) . '/images/boxed-rectangle.png';
     $image = new WP_Thumb($path, array('width' => 10, 'height' => 4, 'cache' => false, 'return' => 'path', 'crop' => true, 'crop_from_position' => 'center,center', 'resize' => true));
     $file = $image->returnImage();
     $this->assertImageRGBAtPoint($file, array(0, 0), array(255, 255, 255));
     $this->assertImageRGBAtPoint($file, array(0, 1), array(255, 255, 255));
     $this->assertImageRGBAtPoint($file, array(9, 0), array(255, 255, 255));
     $this->assertImageRGBAtPoint($file, array(9, 1), array(255, 255, 255));
     $this->assertImageRGBAtPoint($file, array(1, 1), array(0, 0, 0));
 }
Example #4
0
 function testCropLargerThanSingleDimention()
 {
     $path = dirname(__FILE__) . '/images/google.png';
     list($width, $height) = getimagesize($path);
     $this->assertNotNull($width);
     $this->assertNotNull($height);
     $image = new WP_Thumb($path, "width=200&height=300&crop=1&resize=0&cache=0&return=path");
     $file = $image->returnImage();
     $this->assertContains('/cache/', $file);
     $this->assertContains(WP_CONTENT_DIR, $file);
     list($new_width, $new_height) = getimagesize($file);
     $this->assertEquals($new_width, 200, 'Width is not expected');
     $this->assertEquals($new_height, $height, 'Height is not expcted');
 }
Example #5
0
 function testResizeProportionalLargeThanSourceImage()
 {
     $path = dirname(__FILE__) . '/images/google.png';
     list($width, $height) = getimagesize($path);
     $this->assertNotNull($width);
     $this->assertNotNull($height);
     $width = $width * 2;
     $image = new WP_Thumb($path, "width={$width}&crop=0&cache=0&return=path");
     $file = $image->returnImage();
     $this->assertContains('/cache/', $file);
     $this->assertContains(WP_CONTENT_DIR, $file);
     list($new_width, $new_height) = getimagesize($file);
     $this->assertEquals($new_width, $width / 2, 'Width is not expected');
     $this->assertEquals($new_height, floor($height), 'Height is not expected');
 }
 function testBackgroundFillOnWhiteImage()
 {
     $path = dirname(__FILE__) . '/images/google.png';
     list($width, $height) = getimagesize($path);
     $this->assertNotNull($width);
     $this->assertNotNull($height);
     $image = new WP_Thumb($path, "width=100&height=100&cache=0&return=path&background_fill=255255255");
     $file = $image->returnImage();
     $this->assertContains('/cache/', $file);
     $this->assertContains(WP_CONTENT_DIR, $file);
     list($new_width, $new_height) = getimagesize($file);
     $this->assertEquals($new_width, 100, 'Width is not expected');
     $this->assertEquals($new_height, 100, 'Height is not expcted');
     $this->assertImageRGBAtPoint($file, array(1, 1), array(255, 255, 255));
 }
Example #7
0
/**
 * Hook WP Thumb into the WordPress image functions
 *
 * Usage `the_post_thumbnail( 'width=100&height=200&crop=1' );`
 *
 * @param null  $null
 * @param int   $id
 * @param array $args
 * @return null
 */
function wpthumb_post_image($null, $id, $args)
{
    // check if $args is a WP Thumb argument list, or native WordPress one
    // wp thumb looks like this: 'width=300&height=120&crop=1'
    // native looks like 'thumbnail'
    if (is_string($args) && !strpos((string) $args, '=')) {
        $original_args = $args;
        if ($original_args === ($args = apply_filters('wpthumb_create_args_from_size', $args))) {
            return $null;
        }
    }
    $args = wp_parse_args($args);
    if (!empty($args[0])) {
        $args['width'] = $args[0];
    }
    if (!empty($args[1])) {
        $args['height'] = $args[1];
    }
    if (!empty($args['crop']) && $args['crop'] && empty($args['crop_from_position'])) {
        $args['crop_from_position'] = get_post_meta($id, 'wpthumb_crop_pos', true);
    }
    if (empty($path)) {
        $path = get_attached_file($id);
    }
    $path = apply_filters('wpthumb_post_image_path', $path, $id, $args);
    $args = apply_filters('wpthumb_post_image_args', $args, $id);
    $image = new WP_Thumb($path, $args);
    $args = $image->getArgs();
    extract($args);
    if (!$image->errored()) {
        $image_src = $image->returnImage();
        $crop = (bool) empty($crop) ? false : $crop;
        if (!$image->errored() && ($image_meta = @getimagesize($image->getCacheFilePath()))) {
            $html_width = $image_meta[0];
            $html_height = $image_meta[1];
        } else {
            $html_width = $html_height = false;
        }
    } else {
        $html_width = $width;
        $html_height = $height;
        $image_src = $image->getFileURL();
    }
    return array($image_src, $html_width, $html_height, true);
}
Example #8
0
 function testWatermarkPostResize()
 {
     // black 100 x 100 image
     $path = dirname(__FILE__) . '/images/black.png';
     $white = dirname(__FILE__) . '/images/white-10.png';
     $image = new WP_Thumb($path, array('width' => 10, 'height' => 10, 'cache' => false, 'return' => 'path', 'watermark_options' => array('mask' => $white, 'position' => 'top,left', 'padding' => 0, 'pre_resize' => false)));
     $file = $image->returnImage();
     $this->assertImageRGBAtPoint($file, array(0, 0), array(255, 255, 255));
     $this->assertImageRGBAtPoint($file, array(9, 9), array(255, 255, 255));
 }
Example #9
0
/**
 * Hook WP Thumb into the WordPress image functions
 *
 * Usage `the_post_thumbnail( 'width=100&height=200&crop=1' );`
 *
 * @param null $null
 * @param int $id
 * @param array $args
 * @return null
 */
function wpthumb_post_image($null, $id, $args)
{
    // check if $args is a WP Thumb argument list, or native WordPress one
    // wp thumb looks like this: 'width=300&height=120&crop=1'
    // native looks like 'thumbnail'...|array( 300, 300 )
    if (is_string($args) && !strpos((string) $args, '=')) {
        global $_wp_additional_image_sizes;
        // Convert keyword sizes to heights & widths.
        if ($args == 'thumbnail') {
            $new_args = array('width' => get_option('thumbnail_size_w'), 'height' => get_option('thumbnail_size_h'), 'crop' => get_option('thumbnail_crop'));
        } elseif ($args == 'medium') {
            $new_args = array('width' => get_option('medium_size_w'), 'height' => get_option('medium_size_h'));
        } elseif ($args == 'large') {
            $new_args = array('width' => get_option('large_size_w'), 'height' => get_option('large_size_h'));
        } elseif (!empty($_wp_additional_image_sizes) && array_key_exists($args, $_wp_additional_image_sizes)) {
            $new_args = array('width' => $_wp_additional_image_sizes[$args]['width'], 'height' => $_wp_additional_image_sizes[$args]['height'], 'crop' => $_wp_additional_image_sizes[$args]['crop'], 'image_size' => $args);
        } elseif ($args != ($new_filter_args = apply_filters('wpthumb_create_args_from_size', $args))) {
            $new_args = $new_filter_args;
        } else {
            $new_args = null;
        }
        if (!$new_args) {
            return $null;
        }
        $args = $new_args;
    }
    $args = wp_parse_args($args);
    if (!empty($args[0])) {
        $args['width'] = $args[0];
    }
    if (!empty($args[1])) {
        $args['height'] = $args[1];
    }
    if (!empty($args['crop']) && $args['crop'] && empty($args['crop_from_position'])) {
        $args['crop_from_position'] = get_post_meta($id, 'wpthumb_crop_pos', true);
    }
    if (empty($path)) {
        $path = get_attached_file($id);
    }
    $path = apply_filters('wpthumb_post_image_path', $path, $id, $args);
    $args = apply_filters('wpthumb_post_image_args', $args, $id);
    $image = new WP_Thumb($path, $args);
    $args = $image->getArgs();
    extract($args);
    if (!$image->errored()) {
        $image_src = $image->returnImage();
        $crop = (bool) empty($crop) ? false : $crop;
        if (!$image->errored() && ($image_meta = @getimagesize($image->getCacheFilePath()))) {
            $html_width = $image_meta[0];
            $html_height = $image_meta[1];
        } else {
            $html_width = $html_height = false;
        }
    } else {
        $html_width = $width;
        $html_height = $height;
        $image_src = $image->getFileURL();
    }
    return array($image_src, $html_width, $html_height, true);
}