Get Twitter profile picture actual link - php

I'm trying to get the twitter profile picture actual link.
I know I can get the profile picture through the following link:
$test = "http://api.twitter.com/1/users/profile_image?screen_name=".$nickname."&size=original"
but when I want to get the file contents of this url, it doesn't work, cause above mentioned link is redirected to the actual link of the profile picture. So this doesn't work:
file_get_contents($test);
How can I get the actual link of the profile picture and then with the size original?

Try this it might help you.
<?php
function getTwitterProfileImage($username) {
$size = '_bigger';
$api_call = 'http://twitter.com/users/show/'.$username.'.json';
$results = json_decode(file_get_contents($api_call));
return str_replace('_normal', $size, $results->profile_image_url);
}
$img = getTwitterProfileImage('thetutlage');
echo '<img src="'.$img.'"/>';
?>

Try this is up+direct,
<img class="img-rounded" src="<?php
$size = '';
echo str_replace('_normal', $size,$tweet->user->profile_image_url)
?>"
height="250px" width="400px" />

Related

Wordpress: How to get the Header Image ID

Short story:
I'm trying to get the ID of the Header Image in Wordpress.
All I found was this guide, which dosn't seem to work anymore:http://nickohrn.com/2013/09/get-attachment-id-wordpress-header-image/
Long Story
I'm trying to make the WP Header responsive with an srcset. so I don't want to use this code
<img id="masthead-bg" src="<?php header_image() ?>" alt="">
...but instead want to use the wp_get_attachment_image_srcset function to get the srcset of my header image. Only problem: I need an Image ID for this function -> The ID of my Header image.
<img id="masthead-bg"
src="<?php header_image() ?>"
srcset="<?php echo wp_get_attachment_image_srcset( image_id(), 'thumbnail' ); ?>"
sizes="100vw" alt="">
Any suggestions?
Try this...
// Get the header image data
$data = get_object_vars(get_theme_mod('header_image_data'));
// Now check to see if there is an id
$attachment_id = is_array($data) && isset($data['attachment_id']) ? $data['attachment_id'] : false;
if($attachment_id) {
// Put your image code here, user whatever function to get image by id you need
}
Note: if you use a proper WordPress function to get the image it should add in all the srcset etc stuff for you, to allow for responsive images.
To answer the original question, I've found the simplest way to get the ID while I was filtering the markup that gets outputted by <?php the_header_image_tag(); ?> (introduced in v4.4).
function header_img_markup( $html, $header, $attr) {
// we can get the image ID by passing its src url to this method
$header_img_id = attachment_url_to_postid($attr['src']);
// now we can get its metadata from the db
$header_img_data = wp_get_attachment_metadata($header_img_id);
// now we can use the data
$customSizeWidth = $header_img_data['sizes']['my-custom-size']['width'];
// ...your custom output here...
return $html;
}
add_filter('get_header_image_tag', 'header_img_markup', 20, 3);
Responsive Wordpress Header Image with fallback:
if (get_header_image() !== '') {
$attachment_id = attachment_url_to_postid(get_header_image());
echo wp_get_attachment_image($attachment_id, 'large');
}
if (get_header_image() == '') {
echo '<h1>'.get_bloginfo( "name" ).'</h1>';
echo '<h2>'.get_bloginfo( "description" ).'</h2>';
}

Php display image from url into homepage

after no one answered at this question Php Rss feed use img in CDATA -> content:encoded i try to do something else solving this problem...
how can i load an image from a given url directly into my homepage?
<?php
$url = "...";
$image = file_get_contents("$url");
echo $image;
?>
*i don't want to save the image anywhere... just load the image from the url and show it in my own homepage.
Try this code,work fine on my machine.
<?php
$image = 'http://www.google.com/doodle4google/images/d4g_logo_global.jpg';
$imageData = base64_encode(file_get_contents($image));
echo '<img src="data:image/jpeg;base64,'.$imageData.'">';
?>
You are almost there. When you download the contents of the file you need to encode it to base64 if you do not plan to store it on server.
<?php
$url = '...';
$image = base64_encode(file_get_contents($url));
?>
Then you can display it:
<img src="data:image/x-icon;base64,<?= $image ?>">

How to replace a variable URL string (inside <img src="">) depending on pattern? (SimplePie)

This is my first post here. I'm not sure if the title of my post is the most descriptive way of showing my problem, sorry in advance!
Anyway, I've got a Facebook RSS feed on my site using simplepie - I'm displaying posts with a brief summary of its content and an accompanying image, both coming from the feed itself.
I've got a single div that displays this information and is repeated according to the number of stories I've set to display, in my case 3, so the HTML page outputs three div's each with a different story/accompanying image.
<div class="block">
<a href="<?php echo $image_url ?>" target="_blank">
<img src="<?php echo $image ?>" alt="" />
</a>
</div>
The problem is Facebook RSS feeds only show tiny thumbnails generated from the original image (either external or from Facebook's server), but I want them in their original resolution.
I've succesfully replaced the thumbnails coming from Facebook's server by changing the name of the image (from "something_s.jpg" to "something_n.jpg"):
$image = str_replace("_s.jpg", "_n.jpg", $image);
I tried with ltrim to remove the Facebook URL appended before the external image's URL (e.g. https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDZORAaPpbr5COr&w=154&h=154&url=http://example.com/image.jpg). So, I thought this would do it:
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
Oddly enough, external images showed up in their original resolution (e.g. example.com/image.jpg inside <img src="">) but those coming from Facebook (e.g. 10153970_10153145169383306_1966565627_n.jpg) did not anymore. The img src attribute is just empty!
What am I doing wrong here? Any help is appreciated.
(BTW, this is simplepie code in my page):
<?php
require_once('../php/autoloader.php');
$feed = new SimplePie();
$feed->set_feed_url(array('https://www.facebook.com/feeds/page.php?format=rss20&id=40796308305'));
$feed->set_item_limit(3);
$feed->init();
function returnImage ($text) {
$text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');
$pattern = "/<img[^>]+\>/i";
preg_match($pattern, $text, $matches);
$text = $matches[0];
return $text;
}
function scrapeImage($text) {
$pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
preg_match($pattern, $text, $link);
$link = $link[1];
$link = urldecode($link);
return $link;
}
$feed->strip_htmltags(array('embed','center','strong'));
$feed->handle_content_type();
foreach ($feed->get_items() as $item):
$feedDescription = $item->get_content();
$image = returnImage($feedDescription);
$image = scrapeImage($image);
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
$image_url= $item->get_permalink();
$description = $item->get_description();
$description = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $description);
endforeach;
?>
Mr.Rod
use this custom function to change Image src Attribue..
<?php
function ChangeImageSrc($image , $Newsrc){
$regex = '#src="(([^"/]*/?[^".]*\.[^"]*)"([^>]*)>)#';
$Content = preg_replace($regex, $image, 'src="'.$Newsrc.'"');
return $Content;
}
// Useage
$markup = "<img src='fb.com/blahblah.jpg'>";
// Change Src
$toNewSrc = ChangeImageSrc($markup , 'http://example.com/newsrc.png');
print $toNewSrc;
?>
after adding this function to your script
change this line
$image = ltrim(stristr(str_replace("_s.jpg", "_n.jpg", $image), '&url='), '&url=');
to
$image = ChangeImageSrc('src="_n.jpg"', '_s.jpg');
you have to more customize this function to match your needs .

RewriteRule a <img src=...> or other solution

I want my IP cam streaming on my website.
Since I have this piece of code but if you look at the source code, you'll see of course the complete data as user and pass. That is not the intention, I looked at RewriteRule option in .htaccess but don't know how to formulate, or maybe another solution to protect my user and pass data. Who can help me get out the thinking circle and gives a move in the right direction (or example).
<?php
$url = '123.456.7.890:1234';
$user = 'naam';
$pass = 'wachtwoord';
$cam = "http://$url/videostream.cgi?user=$user&pwd=$pass";
?>
<html>
<body>
...
<img name="main" id="main" border="0" width="640" height="480" src="<?php echo("$cam"); ?>">
...
</body>
</html>
If camera response you with image you can get it in PHP code and then add it to image src like base64 data.
For example
<?php
$url = '123.456.7.890:1234';
$user = 'naam';
$pass = 'wachtwoord';
$cam = "http://$url/videostream.cgi?user=$user&pwd=$pass";
$image = file_get_contents($cam); // Or use CURL
// Here you need to replace $type of your camera
$image_src = 'data:image/' . $type . ';base64,' . base64_encode($image);
?>
<html>
<body>
...
<img name="main" id="main" border="0"
width="640" height="480" src="<?php echo $image_src ?>">
...
</body>
</html>
You should use your server side to create a image.
Solution can be found here: Create Image From Url Any File Type
You can create a PHP file that outputs the image or something.
If you do that, you can load the image like this: <img src="http://domain.com/image.php?id=1">
Then in image.php, you can load from your DB, or whatever you like.
However, when you have the real image info, you can make a "fake" image as descriped in the link above.

PHP Image not showing in HTML using img element

Hello there i have a php file with the included:
The image shows properly when i access the PHP file, however when I try to show it in the HTML template, it shows as the little img with a crack in it, so basically saying "image not found"
<img src="http://konvictgaming.com/status.php?channel=blindsniper47">
is what i'm using to display it in the HTML template, however it just doesn't seem to want to show, I've tried searching with next to no results for my specific issue, although I'm certain I've probably searched the wrong title
adding code from the OP below
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
echo "<img src='$online' />";
} else {
echo "<img src='$offline' />";
}
The url is not an image, it is a webpage with the following content
<img src='offline.png' alt='Offline' />
Webpages cannot be displayed as images. You will need to edit the page to only transmit the actual image, with the correct http-headers.
You can probably find some help on this by googling for "php dynamic image".
Specify in the HTTP header that it's a PNG (or whatever) image!
(By default they are interpreted as text/html)
in your status.php file, where you output the markup of <img src=... change it to read as follows
$image = file_get_contents("offline.png");
header("Content-Type: image/png");
echo $image;
Which will send an actual image for the request instead of sending markup. markup is not valid src for an img tag.
UPDATE your code modified below.
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
header("Content-Type: image/png");
$image = null;
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
$image = file_get_contents($online);
} else {
$image = file_get_contents($offline);
}
echo $image;
I suppose you change the picture dynmaclly on this page.
Easiest way with least changes will just be using an iframe:
<iframe src="http://konvictgaming.com/status.php?channel=blindsniper47"> </iframe>

Categories