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); }
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); }
function testUpSizing2Param() { $data = array(); $file_loc = self::copyTestImage('stl.jpg'); $upload_dir = wp_upload_dir(); $new_file = TimberImageHelper::resize($upload_dir['url'] . '/stl.jpg', 500, 300, 'default', true); $location_of_image = TimberImageHelper::get_server_location($new_file); $size = getimagesize($location_of_image); $this->assertEquals(500, $size[0]); $this->assertEquals(300, $size[1]); }
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); } }
/** * 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); }
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 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'); }