Get URL image content using php - 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.

Related

Get file contents after pageload

I have an external resource for my images let's say https://api.domain.com/api/downloads/{file_id}. The file gets downloaded after I visit that page. In this case I want to know the mimetype of the file. file_get_contents() doesn't work because the file is downloaded after I visit the page.
This means that I get HTML as output when I dump the result of file_get_contents(). I don't have any hold on how images are served to my application. So I guess I have to find a solution for this problem.
Is there a way to get the mimetype of a file after the page is loaded and it downloaded the file? If something I just wrote is not clear enough please let me know then I try to explain it further. Thanks in advance.
Some more detailed information:
I am currently creating an EML export from data from an external API from Genesys. This is pure PHP and thus I can’t make use of any client-side code like Javascript. The inline images in the body don’t show on in the EML export email body. I think this is because Genesys saves those images somewhere on their side. The image is not directly available from the URL they gave to me, because when I visit that page the page downloads a file but it is not directly served on that page.
To show the images inside the email body I want to encode them to base64 and change the src of the image to the base64 encoded image. To do so I need to know the filetype which I can’t get as described above.
Did you try with the onload property on the <img /> tag ?
<img src="w3html.gif" onload="loadImage()" width="100" height="132">
<script>
function loadImage() {
alert("Image is loaded");
}
</script>
https://www.w3schools.com/tags/ev_onload.asp
You will need to use javascript as the image is on a remote server and loaded on client side

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

How can I save this php created image?

What are some possible ways to save an image or make use of it that is generated from a PHP script. Using save as it does not help though.
This is not an image created by me that's why I want to avoid get_contents.
here is the picture
and here is the url
https://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0
Just write the content of the URL to a file
<?php
file_put_contents("img.png", file_get_contents("http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0"));
Using file_put_contents() function. If you don't have data in variable and want to readout use file_get_contents()
Since you are not generating the image in your own code, the simplest would be a combo of file_get_contents and file_put_contents:
$url = '...'; // your url here
$data = file_get_conents($url);
file_put_conents('image.png', $data);
In this specific case the render is a PNG image, but if there's a possibility of it being a JPEG or something else then you need to somehow detect that as well. I 'm not giving any suggestions for this because there's not enough info to go by.
You can define a filename in imgpng() or the other functions to tell PHP to store the picture instead of sending it to the calling browser.
I understand you want to save it on the client, with a browser, not on the server.
"Save as" worked fine for me (Firefox 7). In Chrome you'll have to specify the extension of the filename manually. Did not test other browsers, but it should work similarly
You can do this from the terminal using the curl command.
curl -o out.png 'http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0'
This will save the file as out.png
use imagepng function.
It will return file to browser or save it specified location.
Need to set parameter for function to save image on specified location.

FPDF Image from Dynamic Source. php/png/pdf. Can't get image to work

http://babymoments.co/preview/highres%20preview/5_357/
According to the FPDF documentation here: http://www.fpdf.org/en/doc/image.htm
You are supposed to be able to use an Image from a dynamic source.. however, as per the first link, i'm getting an fopen error.
Any suggestions?
Code Snippet:
// Overlay Text & Images
$pdf->Image($conf['rbase'].'page_maker/image_hr.php?id=5&side=1&bg=cover_pink&lo=0_1&imgtxt=0|0|u5_1310329746.jpg##1|1|Elina\'s Puppies 9/2/2010|15|arial_bi.ttf|db0ddb|fedfe4&applet_type=cover',$sx,$sh,(0-$dpi), 0, 'png');
You are trying to open a local php file with get parameters - try instead to open the image file as a url. For example :
http://domain.com/image.php?id=5
Or in your case...
http://babymoments.co/preview/page_maker/image_hr.php?id=5&side=1&bg=cover_pink&lo=0_1&imgtxt=0|0|u5_1310329746.jpg##1|1|Elina's%20Puppies%209/2/2010|15|arial_bi.ttf|db0ddb|fedfe4&applet_type=cover
I wouldn't fetch the preview over HTTP but would include the required code and generate the image in the same script that generates the PDF. That way there is no problems with setups that have disabled fopen()-usage with URLs.
I was implementing a barcode for a label printer. So I was using the 'barcodegen' library and 'fpdf' library for this project, but I was having problems including dynamically the image generated with barcodegen, giving me the following error:
FPDF error: Not a PNG file: ./misc/barcodegen/mostrar-codigo-bcgcode39.php
Afeter that, I use one of the answers described here, and I solved the problem using the full URL of the image, like this:
$pdf->Image("http://localhost/caaf/misc/barcodegen/mostrar-codigo-bcgcode39.php",0,0,20,0,'PNG');
And it worked for me.

Categories