如何查看并上传带有地址的图片?

时间:2013-06-13 16:42:09

标签: php image url upload

我得到$_GET['image_address']的文件地址,如下所示:Logo image。 我想检查这个文件的格式和大小,然后更改它的名称并上传它。我在插入这个问题之前搜索过。大多数代码都使用$_FILE['image'],但我还没有!我只有那个地址。请帮我 。 感谢

3 个答案:

答案 0 :(得分:1)

首先你需要检查文件是否存在并且是图像,然后用curl抓取图像,保存然后调整大小。希望它有所帮助:

实施例

<?php 
//Test case
$_GET['image_address'] = "https://www.google.com/images/srpr/logo4w.png";

if(isset($_GET['image_address'])){
    //Get the size of the image before downloading
    $image = curl_get_file_size( $_GET['image_address'] );

    //if not empt and not too large download
    if($image > 0 && $image < 50000){
        $image = get_image($_GET['image_address']);
        //check string is an image
        $im = imagecreatefromstring($image);
        if ($im !== false) {
            //get extention
            $ext = pathinfo($_GET['image_address'], PATHINFO_EXTENSION);
            //put contents of image in a new file
            file_put_contents('temp.'.$ext, $image);
            //resize the image W/H
            $image = thumbnail('temp.'.$ext, 120, 150);
            //unlink temp
            unlink('temp.'.$ext);
        }
        else {
            echo 'Not an image';
        }
    }
    //do something with the new file
    echo '<h1>'.$image.'</h1><img src="'.$image.'"/>';
}


function curl_get_file_size( $url ) {
    // Assume failure.
    $result = -1;
    $curl = curl_init( $url );
    // Issue a HEAD request and follow any redirects.
    curl_setopt( $curl, CURLOPT_NOBODY, true );
    curl_setopt( $curl, CURLOPT_HEADER, true );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );

    $data = curl_exec( $curl );
    curl_close( $curl );

    if( $data ) {
        $content_length = "unknown";
        $status = "unknown";

        if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
            $status = (int)$matches[1];
        }

        if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
            $content_length = (int)$matches[1];
        }

        // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
        if( $status == 200 || ($status > 300 && $status <= 308) ) {
            $result = $content_length;
        }
    }
    return $result;
}

function get_image($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

function thumbnail($path, $maxw=120, $maxh=150){

    $type = pathinfo($path, PATHINFO_EXTENSION);
    if($type=='jpg'){$img = imagecreatefromjpeg($path);}
    if($type=='png'){$img = imagecreatefrompng($path);}
    if($type=='gif'){$img = imagecreatefromgif($path);}

    // width & height of original image
    $width = imagesx($img);
    $height = imagesy($img);

    // determine which side is the longest to use in calculating length of the shorter side
    if ($height > $width){
        $ratio = $maxh / $height;
        $newheight = $maxh;
        $newwidth = $width * $ratio;
    }else{
        $ratio = $maxw / $width;
        $newwidth = $maxw;
        $newheight = $height * $ratio;
    }

    // create new image resource
    $newimg = imagecreatetruecolor($newwidth,$newheight);

    // keep transparency
    imagealphablending($newimg, false);
    imagesavealpha($newimg,true);
    imagefilledrectangle($newimg, 0, 0, $newwidth, $newheight, imagecolorallocatealpha($newimg, 255, 255, 255, 127));

    // assign color palette to new image
    for ($i = 0; $i < imagecolorstotal($img); $i++) {
        $colors = imagecolorsforindex($img, $i);
        imagecolorallocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
    }

    // copy original image into new image at new size.
    imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

    // save
    $save_path = 'th_'.$maxw.'_'.basename($path);
    if($type=='jpg'){imagejpeg($newimg, $save_path);}
    if($type=='png'){ imagepng($newimg, $save_path);}
    if($type=='gif'){ imagegif($newimg, $save_path);}
    if(file_exists($save_path)){
        return $save_path;
    }else{
        return false;
    }
}
?>

答案 1 :(得分:0)

//如何“下载”图片

$data = file_get_contents($address);

//如何获取图像文件的大小

$size = filesize($address);

//如何获得分机

$parts = explode('.', $address);
$ext= end($parts); // example: C:\fakepath\128.jpg- $ext = jpg

//如何保存图像

file_put_contents('save_me.'.$ext, $data);

答案 2 :(得分:0)

使用 getimagesize()

$url = 'http://google.com/images/srpr/logo4w.png';

list($w_src, $h_src, $type) = getimagesize($url);
相关问题