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

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/)

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...

Programmatically (PHP) save image with no extension

I am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.

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

Get URL image content using php

I have a url like this.
http://www.cardekho.com/
If i wish to get all the image content(jpg,png,gif - only images) from this url to my local machine(C:\images) - How to do this..
Please help me.
Thanks -
Haan
Use either cURL or PHP function file_get_content to get the source code and the source code for the css (if you want to get background-images aswell).
Then use preg_match_all and match all the image tags.
Loop all found image paths and use cURL or file_get_content to save the image to a local destination.

Downloading a photo from twitpic url

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.

Categories