简体   繁体   English

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

[英]How can i check and upload image with an address?

I get a file address with $_GET['image_address'] like this : Logo image . 我得到的文件地址为$_GET['image_address']如下所示: Logo image I want to check format and size of this file and then change name of that and upload that . 我想检查此文件的格式和大小,然后更改其名称并上传。 I searched before inserting this question . 在插入此问题之前,我进行了搜索。 Most of code worked with $_FILE['image'] but i have not this ! 大多数代码与$_FILE['image']一起工作,但我没有! i have only that address , just that . 我只有那个地址,只有那个。 please help me . 请帮我 。 thanks 谢谢

Firstly you need to check file exists and is an image, then grab the image with curl, save it then resize. 首先,您需要检查文件是否存在并且是图像,然后使用curl抓取图像,保存并调整大小。 Hope it helps: 希望能帮助到你:

Example

<?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;
    }
}
?>

// How to "download" the image //如何“下载”图像

$data = file_get_contents($address);

// How to get the size of the image file //如何获取图像文件的大小

$size = filesize($address);

// How to get the ext //如何获取分机号

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

// How to save the image //如何保存图片

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

With getimagesize() like that: 用这样的getimagesize()

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

list($w_src, $h_src, $type) = getimagesize($url);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM