How to download an image from a URL?

Asked

Viewed 11,053 times

1

How do I download an image from a URL using PHP and save it to a folder to use later? I wish to save images from Imdb to use on my own site.

First I tried to download the image using the function copy of PHP but nothing happened

copy($info->Poster, "Posters");

$info->Poster is the direct link to the image.

  • There are several ways. what have you tried? And has the website bid allow this too.

  • I tried using the copy function, but it didn’t work, I believe the download is allowed yes.

  • Add your attempt to the question and better expose what caused you doubts.

  • 3

    This does not prevent your question from being answered, but in principle you could not use these images as they are protected by copyright. This can become a problem.

  • I don’t intend to make money on top of the pictures or on the website, I don’t know if it makes any difference.. '-

  • Of course it makes a difference, it’s like "appropriating" something that’s not yours to use and not ask for. But on top of that, you seem to expect something ready, and it’s not going to happen here, if you don’t expose what you’ve already tried, hardly anyone will make you just copy and paste on your site. So add in question what you have tried and be more specific in doubt.

  • I didn’t ask for something ready, I just want a way to solve it, something like simply showing me the functions I could use would already solve my problem

  • 1

    Well, what I can recommend is that you search by Url and how it works, I think that already fits. Besides, I’ve seen some API’s open sources that take information from imdb, worth checking out : http://www.omdbapi.com/

  • I use an API, it gives me the direct link of the image of each movie or serial I search, with this link I would like to save the image in a directory of the site. But it gets very slow to go from one site to another, I need to store the data on own site :/ but vlw guy, I will search on the Curl

  • 1

    @Diego, as long as it’s not blatantly illegal, it’s not our problem whether the code infringes any copyright or not. There is already problem of the copyright owner and the person/site who is committing the infringement.

  • Ah, I didn’t know how exactly it worked, see there man, I’ll try to be clearer and research more. Hey, but if I have a question, I want to do one thing and not know where to start or what to research?

  • 1

    @Naine, if you search [so], [pt.so] and the PHP Handbook, you’ll already have a lot of code to test and new keywords to search. You can also search only within a stack using Google.

Show 7 more comments

1 answer

1


The first is to make sure that the destination folder on the server has the correct permissions. I tested with this PHP manual code and worked ok, you would have to check the error logs on your server, but it seems that the code below can help in this:

<?php
$imgurl = 'http://ia.media-imdb.com/images/M/MV5BNTA2MTk3NzI5Ml5BMl5BanBnXkFtZTgwNzU2MzYyNzE@._V1_SX300.jpg';
if( !@copy( $imgurl, './teste.jpg' ) ) {
    $errors= error_get_last();
    echo "COPY ERROR: ".$errors['type'];
    echo "<br />\n".$errors['message'];
} else {
    echo "File copied from remote!";
}

I used the Omdb API - The Open Movie Database to catch the URL of the movie poster.

And I tested this other code, the answer Copy image to my server direct from URL and also worked (attention to paths and folder permissions):

function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $useragent);         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 

$imgurl = 'http://ia.media-imdb.com/images/M/MV5BMjE0NDUzMDcyOF5BMl5BanBnXkFtZTcwNzAxMTA2Mw@@._V1_SX300.jpg'; 
$imagename= basename($imgurl);
if(file_exists('./'.$imagename)){continue;} 
$image = getimg($imgurl); 
file_put_contents('./'.$imagename,$image); 

As a note about copyright in this case, usually movie posters are distributed freely by the producer itself, at least it was so in all the feature films that I was involved and whose website makes the poster available for download.

  • PS: the Manuel do PHP is a mother, always has to go there when some function does not do the expected ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.