Pregunta

Suppose i have fetched a images using PHP Simple HTML DOM Parser and obtain it in a specific URL. Suppose it fetched a Image in a variable like

<?php include("simple_html_dom.php");
$webpage ="http://www.example.com";
$html = file_get_html($webpage);
foreach($html->find('img') as $element) {
echo $element->src . '<br>';  }
?>

Now is it possible to get this image in my own domain path, like
$new_img = 'http://mysite.com/some_image_address.JPEG';
I am a PHP beginner so please describe it

¿Fue útil?

Solución

did you try vanilla php, you can try this
Example:

    <?php
function xsrc($url){
return "http://www.my_site.com/myfolder/" . basename($url);
}
//Two liner
$img = xsrc('http://www.example.com/free_images/treacle1.png');
echo "<img src='$img' />";
//One liner
echo '<img src="' . xsrc('http://www.example.com/free_images/treacle.png') . '" />';
//HEREDOC SYNTAX
echo <<<IMG
<img src="$img" />
IMG;
?>
<!-- HTML WITH INLINE PHP -->
<img src="<?php echo xsrc('http://www.example.com/free_images/treacle.png');?>" />
<img src="<?php echo $img;?>" />

Otros consejos

Why do you not right click the image on the site and choose 'Save image as'? No need to use PHP for that.

First learn the basics on browsing, before going into the programming profession!

Most images on other sites are copyrighted, so think twice before you rip a image from another site!

Update:

What you want to do is know as html crawling. First google on that, there a numerous examples. How do I make a simple crawler in PHP?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top