2

我得到一个$_GET['image_address']像这样的文件地址:徽标图像。我想检查这个文件的格式和大小,然后更改它的名称并上传它。我在插入这个问题之前进行了搜索。大多数代码都可以使用,$_FILE['image']但我没有这个!我只有那个地址,就那个。请帮我 。谢谢

4

3 回答 3

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;
    }
}
?>
于 2013-06-13T17:12:17.940 回答
0

使用getimagesize() 像这样:

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

list($w_src, $h_src, $type) = getimagesize($url);
于 2013-06-15T17:59:19.967 回答
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);
于 2013-06-13T16:51:23.033 回答