/** * Set an image representing the content of the page * * @since 1.0.0 * * @param string|\Twitter\Cards\Components\Image $url absolute URL of an image file * @param int $width width of the image in whole pixels * @param int $height height of the image in whole pixels * * @return __CLASS__ support chaining */ public function setImage($url, $width = 0, $height = 0) { if (!$url) { return $this; } if (!(is_int($width) && $width >= 0)) { $width = 0; } if (!(is_int($height) && $height >= 0)) { $height = 0; } $image = null; $preset = false; if (is_a($url, '\\Twitter\\Cards\\Components\\Image')) { $preset = true; $image = $url; $width = $url->getWidth(); $height = $url->getHeight(); } elseif (is_string($url)) { try { $image = new \Twitter\Cards\Components\Image($url); } catch (Exception $e) { return $this; } } if (!$image) { return $this; } // only set dimensions if both width and height exist if (is_int($width) && $width && is_int($height) && $height) { // test if minimum width and height requirements are met for the card type if (defined(__CLASS__ . '::MIN_IMAGE_WIDTH') && defined(__CLASS__ . '::MIN_IMAGE_HEIGHT')) { if ($width >= self::MIN_IMAGE_WIDTH && $height >= self::MIN_IMAGE_HEIGHT) { if (!$preset) { $image->setWidth($width); $image->setHeight($height); } } else { // do not store image if minimum requirements not met return $this; } } else { if (!$preset) { $image->setWidth($width); $image->setHeight($height); } } } $this->image = $image; return $this; }
/** * Convert a WordPress image attachment to a Twitter Card image object * * @since 1.0.0 * * @param int $attachment_id WordPress attachment ID * @param string $size desired size * * @return \Twitter\Cards\Components\Image|null Twitter Card image */ public function attachmentToTwitterImage($attachment_id, $size = 'full') { if (!(is_string($size) && $size)) { $size = 'full'; } // request large version of image if full version exceeds filesize limit if ('full' === $size) { $attached_file = get_attached_file($attachment_id); if ($attached_file && file_exists($attached_file)) { $bytes = filesize($attached_file); if ($bytes && $bytes > self::MAX_FILESIZE) { /** * Filter the intermediate image size to be provided for Twitter thumbnail when a full-size image exceeds Twitter's filesize limit * * Twitter will consume the largest available image under the filesize limit and generate thumbnails appropriate for Twitter Card display in various dimension and DPI contexts * * @since 1.0.0 * * @param string $size The intermediate size. Default: large * @param int $attachment_id Attachment identifier */ $intermediate_size = apply_filters('twitter_card_intermediate_image_size', 'large', $attachment_id); // check filtered intermediate size to avoid possible infinite loop if (!$intermediate_size || 'full' === $intermediate_size || !has_image_size($intermediate_size)) { return; } return $handler->attachmentToTwitterImage($attachment_id, $intermediate_size); } unset($bytes); } unset($attached_file); } list($url, $width, $height) = wp_get_attachment_image_src($attachment_id, $size); if (empty($url)) { return; } $image = new \Twitter\Cards\Components\Image($url); if (!empty($width)) { $width = absint($width); if ($width) { if ($width < $this->min_width) { // reject if image width below required width return; } $image->setWidth($width); } // width and height are a resizing hint. must exist as a pair if (!empty($height)) { $height = absint($height); if ($height) { if ($height < $this->min_height) { // reject if image height below required height return; } $image->setHeight($height); } } } return $image; }
/** * Add an image representing the content of the page * * @since 1.0.0 * * @param string|\Twitter\Cards\Components\Image $url absolute URL of an image file * @param int $width width of the image in whole pixels * @param int $height height of the image in whole pixels * * @return __CLASS__ support chaining */ public function addImage($url, $width = 0, $height = 0) { // URL required if (!$url) { return $this; } if (!(is_int($width) && $width >= 0)) { $width = 0; } if (!(is_int($height) && $height >= 0)) { $height = 0; } // have we already filled the image allotment? if (defined(__CLASS__ . '::MAX_IMAGES') && $this->image_count === self::MAX_IMAGES) { return $this; } $image = null; $preset = false; if (is_a($url, '\\Twitter\\Cards\\Components\\Image')) { // support overloading the function $image = $url; $url = $url->getURL(); if (isset($this->images[$url])) { return $this; } $preset = true; $width = $image->getWidth(); $height = $image->getHeight(); } elseif (is_string($url)) { if (isset($this->images[$url])) { return $this; } try { $image = new \Twitter\Cards\Components\Image($url); } catch (Exception $e) { return $this; } } if (!$image) { return $this; } // only set dimensions if both width and height exist if (is_int($width) && $width && is_int($height) && $height) { // test if minimum width and height requirements are met for the card type if (defined(__CLASS__ . '::MIN_IMAGE_WIDTH') && defined(__CLASS__ . '::MIN_IMAGE_HEIGHT')) { if ($width >= self::MIN_IMAGE_WIDTH && $height >= self::MIN_IMAGE_HEIGHT) { if (!$preset) { $image->setWidth($width); $image->setHeight($height); } } else { // do not store image if minimum requirements not met return $this; } } else { if (!$preset) { $image->setWidth($width); $image->setHeight($height); } } } $this->images[$url] = $image; $this->image_count = count($this->images); return $this; }