Autor Zpráva
lopikol
Profil
Potřeboval bych poradit, jak to udělat, Jde mi o to, že uživatel vezme url obrázku a ten se uloží na daný server.

Zkoušel jsem file_get_contents, readfile , ale stále jsem nepřišel na to, že jak ho uložit na svůj server.

Např, když zadám tuto url http://img.csfd.cz/posters/22/228783_1.jpg
Medvídek
Profil
lopikol:
Třeba nějak takto:

<?php
function download_img($from, $dir = '', $max_width = 0, $max_height = 0, $tmp_dir = ''){
    $accept_type = array(
        'image/gif'     => 'gif',
        'image/png'     => 'png',
        'image/jpeg'     => 'jpg',
        'image/pjpeg'     => 'jpg',
        'image/jpg'     => 'jpg'
    );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, rawurldecode($from));
    curl_setopt($curl, CURLOPT_FAILONERROR, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    $img_data = curl_exec($curl);
    curl_close($curl);

    $return = false;
    if ($img_data != ''){
        if (mb_substr($tmp_dir, mb_strlen($tmp_dir) - 1) != '/' && $tmp_dir != '') $tmp_dir .= '/';
        $tmp_file = md5($tmp_dir.$from).'.tmp';
        $f = fopen($tmp_file, 'wb');
        fwrite($f, $img_data);
        fclose($f);
        unset($img_data);

        $img_info = getimagesize($tmp_file);
        if (array_key_exists($img_info['mime'], $accept_type)){
            $ext = $accept_type[$img_info['mime']];
            if (mb_substr($dir, mb_strlen($dir) - 1) != '/' && $dir != '') $dir .= '/';
            $to = $dir.md5($from).'.'.$ext;

            if ($max_width > 0 || $max_height > 0){
                list($width, $height) = $img_info;

                if ($width > $max_width || $height > $max_height){
                    switch ($ext){
                        case 'gif':
                            $imgsrc = imagecreatefromgif($tmp_file);
                            break;
                        case 'png':
                            $imgsrc = imagecreatefrompng($tmp_file);
                            break;
                        case 'jpg':
                            $imgsrc = imagecreatefromjpeg($tmp_file);
                            break;
                     }

                    $imgratio = $width / $height;
                    if ($imgratio > 1){
                        $new_width = $max_width;
                        $new_height = $max_width / $imgratio;
                    } else {
                        $new_height = $max_width;
                        $new_width = $max_width * $imgratio;
                    }

                    $newimg = imagecreatetruecolor($new_width, $new_height);
                    if ($ext == 'gif' || $ext == 'png'){
                        $bgcolor = imagecolorallocate($newimg, 255, 255, 255);
                        imagefill($newimg, 0, 0, $bgcolor);
                        imagecolortransparent($newimg, $bgcolor);
                    }
                    imagecopyresampled($newimg, $imgsrc, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

                    if (file_exists($to)) unlink($to);
                    switch ($ext){
                        case 'gif':
                            $imgsrc = imagegif($newimg, $to);
                            break;
                        case 'png':
                            $imgsrc = imagepng($newimg, $to);
                            break;
                        case 'jpg':
                            $imgsrc = imagejpeg($newimg, $to, 90);
                            break;
                     }

                    $return = array(
                        'file'        => $to,
                        'width'        => $width,
                        'height'    => $height,
                        'type'        => $img_info['mime']
                    );
                }
            } else {
                copy($tmp_file, $to);
            }
        }

        unlink($tmp_file);
    }

    return $return;
}



/*
Použití:
$newimg = download_img('http://www.autorevue.cz/files/564783651.jpg', 'images', 300, 300, 'temp');
$newimg = download_img('http://www.autorevue.cz/files/564783651.jpg', 'images', 300, 300);
$newimg = download_img('http://www.autorevue.cz/files/564783651.jpg', 'images');
$newimg = download_img('http://www.autorevue.cz/files/564783651.jpg', '', 0, 0, 'temp');
$newimg = download_img('http://localhost/et.jpg');
*/

$newimg = download_img('http://www.autorevue.cz/files/564783651.jpg', '', 300, 300);
print_r($newimg);
lopikol
Profil
Díky za tu funkci, ale výsledkem je bílá stránka :(
Stano
Profil
a čo iné očakávaš od funkcie, ktorá vyhľadá obrázok na nete a uloží ho na server?
Medvídek
Profil
lopikol:
Není zač, chyba bude na tvém přijímači :)
(http://filez.party-akce.cz/img/)

Vaše odpověď

Mohlo by se hodit


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0