php 涓婁紶鍥剧墖骞剁敓鎴愮缉鐣ュ浘

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 涓婁紶鍥剧墖骞剁敓鎴愮缉鐣ュ浘相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/mime%e7%b1%bb%e5%9e%8b' title='mime绫诲瀷'>mime绫诲瀷   while   end   copy   rect   upload   open   str   array   

<?php
    // clearDir(鈥榰pload/images/鈥?;

    if ($_FILES[鈥榝ile鈥榏[鈥榚rror鈥榏 == 0) {
        $MAX_FILE_SIZE = 300000;
        if ($_FILES[鈥榝ile鈥榏[鈥榮ize鈥榏 > $MAX_FILE_SIZE) {
            exit(鈥樻枃浠惰秴鍑烘寚瀹氬ぇ灏忊€?span style="color: #000000;">);
        }
        
        $allowSuffix = [鈥榡pg鈥? 鈥榩ng鈥? 鈥榡peg鈥?span style="color: #000000;">];
        
        $myImg = explode(鈥?鈥? $_FILES[鈥榝ile鈥榏[鈥榥ame鈥?span style="color: #000000;">]);
        
        $mySuffix = array_pop($myImg);
        
        if (!in_array($mySuffix, $allowSuffix)) {
            exit(鈥樻枃浠剁被鍨嬩笉瀵光€?span style="color: #000000;">);
        }
        
        $allowMine = [
            鈥榠mage/jpg鈥?
            鈥榠mage/jpeg鈥?
            鈥榠mage/pjpeg鈥?
            鈥榠mage/gif鈥?span style="color: #000000;">
        ];
        
        if (!in_array($_FILES[鈥榝ile鈥榏[鈥榯ype鈥榏, $allowMine)) {
            exit(鈥樻枃浠秏ime绫诲瀷涓嶅鈥?span style="color: #000000;">);
        }
        
        $path = "upload/images/";
        $name = date(鈥榊mdhis鈥?.mt_rand(0, 9). 鈥?鈥?.$mySuffix;
        
        if(is_uploaded_file($_FILES[鈥榝ile鈥榏[鈥榯mp_name鈥?span style="color: #000000;">])) {
            if (move_uploaded_file($_FILES[鈥榝ile鈥榏[鈥榯mp_name鈥榏, $path.$name)) {
                $thumb_url = create_thumb($path.$name);
                $thumb_url2 = create_thumb($path.$name, 200, true);
                echo 鈥樹笂浼犳垚鍔? 缂╃暐鍥捐矾寰勪负: 鈥?$thumb_url;
                echo 鈥樻柟缂╃暐鍥捐矾寰勪负: 鈥?$thumb_url2;
            } else {
                echo 鈥樹笂浼犲け璐モ€?span style="color: #000000;">;
            }
        } else {
            exit(鈥樹笉鏄笂浼犳枃浠垛€?span style="color: #000000;">);
        }
    } else {
        exit(鈥樹笂浼犲け璐モ€?span style="color: #000000;">);
    }
    
    /**
     * 缁欏浘鐗囩敓鎴愮缉鐣ュ浘
     */
    function create_thumb($url, $max_width = 200, $is_square = false) {
        $image = imagecreatefromjpeg($url);
        
        list($ow, $oh) = getimagesize($url);
        $path_arr = explode(鈥?鈥? $url);
        
        //濡傛灉瑕佹眰鐢熸垚姝f柟褰㈢殑缂╃暐鍥?/span>
        if ($is_square) {
            if ($ow > $max_width) {
                $nw = $max_width;
                $new_image = imagecreatetruecolor($nw, $nw);
                $white = imagecolorallocate($new_image, 255, 255, 255);
                imagefilledrectangle($new_image, 0, 0, $nw, $nw, $white);
                
                $o_wh = $ow > $oh ? $oh : $ow;
                imagecopyresampled($new_image, $image, 0, 0, 0, 0, $nw, $nw, $o_wh, $o_wh);
                imagejpeg($new_image, $path_arr[0].鈥榑thumbf.jpg鈥?span style="color: #000000;">);
                
                $result = $path_arr[0].鈥榑thumbf.jpg鈥?span style="color: #000000;">;
            } else {
                $new_image = imagecreatetruecolor($ow, $ow);
                $white = imagecolorallocate($new_image, 255, 255, 255);
                imagefilledrectangle($new_image, 0, 0, $ow, $ow, $white);
                
                if ($ow > $oh) {
                    imagecopyresampled($new_image, $image, 0, ($ow -  $oh)/ 2, 0, 0, $ow, $oh, $ow, $oh);
                } else {
                    imagecopyresampled($new_image, $image, 0, 0, 0, 0, $ow, $ow, $ow, $ow);
                }
                
                imagejpeg($new_image, $path_arr[0].鈥榑thumbf.jpg鈥?span style="color: #000000;">);
                $result = $path_arr[0].鈥榑thumbf.jpg鈥?span style="color: #000000;">;
            }
        } else {
            if ($ow > $max_width) {
                $percent = $max_width / $ow;
                $nw = $max_width;
                $nh = $oh * $percent;
                $new_image = imagecreatetruecolor($nw, $nh);
                imagecopyresampled($new_image, $image, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
                
                imagejpeg($new_image, $path_arr[0].鈥榑thumb.jpg鈥?span style="color: #000000;">);
                $result = $path_arr[0].鈥榑thumb.jpg鈥?span style="color: #000000;">;
            } else {
                $new_image = imagecreatetruecolor($ow, $oh);
                $white = imagecolorallocate($new_image, 255, 255, 255);
                imagefilledrectangle($new_image, 0, 0, $ow, $oh, $white);
                imagecopyresampled($new_image, $image, 0, 0, 0, 0, $ow, $oh, $ow, $oh);
                imagejpeg($new_image, $path_arr[0].鈥榑thumb.jpg鈥?span style="color: #000000;">);
                $result = $path_arr[0].鈥榑thumb.jpg鈥?span style="color: #000000;">;
            }
        }
        
        imagedestroy($image);
        return $result;
    }
    
    //娓呯┖鏂囦欢澶?/span>
    function clearDir($dir) {
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while ($file=readdir($dh)) {
                    if (is_file($dir.$file)) {
                        unlink($dir.$file);
                    }
                }
                
                closedir($dh);
            }
        }
    }
?>

 

以上是关于php 涓婁紶鍥剧墖骞剁敓鎴愮缉鐣ュ浘的主要内容,如果未能解决你的问题,请参考以下文章

瀹夊崜寮€鍙戦」鐩疄鎴樻垜鐨勪簯闊充箰

閰嶇疆php.ini 淇敼php鏂囦欢涓婁紶澶у皬

[PHP] layui瀹炵幇澶氬浘涓婁紶锛屽浘鐗囪嚜鐢辨帓搴忥紝鑷敱鍒犻櫎

閫夋嫨鏂囦欢杩涜涓婁紶

python鎺ュ彛鑷姩鍖?-post鏂囦欢涓婁紶

鍒涘缓鏂囦欢涓婁紶琛ㄥ崟