/** * 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 } } }
function testAnimatedGifResize() { $image = self::copyTestImage('robocop.gif'); $data = array('crop' => 'default'); $data['size'] = array('width' => 90, 'height' => 90); $upload_dir = wp_upload_dir(); $url = $upload_dir['url'] . '/robocop.gif'; $data['test_image'] = $url; Timber::compile('assets/image-test.twig', $data); $resized_path = $upload_dir['path'] . '/robocop-' . $data['size']['width'] . 'x' . $data['size']['height'] . '-c-' . $data['crop'] . '.gif'; $this->addFile($resized_path); $this->assertFileExists($resized_path); $this->assertTrue(TimberImageHelper::is_animated_gif($resized_path)); }
function testIsNotGif() { $arch = TestTimberImage::copyTestImage('arch.jpg'); $this->assertFalse(TimberImageHelper::is_animated_gif($arch)); }