I created this script to get the sources of the images from a single webpage, now, i wanted to download all those images to my server using curl.
<?php
// Create DOM from URL or file
require_once 'library/simple_html_dom.php';
$html = file_get_html('http://adamkhoury.com/');
// Find all images
foreach($html->find('img') as $element) //every image found is declared as $element
echo $element->src . '<br>';
?>
It's as simple as this
file_put_contents("filename.extension", file_get_contents($element->src));
There is file_get_contents() function in PHP for getting external contents and file_put_contents() for storing content in a file...
use file_get_contents() to download images and use file_put_contents() to store that:
$image = file_get_contents($element->src);
file_get_contents("image.jpg",$image);
of course, there is another method names cURL in php for downloading external data...
Related
I use this code to generate a QR Code and display it:
<?php
$aux = 'qr_img0.50j/php/qr_img.php?';
$aux .= 'd=Text&';
$aux .= 'e=H&';
$aux .= 's=4&';
$aux .= 't=P';
?>
<img width="250" src="<?php echo $aux; ?>" />
It generates and displays it without problems,
but I don't want to display it, but load it into "dompdf" (PHP PDF Generator).
I found out, that I can't give dompdf the "$aux" variable ("< img src='$aux' />"). The variable returns the correct string, but dompdf can't display it (Probably due to being a PHP file).
I came up with file_get_contents, but surprisingly, it returned a blank file.
I used:
file_put_contents('tempqr.png', file_get_contents($qrc));
It is not due to wrong permissions, because...
when I typed the entire URL path, it 'copied' the file successfully (http://localhost:2180/work/qr_img0.50j/php/qr_img.php?...), but I think that's not a reliable solution, because of the port and stuff that can change over time. I installed cURL, and the same issue persists: It only displays with the full URL path. I tried fopen to 'read' the image into a buffer, and the buffer remained blank.
Maybe anyone can help me (and other readers), to get those two functions to load the file (maybe without the whole http unreliable thing?).
Or maybe there's another way to generate images from "qr_img0.50j" without calling php that I didn't know...
Dompdf (as of 0.6.1) will no longer parse PHP in your document. You will need to do that prior to passing the document to Dompdf. Probably the easiest method to do this is to render the image and insert it into the document as a data-uri.
You may have to modify your QR generator to work in this flow if it's not designed for command-line execution. Ideally it would just be a callable function, which is what I presumed for the sample.
With Dompdf 0.7.x:
<?php
// require dompdf autoloader, then ...
using Dompdf\Dompdf;
$image = qr_img('Text', 'H', '4', 'P'); // assumine PNG output
$html = '<img width="250" src="data:image/png;base64,' . base64_encode($image) . '" />';
$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream();
?>
FYI, the same issue applied to file_get_contents. It does not parse the PHP of a file. It can only be used to get the file itself.
Regardless of the particular method you use to get a file the results depend on the source. Files retrieved from the local file system will not have their PHP parsed. Files retrieved via a web server will.
I am trying to get all the image src's and rename the files using this code but it doesnt seem to work.
Any ideas ??
require_once('catalog/controller/forum/simple_html_dom.php');
$test = $data['description'];
$html = str_get_html($test);
foreach($html->find('img') as $element) {
$src = $element->src;
rename($src,$src.".jms");
}
All images files are local and this is an example image tag.
<img alt="Image" src="image/data/attaches/f7ff31f73f6d41f108ef31c01ea69228.png">
So i'm trying to rename the file from that to the same location but by adding .jms to the end.
Also i'm not sure how but i want to modify all the image tags in the string so i can put it back modified with the mysql update command.
The string $data['description'] contains other html not just image tags. Its varied.
rename() could only used to the local files in your server, no way to rename the remote files.
If the images are in your server, and you could know the real path from the src, then you need to convert the src to the real path of the image.
I load an XML file from a service provider, and then my HTML displays the images in the necessary place. However, I wish to cache all of these files locally, instead of having the browser load them from the remote server each time.
Here is a sample of my XML file...
feed.xml
<URI>http://imt.boatwizard.com/images/1/14/77/3801477_-1_20120229071449_0_0.jpg</URI>
<URI>http://imt.boatwizard.com/images/1/40/6/3794006_-1_20120814035230_16_0.jpg</URI>
<URI>http://imt.boatwizard.com/images/1/21/74/4012174_-1_20120706051335_21_0.jpg</URI>
Can someone please help me write the PHP to loop through the XML, and download each image.
1) Download image
2) Rename image URL in XML, to match local file.
3) Save XML
Thanks!
I guess you should do something like this
// xmlize your... ehm... xml
$xml = new SimpleXMLElement($xml_content);
// extract uri elements
$result = $xml->xpath('/URI');
// loop through uris
while(list( , $node) = each($result)) {
// with curl functions, download every image
curl_stuff_i_dont_remember($node);
// move it to your folder
rename($downloaded_img, $newpath_img);
// if everything went ok, add a new line into the output xml
$outxml = $outxml . '<URI>' . basename($newpath_img) . '</URI>';
}
// dump the outxml
$fp = fopen('newxml.xml', 'w+');
fwrite($fp, $outxml);
I have this code that has an image tag. The img src is equal to "https://graph.facebook.com/<?php echo $user_id; ?>/picture". Is there anyway that I could get that image file and upload it to my server using the move_uploaded_image function with php?
No, move_uploaded_image moves files that were uploaded in a POST request.
If you want to get an image from a URL you need to make an HTTP request for it, e.g. via cURL.
Using PHP's imagecreate and file_get_contents and file_put_contents, you can create a blank image, get the image from the remote URL, then replace the blank image you created.
Similar to,
// Create a blank image
$imPath = "path/to/your/desired/image.jpg";
$im = imagecreatetruecolor(180, 151);
imagejpeg($im,$imPath);
// Download their fb image
$url = 'https://graph.facebook.com/' . $user['username'] . '/picture?type=large';
$bytes = file_put_contents($imPath, file_get_contents($url));
there is no function like move_uploaded_image . correct function is move_uploaded_file.
no. you can not get image file by this function .
first argument of this function is temp file name which is uploaded through Input type = file.
and another argument is the location where you want to put file....
you should refer $_FILES of php manual for more information....
http://php.net/manual/en/function.move-uploaded-file.php
I just need text data. I can't find the answer to it anywhere.
Well, cURL will not download the images, but only the HTML text. You might have image tags in there, but that doesn't make PHP download the appropriate images.
I am not familiar with cURL but you can use get filecontents
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>