Downloading a photo from twitpic url - php

Is there any way of downloading an image from Twitpic URL? Let's say I want to get next photo http://twitpic.com/49275c.

The corresponding link for an image with ID of 49275c is given by
http://twitpic.com/show/full/49275c for a full sized image.
Replace 'full' with 'thumb' or 'mini' for different sizes.
http://twitpic.com/show/[size]/[image-id]
You should really have a look at the API and tinker:
http://dev.twitpic.com/

As #Gordon has pointed out in a comment, twitpic seems to have an API -- which you should use, for this kind of thing.
See : http://dev.twitpic.com/
Now, here's the old answer -- fun, but not really a good idea, considering there is an API :
The URL you have is not the URL of the image itself : it's the URL of an HTML page, in which the image is displayed, in an <img> tag.
So, you need to act in two steps :
First, load that page, and extract the URL of the <img> tag.
This can probably be done using DOMDocument::loadHTMLFile
Then, when you have the image's URL, you can download it
Using file_get_contents,
Or curl.
And here's an example of code that does that :
$twitpic_url = 'http://twitpic.com/49275c';
$dom = new DOMDocument();
if (#$dom->loadHTMLFile($twitpic_url)) {
// HTML loaded successfully
// => You need to find the right <img> tag
// Looking at the HTML, you'll see it has id="photo-display"
$img_tag = $dom->getElementById('photo-display');
$src = $img_tag->getAttribute('src');
// Just to be sure, let's display the image's URL
var_dump($src);
// Now, you have to download the image which URL is $src
$img_content = file_get_contents($src);
// ANd do whatever you want with that binary image content
// like save if to a file :
file_put_contents('/tmp/my-image.jpg', $img_content);
}
Note : you have to add some checks here and there -- like check if the photo-display element exists, for instance.

You can do it directly from the Twitter API, using the entities parameter.

Related

How display img without the file extension

I want to show an img like this:
http://picviewer.umov.me/Pic/GetImage?id=92013681&token=ee103380297bbb2df0d8855949d791df
How should i use php to show the img with dynamic parameters?
You need to set the proper content type headers and then stream the binary data.
$imagepath = '/path/to/file';
header("Content-type: image/jpeg");
echo file_get_contents( $imagepath );
First i'll explain what does that URL do: it's configured to point to a script, or class->function, that manages the searching in the database, for the real path, the path you want to hide for the user, then returns the image to the request.
That said, in two steps, what you have to do is the following.
Check how to customize your url, this could be a start:
How to create friendly URL in php?
create your custom url pointing to a script or class/function like this:
Return a PHP page as an image
Thats, all... Assuming that you already have the images/database covered of course. if not, well you'll have to make the necessary database and tables...

How to treat a PHP image generation script as an image

This is an odd question but I'm stuck on how I would achieve this and I am unable to find any methods of doing so.
I have a simple php script that takes variables (containing file names) from the URL, cleans then and then uses them to generate a single image from the inputted values. This works fine and outputs a new png to the webpage using:
imagepng($img);
I also have a facebook sharing script in PHP that takes a filepath as an input and then shares the image on the users feed where this statement is used to define the image variable:
$photo = './mypic.png'; // Path to the photo on the local filesystem
I don't know how I can link these two together though. I would like to use my generation script as the image to share.
Can anyone point me in the right direction of how to do this? I am not the master of PHP so go easy please.
-Tim
UPDATE
If it helps, here are the links to the two pages on my website containing the outputs. They are very ruff mind you:
The php script generating the image:
http://the8bitman.herobo.com/Download/download.php?face=a.png&color=b.png&hat=c.png
The html page with the img tag:
http://the8bitman.herobo.com/Share.html
Treat it as a simple image:
<img src="http://yourserve/yourscript.php?onlyImage=1" />
yourscript.php
if($_GET['onlyimage']) {
header('Content-type:image/png'); //or your image content type
//print only image
} else {
//print image and text too
}

PHP distinguish between image url and url of page with that image

I have a script that can find images from a URL, given by the user.
So for input string: "http://www.google.com/",
I use something like
$html = file_get_html ( $url );
if ($html->find ( 'img' )) {....}
It brings the result as 'http://www.google.com/images/srpr/logo3w.png'.
But if the user inputs a string "http://www.google.com/images/srpr/logo3w.png". There is no img tag. URL is valid, but code fails. How can this case be included in the code?
The best way to do this would be to perform a HEAD request and check the content type to see if the URL corresponds to an image. If it's an image, then do whatever you want with it, but if it's an HTML page, then use your other code.
You can use curl to get the content type from the server. Then if its an image mime type you can use it directly instead of parsing the html.
Get mime type of external file using cURL and php
Depending on what you want to do with the images, I would use file_get_contents to get the content of the page, and then use imagecreatefromstring to turn it into a gd image resource. This returns false if it can't turn it into an image, so you can then use str_get_html to parse the html (assuming you're using http://simplehtmldom.sourceforge.net/)

Get image source from given URL with javascript or PHP

What I want to do is following:
User takes screenshot with the application like jing. ok!
Pastes link that Jing returned back. ok!
Server processes the link that user entered, and extracts images source url. But, I have no idea how server will get "clean" image source URL. For example, this is the link that Jing returned after sharing screenshot http://screencast.com/t/zxBzNNkcg but real url of image looks like http://content.screencast.com/users/TT13/folders/Jing/media/c25ec5c6-bc6a-413c-a50b-ada95fac4ed2/2012-07-25_0221.png
Server returns back image source URL. no idea!
Is there any possible way to get image url with Javascript or PHP?
you could use Simple PHP DOM Parser to retrieve the image from the url without considering the url for as long as it contains and image inside, like so:
foreach($html->find('div[class=div-that-contain-the image]') as $div) {
foreach($div->find('img') as $img){
echo "<img src='" . $img->src . "'/>";
}
}
That is my solution.
You can retrieve the page containing the image using a DOM library like Query Path.
Using that you can extract the URL to the image.
So in your step 3:
Get source of shared screenshot page (maybe use file_get_contents)
Extract screenshot's image src, using Query Path.
Return image src URL to user
Yes. If you right click on the image and go Copy Image Location, you'll see it's http://content.screencast.com/users/TT13/folders/Jing/media/c25ec5c6-bc6a-413c-a50b-ada95fac4ed2/2012-07-25_0221.png
If you were to do it programmatically, you would use cURL and simplehtmldom.sourceforge.net to parse the outputed HTML for the actual link.
Javascript answer:
Will the output image always have the same class, embeddedObject?
If so, how about something like:
myVar = document.getElementsByClassName('embeddedObject');
myVar[0].getAttribute("src");
The myVar[0] reference of course assumes that there is only ever one image on the page with the embeddedObject class, otherwise you'd need to sort, or know which index to reference each time.
Also, this sadly doesn't seem to be supported in IE8 (which browser do you need to support?):
http://caniuse.com/getelementsbyclassname

Scraping relative path image

I need some help with screen scraping a site (http://website.com).
Lets say I'm trying to get an image inside <div id="imageHolder">
But when I pull it down, it's path is relative ie "image_large/imageName.jpg" (I'm going to pull down this image daily as it changes daily. It always begins with "images_large/.
How can I go in and prepend the url website.com to that image inside <div id="imageHolder">?
$url = 'http://www.website.com/';
// screen scraping here
// say you have your image you scraped stored in the variable below
$img = 'images_large/imageName.jpg';
// your new full path to the image
$fullpath = $url . $img;
// test it (just to see for yourself)
echo $fullpath;
This is just making basic usage of PHP's concatenation operator, ., to append the relative image path onto your scraped URL.
after scrapping the data , u can use str_replace to replace "image_large/" with "http://website.com/image_large/" ... thats the simplest i think
this will replace all similar image paths in the html.. or if u just want one image out of it , i think another user has given u a solution for that. doesnt get any simpler that that i think.

Categories