简体   繁体   中英

Oauth2 returned image url breaks imagecreatefrompng

I am using hybridauth-3.7.1 from oauth library. https://github.com/hybridauth/hybridauth/releases

The library works perfectly so I have the following:

<?php
  require_once 'hybridauth-3.7.1/vendor/autoload.php';
  $config = [
    'callback' => 'my_callback_url',
    'keys'     => [
      'id' => 'my_app_id',
      'secret' => 'my_secret'
    ],
  ];
  $adapter = new Hybridauth\Provider\Reddit( $config );
  $adapter->authenticate();
  $userProfile = $adapter->getUserProfile();
  $photo = $userProfile->photoURL;

If I echo the $photo I get: https://styles.redditmedia.com/t5_55fo22/styles/profileIcon_bblqmk8klas71.png?width=256&height=256&crop=256:256,smart&s=30a3bb6af945eadeaddb40271d2c0161ca82768d

now if I try to:

imagecreatefrompng($photo);

my logs return:

imagecreatefrompng( https://styles.redditmedia.com/t5_55fo22/styles/profileIcon_bblqmk8klas71.png?width=256&amp;amp;height=256&amp;amp;crop=256:256,smart&amp;amp;s=30a3bb6af945eadeaddb40271d2c0161ca82768d ): failed`

So the url is properly returned in the echo, but as can be seen from the logs imagecreatefrompng is adding a weird &amp;amp;

i have also tried

imagecreatefrompng(urldecode($photo));

and

$decoded_photo = urldecode($photo);
imagecreatefrompng($decoded_photo);

both return the same above error logs.

However if I manually type the url in it works fine.

imagecreatefrompng( 'https://styles.redditmedia.com/t5_55fo22/styles/profileIcon_bblqmk8klas71.png?width=256&height=256&crop=256:256,smart&s=30a3bb6af945eadeaddb40271d2c0161ca82768d' );

How can I get the returned image url to work with imagecreatefrompng?

The problem is when directly pasting the url as shown in the last example I was not realizing the cut and paste was different than the actual source.

This means although i was copying &height=256... what was in the actual source was &amp;=256... Therefore when trying to imagecreatefrompng() it was encoding already encoded &amp; thus resulting in &amp;amp;

The fix was to change the $userProfile->photoUrl as displayed below.

$photo = htmlspecialchars_decode($userProfile->photoURL);
imagecreatefrompng($photo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM