Sprijin Bază de date filme (The Movie Database)

I want to save the movie posters on webserver. For my website i´m using classic ASP, but i can´t find any solution how to save the image file. Can anyone help?

7 răspunsuri (pe pagina 1 din 1)

Jump to last post

I don't have a asp example but I could send a php example if you would like.

Assuming that you allready have populated the json and u just want to save. Assuming that you have folder rights

here is an function that saves the image url to local file.



Response.Write SaveImage2LocalSystem("http://i.stack.imgur.com/rxByL.gif",".","myfile.gif")
'returns true or false


Public Function SaveImage2LocalSystem(ByVal ImageURL, ByVal DestinationFolder, ByVal ImageName)
    
    'constants
    Const adTypeBinary = 1
    Const adSaveCreateOverWrite = 2
    Const adSaveCreateNotExist = 1
    
    'declare
    Dim http , fso , Stream
    Set http    = CreateObject("Msxml2.ServerXMLHTTP")      'downloads
    Set fso     = CreateObject("Scripting.FileSystemObject")'directory
    Set Stream  = CreateObject("ADODB.Stream")              'saves
    
    'do the request
    http.open "GET", ImageURL,False
    http.send()
    
    'avoid error
    If http.Status >= 400 And http.Status <= 599 Then
        Response.Write "Error Occurred : " & http.Status & " - " & http.statusText
        SaveImage2LocalSystem = False
        Exit Function
    End If
    
    'Load BinaryData to Object
    Dim image : image = http.responseBody
    
    'convert the folder to real path
    DestinationFolder = Server.MapPath(DestinationFolder) & "\" '. or / will do
    
    'check and create
    If Not fso.FolderExists(DestinationFolder) Then fso.CreateFolder(DestinationFolder)
    
    'Save stream To File
    Stream.Type     = adTypeBinary
    Stream.Open
    Stream.Write image
    Stream.SaveToFile DestinationFolder & ImageName, adSaveCreateOverWrite
    Response.Write DestinationFolder & ImageName
    
    'or view
    'Response.BinaryWrite http.ResponseBody
    
    'close objects
    Set fso     = Nothing
    Set http    = Nothing
    Set Stream  = Nothing
    
    'return true or whatever
    SaveImage2LocalSystem = True
End Function

@Dale, I would like to see the PHP example :)

Here is my example:

You need to ensure each of the folder structure are created for each images.


$myimg1 = array('bIg5Hv4FZn9210BIO2wJ36MOjMF.jpg', 'aQpXLhmXQW69OaXsX6uv9F3zsuR.jpg');


 $original = "images/themoviedb/p/original";
 $w45 = "images/themoviedb/p/w45";
 $w92 = "images/themoviedb/p/w92";
 $w130 = "images/themoviedb/p/w130/"; 
 $w154 = "images/themoviedb/p/w154/";
 $w185 = "images/themoviedb/p/w185/";
 $w780 = "images/themoviedb/p/w780/";


foreach($myimg1 as $key => $value){

    $i = $value;
    if(!empty($i)){

        $fullpath = $original."/".basename($i);
        if(!file_exists($fullpath)){
            $originalImage = "http://image.tmdb.org/t/p/original/".$i;
            image_save_from_url($originalImage,$original);
            echo "Image original saved ". $fullpath ;
        }
        else { echo "Image original exist ". $fullpath; }

        $fullpath = $w45."/".basename($i);
        if(!file_exists($fullpath)){
            $w45Image = "http://image.tmdb.org/t/p/w45/".$i;
            image_save_from_url($w45Image,$w45);
            echo "Image w45 saved ". $fullpath;
        }
        else { echo "Image w45 exist ". $fullpath ; }

        $fullpath = $w92."/".basename($i);
        if(!file_exists($fullpath)){
            $w92Image = "http://image.tmdb.org/t/p/w92/".$i;
            image_save_from_url($w92Image,$w92);
            echo "Image w92 saved ". $fullpath ";
        }
        else { echo "Image w92 exist ". $fullpath; }

        $fullpath = $w130."/".basename($i);
        if(!file_exists($fullpath)){
            $w130Image = "http://image.tmdb.org/t/p/w130/".$i;
            image_save_from_url($w130Image,$w130);
            echo "Image w130 saved ". $fullpath;
        }
        else { echo "Image w130 exist ". $fullpath; }


        $fullpath = $w154."/".basename($i);
        if(!file_exists($fullpath)){
            $w154Image = "http://image.tmdb.org/t/p/w154/".$i;
            image_save_from_url($w154Image,$w154);
            echo "Image w154 saved ". $fullpath;
        }
        else { echo "Image w154 exist ". $fullpath ; }

        $fullpath = $w185."/".basename($i);
        if(!file_exists($fullpath)){
            $w185Image = "http://image.tmdb.org/t/p/w185/".$i;
            image_save_from_url($w185Image,$w185);
            echo "image w185 saved ". $fullpath ;
        }
        else { echo "Image w185 exist ". $fullpath ; }
    }   
}

function image_save_from_url($my_img,$fullpath){
    if($fullpath!="" && $fullpath){
        $fullpath = $fullpath."/".basename($my_img);
    }
    $ch = curl_init ($my_img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($fullpath)){
        unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);
    fclose($fp);
}

If you need any help let me know. I am using someone Image_save_from_url() function. Sorry that I couldn't give credit to the person I got it from.

Dale.

Thanks Dale, I'm having problems getting the images using AJAX and json

No problem. Sorry there was some format issues with my post. I have fixed it. Hope that helps.

Thanks Dale

Nu găsiți un film sau un serial? Autentificați-vă pentru a-l crea.

Globale

s focalizează bara de căutare
p deschide meniul profilului
esc închide o fereastră deschisă
? deschide fereastra cu scurtături de la tastatură

Pe paginile media

b mergi înapoi (sau la părinte atunci când este cazul)
e mergi la pagina de editare

Pe paginile sezoanelor filmelor seriale

(săgeată dreapta) mergi la sezonul următor
(săgeată stânga) mergi la sezonul precedent

Pe paginile episoadelor filmelor seriale

(săgeată dreapta) mergi la episodul următor
(săgeată stânga) mergi la episodul precedent

Pe toate paginile de imagini

a deschide fereastra pentru adăugarea de imagini

Pe toate paginile de editare

t deschide selectorul de traduceri
ctrl+ s trimite formularul

Pe paginile de discuții

n crează o discuție nouă
w comută starea de vizionare
p comută publică/privată
c comută închisă/deschisă
a deschide activitatea
r răspunde la discuție
l mergi la ultimul răspuns
ctrl+ enter trimite mesajul
(săgeată dreapta) pagina următoare
(săgeată stânga) pagina precedentă

Setări

Doriți să evaluați sau să adăugați acest articol într-o listă?

Autentificare