【问题标题】:php image upload from url and resize从url上传php图片并调整大小
【发布时间】:2015-02-13 19:05:10
【问题描述】:

我有这个上传表单,我想在上传时调整图片大小 它来自 url 的上传表单我是通过文件上传完成的,但不能用于 url 上传 谁能帮帮我?

<?php
    if($_POST["sub"]){
        $url = trim($_POST["url"]);
        if($url){
            $file = fopen($url,"rb");
            if($file){
                $directory = "../images/news/";
                $valid_exts = array("jpg","jpeg","gif","png");
                $ext = substr(basename($url), strrpos(basename($url), '.'));
                $ext = str_replace('.','',$ext);
                if(in_array($ext,$valid_exts)){
                    $rand = rand(0,9999999999);
                    $filename = $rand.'.'.$ext;
                    $newfile = fopen($directory . $filename, "wb");
                    if($newfile){
                        while(!feof($file)){
                            fwrite($newfile,fread($file,1024 * 8),1024 * 8);
                        }
                        echo ''."";
                        echo ''.$filename.'';
                    } else { echo 'Could not establish new file ('.$directory.$filename.') on local server. Be sure to CHMOD your directory to 777.'; }
                } else { echo 'Invalid file type. Please try another file.'; }
            } else { echo 'Could not locate the file: '.$url.''; }
        } else { echo 'Invalid URL entered. Please try again.'; }
    }
?>

【问题讨论】:

    标签: php


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      从 URL 保存文件:

      如果您将 allow_url_fopen 设置为 true:

      $url = 'http://example.com/image.php';
      $img = '/my/folder/flower.gif';
      file_put_contents($img, file_get_contents($url));
      

      其他使用cURL:

      $ch = curl_init('http://example.com/image.php');
      $fp = fopen('/my/folder/flower.gif', 'wb');
      curl_setopt($ch, CURLOPT_FILE, $fp);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_exec($ch);
      curl_close($ch);
      fclose($fp);
      

      这与this帖子中的答案相同...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-28
        • 2017-03-06
        • 1970-01-01
        • 2012-08-01
        • 2015-09-25
        • 2011-08-25
        • 2014-08-26
        • 2011-05-28
        相关资源
        最近更新 更多