public function sanitize($amp_attributes = array()) { $nodes = $this->dom->getElementsByTagName(self::$tag); $num_nodes = $nodes->length; if (0 === $num_nodes) { return; } for ($i = $num_nodes - 1; $i >= 0; $i--) { $node = $nodes->item($i); $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array($node); if (!array_key_exists('src', $old_attributes)) { $node->parentNode->removeChild($node); continue; } $new_attributes = $this->filter_attributes($old_attributes); if (!isset($new_attributes['width']) || !isset($new_attributes['height'])) { $dimensions = AMP_Image_Dimension_Extractor::extract($new_attributes['src']); if ($dimensions) { $new_attributes['width'] = $dimensions[0]; $new_attributes['height'] = $dimensions[1]; } } $new_attributes = $this->enforce_sizes_attribute($new_attributes); $new_attributes = array_merge($new_attributes, $amp_attributes); if ($this->is_gif_url($new_attributes['src'])) { $this->did_convert_elements = true; $new_tag = 'amp-anim'; } else { $new_tag = 'amp-img'; } $new_node = AMP_DOM_Utils::create_node($this->dom, $new_tag, $new_attributes); $node->parentNode->replaceChild($new_node, $node); } }
function test__valid_attachment_and_dimensions() { $source = wp_get_attachment_url($this->_attachment_id); $expected = array(498, 113); $dimensions = AMP_Image_Dimension_Extractor::extract_from_attachment_metadata(false, $source); $this->assertEquals($expected, $dimensions); }
function test__amp_wp_user_agent() { $expected = 'amp-wp, v' . AMP__VERSION . ', '; $user_agent = AMP_Image_Dimension_Extractor::get_default_user_agent(''); $user_agent = substr($user_agent, 0, strlen($expected)); $this->assertEquals($expected, $user_agent); }
private static function register_callbacks() { self::$callbacks_registered = true; add_filter('amp_extract_image_dimensions', array(__CLASS__, 'extract_from_attachment_metadata'), 10, 2); add_filter('amp_extract_image_dimensions', array(__CLASS__, 'extract_by_downloading_image'), 999, 2); // Run really late since this is our last resort do_action('amp_extract_image_dimensions_callbacks_registered'); }
/** * Figure out width and height attribute values for images that don't have them by * attempting to determine actual dimensions and setting reasonable defaults otherwise. * * @param array $need_dimensions List of Img src url to node mappings corresponding to images that need dimensions. */ private function determine_dimensions($need_dimensions) { $dimensions_by_url = AMP_Image_Dimension_Extractor::extract(array_keys($need_dimensions)); foreach ($dimensions_by_url as $url => $dimensions) { foreach ($need_dimensions[$url] as $node) { // Provide default dimensions for images whose dimensions we couldn't fetch. if (false === $dimensions) { $width = isset($this->args['content_max_width']) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH; $height = self::FALLBACK_HEIGHT; $node->setAttribute('width', $width); $node->setAttribute('height', $height); $class = $node->hasAttribute('class') ? $node->getAttribute('class') . ' amp-wp-unknown-size' : 'amp-wp-unknown-size'; $node->setAttribute('class', $class); } else { $node->setAttribute('width', $dimensions['width']); $node->setAttribute('height', $dimensions['height']); } } } }
public function sanitize() { $nodes = $this->dom->getElementsByTagName(self::$tag); $num_nodes = $nodes->length; if (0 === $num_nodes) { return; } for ($i = $num_nodes - 1; $i >= 0; $i--) { $node = $nodes->item($i); $old_attributes = AMP_DOM_Utils::get_node_attributes_as_assoc_array($node); // Added data-src for lazy-loaded imgs. if (!array_key_exists('src', $old_attributes) && !array_key_exists('data-src', $old_attributes)) { $node->parentNode->removeChild($node); continue; } $new_attributes = $this->filter_attributes($old_attributes); // Try to extract dimensions for the image, if not set. if (!isset($new_attributes['width']) || !isset($new_attributes['height'])) { $dimensions = AMP_Image_Dimension_Extractor::extract($new_attributes['src']); if (is_array($dimensions)) { $new_attributes['width'] = $dimensions[0]; $new_attributes['height'] = $dimensions[1]; } } // Final fallback when we have no dimensions. if (!isset($new_attributes['width']) || !isset($new_attributes['height'])) { $new_attributes['width'] = isset($this->args['content_max_width']) ? $this->args['content_max_width'] : self::FALLBACK_WIDTH; $new_attributes['height'] = self::FALLBACK_HEIGHT; $this->add_or_append_attribute($new_attributes, 'class', 'amp-wp-unknown-size'); } $new_attributes = $this->enforce_sizes_attribute($new_attributes); if ($this->is_gif_url($new_attributes['src'])) { $this->did_convert_elements = true; $new_tag = 'amp-anim'; } else { $new_tag = 'amp-img'; } $new_node = AMP_DOM_Utils::create_node($this->dom, $new_tag, $new_attributes); $node->parentNode->replaceChild($new_node, $node); } }
/** * @expectedException PHPUnit_Framework_Error_Warning */ function test__invalid_image_file() { $source = AMP_IMG_DIMENSION_TEST_INVALID_FILE; $expected = false; $dimensions = AMP_Image_Dimension_Extractor::extract_by_downloading_image(false, $source); $this->assertEquals($expected, $dimensions); }
private static function register_callbacks() { self::$callbacks_registered = true; add_filter('amp_extract_image_dimensions', array(__CLASS__, 'extract_from_filename'), 10, 2); add_filter('amp_extract_image_dimensions', array(__CLASS__, 'extract_from_attachment_metadata'), 10, 2); }
private static function register_callbacks() { self::$callbacks_registered = true; add_filter('amp_extract_image_dimensions_batch', array(__CLASS__, 'extract_by_downloading_images'), 999, 1); do_action('amp_extract_image_dimensions_batch_callbacks_registered'); }