function testImageBiggerRetina()
 {
     $file = TestTimberImage::copyTestImage();
     $ret = TimberImageHelper::retina_resize($file, 3);
     $image = new TimberImage($ret);
     $this->assertEquals(4500, $image->width());
 }
Exemplo n.º 2
0
 protected function init()
 {
     TimberTwig::init();
     TimberRoutes::init($this);
     TimberImageHelper::init();
     TimberAdmin::init();
     TimberIntegrations::init();
 }
 function testResizeFileNamingWithUploadsConst()
 {
     define('UPLOADS', 'my/up');
     $file_loc = self::copyTestImage('eastern.jpg');
     $upload_dir = wp_upload_dir();
     $url_src = $upload_dir['url'] . '/eastern.jpg';
     $filename = TimberImageHelper::get_resize_file_url($url_src, 300, 500, 'default');
     $this->assertEquals($upload_dir['url'] . '/eastern-300x500-c-default.jpg', $filename);
 }
 public static function resize($src, $w, $h = 0)
 {
     global $CJG_retina;
     if ($CJG_retina && false) {
         //turned off by Graham 2013-11-19 because of issues it was causing
         $w = $w * 2;
     }
     return parent::resize($src, $w, $h);
 }
Exemplo n.º 5
0
 function testCropHeight()
 {
     $arch = TestTimberImage::copyTestImage('arch.jpg');
     $resized = TimberImageHelper::resize($arch, false, 250);
     $resized = str_replace('http://example.org', '', $resized);
     $resized = TimberUrlHelper::url_to_file_system($resized);
     $is_sized = TestTimberImage::checkSize($resized, 375, 250);
     $this->assertTrue($is_sized);
 }
 /**
  *
  *
  * @param string  $src
  * @param int     $w
  * @param int     $h
  * @param string  $crop
  * @return string
  */
 private static function get_retina_file_name_relative_to_content($src, $mult = 2)
 {
     $path_parts = pathinfo($src);
     $dir_relative_to_content = TimberImageHelper::get_directory_relative_to_content($src);
     $newbase = $path_parts['filename'] . '@' . $mult . 'x';
     // add @2x, @3x, @1.5x, etc.
     $new_name = $newbase . '.' . $path_parts['extension'];
     return $dir_relative_to_content . '/' . $new_name;
 }
 static function add_actions()
 {
     add_action('delete_post', function ($post_id) {
         $post = get_post($post_id);
         $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/jpg');
         if ($post->post_type == 'attachment' && in_array($post->post_mime_type, $image_types)) {
             TimberImageHelper::delete_resized_files_from_url($post->guid);
             TimberImageHelper::delete_letterboxed_files_from_url($post->guid);
         }
     });
 }
Exemplo n.º 8
0
 static function testImageResize()
 {
     $img = 'arch.jpg';
     $upload_dir = wp_upload_dir();
     $destination = $upload_dir['path'] . '/' . $img;
     if (!file_exists($destination)) {
         copy(__DIR__ . '/../assets/' . $img, $destination);
     }
     for ($i = 0; $i < 20; $i++) {
         $upload_dir = wp_upload_dir();
         $img = TimberImageHelper::resize($upload_dir['url'] . '/arch.jpg', 500, 200, 'default', true);
     }
 }
 function testLetterboxSixCharHex()
 {
     $data = array();
     $file_loc = TestTimberImage::copyTestImage('eastern.jpg');
     $upload_dir = wp_upload_dir();
     $new_file = TimberImageHelper::letterbox($upload_dir['url'] . '/eastern.jpg', 500, 500, '#FFFFFF', true);
     $location_of_image = TimberImageHelper::get_server_location($new_file);
     $this->assertTrue(TestTimberImage::checkSize($location_of_image, 500, 500));
     //whats the bg/color of the image
     $image = imagecreatefromjpeg($location_of_image);
     $pixel_rgb = imagecolorat($image, 1, 1);
     $colors = imagecolorsforindex($image, $pixel_rgb);
     $this->assertEquals(255, $colors['red']);
     $this->assertEquals(255, $colors['blue']);
     $this->assertEquals(255, $colors['green']);
 }
 /**
  * Timber creates resized images in the Twig templates. However,
  * these do not get updated when new crops as made.
  */
 public static function delete_timber_resized_images()
 {
     // Gather POST data from the crop resize
     $post_id = $_POST['post'];
     $crop_name = $_POST['size'];
     if (!($image_metadata = wp_get_attachment_metadata($post_id))) {
         throw new \Exception('Unable to get attachment from ID');
     }
     $crop_metadata = $image_metadata['sizes'][$crop_name];
     $crop_file = $crop_metadata['file'];
     $crop_width = $crop_metadata['width'];
     $crop_height = $crop_metadata['height'];
     // get the filename without file extension
     $crop_size_ideal_string = $crop_width . 'x' . $crop_height;
     $matches = [];
     preg_match('/-([0-9]*)x([0-9]*)\\./', $crop_file, $matches);
     if (count($matches) !== 3) {
         throw new \Exception('Could not infer the image size from filename: ' . $crop_file);
     }
     $crop_size_actual_string = $matches[1] . 'x' . $matches[2];
     $crop_fileNoExtension = substr($crop_file, 0, strpos($crop_file, $crop_size_actual_string) + strlen($crop_size_actual_string));
     $crop_file_no_extension_hyphen = $crop_fileNoExtension . '-';
     $upload_directory = wp_upload_dir();
     $upload_path = $upload_directory['path'];
     $dh = opendir($upload_path);
     while (false !== ($upload_filename = readdir($dh))) {
         $upload_filename_no_extension_hyphen = substr($upload_filename, 0, strlen($crop_file_no_extension_hyphen));
         if ($upload_filename_no_extension_hyphen === $crop_file_no_extension_hyphen) {
             // Now capture the image size thats been resized by Timber
             // e.g. 800x0-c-default.jpg => 800 width, 0 height (proportionate)
             $upload_filename_end_part = substr($upload_filename, strlen($upload_filename_no_extension_hyphen));
             $resizeMatches = [];
             preg_match('/([^x]*)x([^-]*)-/', $upload_filename_end_part, $resizeMatches);
             if (count($resizeMatches) < 2) {
                 throw new \Exception('Could not retrieve Timber resize information from image ' . $upload_filename);
             }
             $resizeWidth = $resizeMatches[1];
             $filepathToResize = $upload_directory['url'] . '/' . $crop_file;
             TimberImageHelper::resize($filepathToResize, $resizeWidth, 0, 'default', true);
         }
     }
     closedir($dh);
 }
Exemplo n.º 11
0
 /**
  * @deprecated
  */
 static function get_image_path($iid)
 {
     return TimberImageHelper::get_image_path($iid);
 }
Exemplo n.º 12
0
 function testTimberImageForExtraSlashes()
 {
     add_filter('upload_dir', array($this, '_filter_upload'), 10, 1);
     $post = $this->get_post_with_image();
     $image = $post->thumbnail();
     $resized_520_file = TimberImageHelper::resize($image->src, 520, 500);
     remove_filter('upload_dir', array($this, '_filter_upload'));
     $this->assertFalse(strpos($resized_520_file, '//arch-520x500-c-default.jpg') > -1);
 }
function wp_resize_letterbox($src, $w, $h, $color = '#000000')
{
    //$old_file = TimberHelper::get_full_path($src);
    $urlinfo = parse_url($src);
    $old_file = ABSPATH . $urlinfo['path'];
    $new_file = TimberImageHelper::get_letterbox_file_path($urlinfo['path'], $w, $h);
    $new_file_rel = TimberImageHelper::get_letterbox_file_rel($urlinfo['path'], $w, $h);
    $new_file_boxed = str_replace('-lb-', '-lbox-', $new_file);
    if (file_exists($new_file_boxed)) {
        $new_file_rel = str_replace('-lb-', '-lbox-', $new_file_rel);
        return $new_file_rel;
    }
    $bg = imagecreatetruecolor($w, $h);
    $c = hexrgb($color);
    $white = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
    imagefill($bg, 0, 0, $white);
    $image = wp_get_image_editor($old_file);
    if (!is_wp_error($image)) {
        $current_size = $image->get_size();
        $ow = $current_size['width'];
        $oh = $current_size['height'];
        $new_aspect = $w / $h;
        $old_aspect = $ow / $oh;
        if ($new_aspect > $old_aspect) {
            //taller than goal
            $h_scale = $h / $oh;
            $owt = $ow * $h_scale;
            $y = 0;
            $x = $w / 2 - $owt / 2;
            $oht = $h;
            $image->crop(0, 0, $ow, $oh, $owt, $oht);
        } else {
            $w_scale = $w / $ow;
            $oht = $oh * $w_scale;
            $x = 0;
            $y = $h / 2 - $oht / 2;
            $owt = $w;
            $image->crop(0, 0, $ow, $oh, $owt, $oht);
        }
        $image->save($new_file);
        $func = 'imagecreatefromjpeg';
        $ext = pathinfo($new_file, PATHINFO_EXTENSION);
        if ($ext == 'gif') {
            $func = 'imagecreatefromgif';
        } else {
            if ($ext == 'png') {
                $func = 'imagecreatefrompng';
            }
        }
        $image = $func($new_file);
        imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht);
        $new_file = str_replace('-lb-', '-lbox-', $new_file);
        imagejpeg($bg, $new_file);
        return TimberHelper::get_rel_path($new_file);
    } else {
        TimberHelper::error_log($image);
    }
    return null;
}
Exemplo n.º 14
0
 function testWPMLurlLocal()
 {
     // this test replicates the url issue caused by the WPML language identifier in the url
     // However, WPML can't be installed with composer so this test mocks the WPML plugin
     // WPML uses a filter to alter the home_url
     $home_url_filter = function ($url) {
         return $url . '/en';
     };
     add_filter('home_url', $home_url_filter, -10, 4);
     // test with a local and external file
     $img = 'arch.jpg';
     $img = TestTimberImage::copyTestImage($img);
     $resized = TimberImageHelper::resize($img, 50, 50);
     // make sure the base url has not been duplicated (https://github.com/timber/timber/issues/405)
     $this->assertLessThanOrEqual(1, substr_count($resized, 'example.org'));
     // make sure the image has been resized
     $resized = TimberUrlHelper::url_to_file_system($resized);
     $this->assertTrue(TestTimberImage::checkSize($resized, 50, 50), 'image should be resized');
 }
Exemplo n.º 15
0
 function testThemeImageLetterbox()
 {
     $dest = get_template_directory() . '/images/cardinals.jpg';
     copy(__DIR__ . '/assets/cardinals.jpg', $dest);
     $image = get_template_directory_uri() . '/images/cardinals.jpg';
     $image = str_replace('http://example.org', '', $image);
     $letterboxed = TimberImageHelper::letterbox($image, 600, 300, '#FF0000');
     $this->assertFileExists(get_template_directory() . '/images/cardinals-lbox-600x300-FF0000.jpg');
     unlink(get_template_directory() . '/images/cardinals-lbox-600x300-FF0000.jpg');
 }
Exemplo n.º 16
0
 function testImageHelperInit()
 {
     $helper = TimberImageHelper::init();
     $this->assertTrue(defined('WP_CONTENT_SUBDIR'));
 }
 /**
  * Performs the actual image manipulation,
  * including saving the target file.
  *
  * @param  string $load_filename filepath (not URL) to source file
  *                               (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
  * @param  string $save_filename filepath (not URL) where result file should be saved
  *                               (ex: /src/var/www/wp-content/uploads/my-pic-300x200-c-default.jpg)
  * @return bool                  true if everything went fine, false otherwise
  */
 public function run($load_filename, $save_filename)
 {
     //should be resized by gif resizer
     if (TimberImageHelper::is_animated_gif($load_filename)) {
         //attempt to resize
         //return if successful
         //proceed if not
         $gif = self::run_animated_gif($load_filename, $save_filename);
         if ($gif) {
             return true;
         }
     }
     $image = wp_get_image_editor($load_filename);
     if (!is_wp_error($image)) {
         $crop = self::get_target_sizes($load_filename);
         $image->crop($crop['x'], $crop['y'], $crop['src_w'], $crop['src_h'], $crop['target_w'], $crop['target_h']);
         $result = $image->save($save_filename);
         if (is_wp_error($result)) {
             // @codeCoverageIgnoreStart
             TimberHelper::error_log('Error resizing image');
             TimberHelper::error_log($result);
             return false;
             // @codeCoverageIgnoreEnd
         } else {
             return true;
         }
     } else {
         if (isset($image->error_data['error_loading_image'])) {
             // @codeCoverageIgnoreStart
             TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']);
         } else {
             TimberHelper::error_log($image);
             // @codeCoverageIgnoreEnd
         }
     }
 }
Exemplo n.º 18
0
 /**
  * Deletes all resized versions of an image when the source is deleted
  */
 protected static function add_actions()
 {
     add_action('delete_attachment', function ($post_id) {
         $post = get_post($post_id);
         $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/jpg');
         if (in_array($post->post_mime_type, $image_types)) {
             $attachment = new TimberImage($post_id);
             if ($attachment->file_loc) {
                 TimberImageHelper::delete_generated_files($attachment->file_loc);
             }
         }
     });
 }
Exemplo n.º 19
0
 function testIsNotGif()
 {
     $arch = TestTimberImage::copyTestImage('arch.jpg');
     $this->assertFalse(TimberImageHelper::is_animated_gif($arch));
 }