save external site IMG

0

Is it possible to save an image to a /imagens folder of an external site?

I can save normal image if I right click and put "Save image as" , however I'd like to know how to do this in JavaScript or < in> PHP , I've inspected and the element is this:

<img id="imagem" alt="Painel" src="http://appsite/caminho/caminho2/Content/caminho3/Painelimg/2018_03_02/08_03_28.PNG"style="width: 2790.72px; height: 391.68px;">

NOTE: This image is generated every 10 seconds, so I need to save it automatically only if the image name is the current time, for example, this time I inspected the image name is 08_03_28. png or it was 08h: 03m: 28s I do not need to save all I can overlap them too, I only need the image at that moment as I will do another function.     

asked by anonymous 02.03.2018 / 12:15

3 answers

1

There are two ways to do this with PHP .

Example with file_get_contents :

This function will access the page and download all its content, to save, we can use fopen or file_put_contents .

<?php

while (true) {

    /* Captura a data atual */
    $date = date('Y_m_d');

    /* Captura o tempo patual */
    $time = date('H_i_s');

    /* Monta a URL com a data e o tempo */
    $url = "http://appsite/caminho/caminho2/Content/caminho3/Painelimg/{$date}/{$time}.PNG";

    /* Faz uma requisição para a URL e salva o conteúdo em binário na variável */
    $content = file_get_contents($url, FILE_BINARY);

    /* Cria o arquivo no servidor com o conteúdo baixado */
    file_put_contents( "{$time}.png", $content, FILE_BINARY );

    /* Aguarda 10 segundos */
    sleep(10);
}
Ready! It's working. The problem is that while(true) will be infinite, so you may have a problem with the server resources.

If it is sporadic, there are no problems.

Example with curl :

This function will also make a request and return the data, but will have a few more lines. The advantage is that it is more robust than the previous function.

To save, we can use fopen or file_put_contents .

<?php

while (true) {

    /* Captura a data atual */
    $date = date('Y_m_d');

    /* Captura o tempo patual */
    $time = date('H_i_s');

    /* Monta a URL com a data e o tempo */
    $url = "http://appsite/caminho/caminho2/Content/caminho3/Painelimg/{$date}/{$time}.PNG";

    /* Cria o arquivo. Caso ele já exista, sobrepõe */
    $file = fopen("{$time}.png", "w+");

    /* Instancia o objeto */
    $ch = curl_init($url);

    /* Define as configurações */
    curl_setopt_array($ch, [

        /* Informa que deseja capturar o valor retornado */
        CURLOPT_RETURNTRANSFER => true,

        /* Indica o "Resource" do arquivo onde será salvado */
        CURLOPT_FILE           => $file
    ]);

    /* Fecha a conexão da requisição e do arquivo */
    curl_close($ch);
    fclose($file);

    /* Aguarda 10 segundos */
    sleep(10);
}
    
02.03.2018 / 12:33
1

You can also do with NodeJs .

You can use http to download the image and pipe to write the image to your system.

let output = fs.createWriteStream(file);

http.get(URL, response => {
    response.pipe(output);
});

Code:

const URL = "http://appsite/caminho/caminho2/Content/caminho3/Painelimg";
const http = require("http");
const fs = require("fs");

async function startDownload() {

    const date = new Date();

    /* Captura a data */
    let year = date.getFullYear();
    let month = (date.getMonth()+1).toString().padStart(2, "0");
    let day = date.getDay().toString().padStart(2, "0");

    /* Captura o tempo */
    let hour = date.getHours().toString().padStart(2, "0");
    let min = date.getMinutes().toString().padStart(2, "0");
    let seconds = date.getSeconds().toString().padStart(2, "0");

    let datePath = '${year}_${month}_${day}'
    let file = '${hour}_${min}_${seconds}.PNG'

    /* Baixa a imagem */
    http.get('${URL}/${datePath}/${file}', async (response) => {
        await wait(10000);
        startDownload();
    });

};

function wait(ms) {
    return new Promise( resolve => setTimeout(resolve, ms) )
}

startDownload();
    
02.03.2018 / 16:24
0

You can also use the copy() of php function

bool copy ( string $source , string $dest [, resource $context ] )

basically will use the first 2 parameters

  

source: Path to the source file.

     

dest: The destination path. If the dest is a URL, the copy may fail   if the wrapper does not support overwriting existing files.

    
02.03.2018 / 14:38