Skip to content

Build an img code for oEmbed photo responses - #581

Closed
Vitorinox wants to merge 1 commit into
php-embed:masterfrom
Vitorinox:fix/oembed-photo-code
Closed

Vitorinox wants to merge 1 commit into
php-embed:masterfrom
Vitorinox:fix/oembed-photo-code

Conversation

@Vitorinox

Copy link
Copy Markdown
Collaborator

Problem

oEmbed responses of type: photo have no html field. The spec puts the image in url, with width and height. The generic Code detector only reads html, so for these providers $info->code is null even though the image is right there.

Live reproduction (GIPHY)

$info = (new Embed\Embed())->get('https://giphy.com/gifs/cat-funny-JIX9t2j0ZTN9S');
var_dump($info->getOEmbed()->str('type')); // "photo"
var_dump($info->code);
  • master (4.5.1): code is null.
  • This branch: <img src="https://media0.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" alt="Cat Working GIF - Find &amp; Share on GIPHY" width="480" height="480" />, width 480, height 480.

The same happens live with DeviantArt (https://www.deviantart.com/sakimichan/art/Misty-510056679).

What changes

src/Detectors/Code.php: when the oEmbed response has no html, and only then, a new detectFromOEmbedPhoto() checks for type: photo with a url. It builds the tag with Embed\html('img', ...) (src, alt from title, width, height when present, all escaped) and returns an EmbedCode with the width and height.

  • An oEmbed html still wins, so providers that send both are unchanged.
  • Other types without html (for example link) still fall through to OpenGraph, Twitter and content type, as before.
  • PHP 7.4 compatible, no new dependencies, no public API change.

Fixtures

Three page fixtures change from 'code' => null to an <img> code, because their cached oEmbed responses are type: photo:

  • tests/fixtures/www.deviantart.com.art-misty-510056679.php
  • tests/fixtures/pachunka.deviantart.com.art-cope-145564099.php
  • tests/fixtures/www.23hq.com.zzleeper-photo-16600737.php

They were regenerated from the existing cache only (no network). No other fixture changes.

Tests

New tests/OEmbedPhotoCodeTest.php, offline (fake HTTP client, no network):

  • photo with title and size builds an escaped <img> with width and height
  • photo without size omits the dimensions
  • html still wins over the photo url
  • a non photo response without html still has no code

Results:

  • PHP 8.5.12: OK (241 tests, 591 assertions)
  • PHP 7.4.33: OK (241 tests, 591 assertions)
  • PHPStan: no errors

Backward compatibility

No BC break. The only visible difference is that code is now filled for oEmbed photo responses where it used to be null.

@Vitorinox

Copy link
Copy Markdown
Collaborator Author

Superseded by #584 (same commit, branch moved from my fork to this repository).

@Vitorinox Vitorinox closed this Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant