中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

PHP文件夾遍歷,圖片等比例壓縮

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    <?php  
    /** 
    * 來源:互聯(lián)網(wǎng) 
    */  
    class image  
    {  
        var $w_pct = 100; //透明度  
        var $w_quality = 100; //質(zhì)量  
        var $w_minwidth = 500; //最小寬  
        var $w_minheight = 500; //最小高  
        var $interlace = 0;  //圖像是否為隔行掃描的  
        var $fontfile ;  //字體文件  
        var $w_img ; //默認水印圖  
      
        function __construct()  
        {  
            $this->fontfile = $_SERVER['DOCUMENT_ROOT'].'/public/fonts/simhei.ttf';  
            $this->w_img = '';  
        }  
        function image()  
        {  
            $this->__construct();  
        }  
      
        function info($img)  
        {  
            $imageinfo = getimagesize($img); //返回圖像信息數(shù)組 0=>寬的像素 1=>高的像素 2=>是圖像類型的標記 3 =>是文本字符串,內(nèi)容為“height="yyy" width="xxx"”,  
            if($imageinfo === false) return false;  
            $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1)); //獲取圖像文件類型 $imageinfo[2]是圖像類型的標記  
            $imagesize = filesize($img); //圖像大小  
            $info = array(  
            'width'=>$imageinfo[0],  
            'height'=>$imageinfo[1],  
            'type'=>$imagetype,  
            'size'=>$imagesize,  
            'mime'=>$imageinfo['mime']  
            );  
            return $info;  
        }  
      
        /** 
         * 縮略圖 
         * 
         * @param string $image 
         * @param string $filename 
         * @param int $maxwidth 
         * @param int $maxheight 
         * @param string $suffix 
         * @param int $autocut 
         * @return string 
         */  
        function thumb($image, $filename = '',$maxwidth = 50, $maxheight = 50, $suffix='_thumb', $autocut = 0)  
        {  
            if( !$this->check($image)) return false;  
            $info  = $this->info($image); //獲取圖片信息  
            if($info === false) return false;  
            $srcwidth  = $info['width']; //源圖寬  
            $srcheight = $info['height']; //源圖高  
            $pathinfo = pathinfo($image);  
            $type =  $pathinfo['extension']; //取得擴展名  
            if(!$type) $type = $info['type']; //如果沒有取到,用$info['type']  
            $type = strtolower($type);  
            unset($info);  
            $scale = min($maxwidth/$srcwidth, $maxheight/$srcheight); //獲取縮略比例  
            //獲取按照源圖的比列  
            $createwidth = $width  = (int) ($srcwidth*$scale);//取得縮略寬();  
            $createheight = $height = (int)($srcheight*$scale);//(); //取得縮略高  
            $psrc_x = $psrc_y = 0;  
            if($autocut) //按照縮略圖的比例來獲取  
            {  
                if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) //如果縮略圖按比列比源圖窄的話  
                {  
                    $width = $maxheight/$height*$width; //寬按照相應(yīng)比例做處理  
                    $height = $maxheight;// //高不變  
                }  
                elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width)//如果縮略圖按比列比源圖寬的話  
                {  
                    $height = $maxwidth/$width*$height;//  
                    $width = $maxwidth;  
                }  
                if($maxwidth == '55'){  
                    $width = '55';  
                    $createwidth = '55';  
                }  
                if($maxwidth == '110'){  
                    $width = '110';  
                    $createwidth = '110';  
                }  
      
            }  
                if($srcwidth<='280' && $maxwidth != '110'){  
                    $createwidth= $srcwidth;  
                    $createheight = $srcheight;  
                }  
            $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的圖像處理函數(shù)  
            $srcimg = $createfun($image); //返回圖像標識符  
            if($type != 'gif' && function_exists('imagecreatetruecolor')){  
                $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像  
            }else{  
                if($maxwidth == '110' && $srcheight >= $srcwidth){  
                    $height1 = 72;  
                    $thumbimg = imagecreate($width, $height1); //新建一個基于調(diào)色板的圖像  
                }elseif ($maxwidth == '110' && $srcheight <= $srcwidth){  
                    $width1 = 110;  
                    $thumbimg = imagecreate($width1, $height); //新建一個基于調(diào)色板的圖像  
                }else{  
                    $thumbimg = imagecreate($width, $height); //新建一個基于調(diào)色板的圖像  
                }  
            }  
            if(function_exists('imagecopyresampled')){ //重采樣拷貝部分圖像并調(diào)整大小,真彩  
                //imagecopyresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)  
                    if($maxwidth == '110' && $srcheight >= $srcwidth){  
                        $psrc_x = 0;  
                        $psrc_yz = $height/2;  
                        $psrc_y = $psrc_yz;  
                        $width = '110';  
                    }  
                    if($maxwidth == '110' && $srcheight >= $srcwidth && $srcheight>= '72' && $srcwidth <= '110'){  
                        $psrc_x = 0;  
                        $psrc_yz = $height/2;  
                        $psrc_y = $psrc_yz;  
                        $width = '110';  
                    }  
                    if($maxheight == '72' && $srcheight <= $srcwidth && $srcheight<= '72' && $srcwidth >= '110'){  
                        $cha = ($maxheight-$srcheight)/2;  
                        $psrc_y = -($cha);  
                        $width = $srcwidth;  
                        $height = '72';  
                        $srcheight = '72';  
                    }  
                imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);  
      
            }else{ //拷貝部分圖像并調(diào)整大小,調(diào)色板  
                imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height,  $srcwidth, $srcheight);  
            }  
            if($type=='gif' || $type=='png')  
            {  
                //imagecolorallocate 為一幅圖像分配顏色  
                $background_color  =  imagecolorallocate($thumbimg,  0, 255, 0);  // 給基于調(diào)色板的圖像填充背景色, 指派一個綠色  
                // imagecolortransparent 將某個顏色定義為透明色  
                imagecolortransparent($thumbimg, $background_color);  //  設(shè)置為透明色,若注釋掉該行則輸出綠色的圖  
            }  
            // imageinterlace 激活或禁止隔行掃描  
            if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);  
            $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);  
            //imagejpeg imagegif imagepng  
            if(empty($filename)) $filename  = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //獲取文件名  
            //aaa.gif aaa_thumb.gif  
            $imagefun($thumbimg, $filename); //新建圖像  
            imagedestroy($thumbimg); //銷毀縮略圖  
            imagedestroy($srcimg); //銷毀源圖  
            return $filename;  
        }  
        /** 
         * 限制寬或高 
         * 
         * @param string $image 
         * @param int $maxwidth 
         * @param int $maxheight 
         * @param string $suffix 
         * @return string 
         */  
        function thumb3($image,$maxwidth = 0, $maxheight = 0, $suffix='_thumb')  
        {  
            if( !$this->check($image)) return false;  
            $info  = $this->info($image); //獲取圖片信息  
            if($info === false) return false;  
            $srcwidth  = $info['width']; //源圖寬  
            $srcheight = $info['height']; //源圖高  
            $pathinfo = pathinfo($image);  
            $type =  $pathinfo['extension']; //取得擴展名  
            if(!$type) $type = $info['type']; //如果沒有取到,用$info['type']  
            $type = strtolower($type);  
            unset($info);  
            if($maxwidth==0){  
                $scale = $maxheight/$srcheight;  
                if($scale<1){  
                    $createwidth = $srcwidth*$scale;  
                    $createheight = $maxheight;  
                }else{  
                    $createwidth = $srcwidth;  
                    $createheight = $srcheight;  
                }  
            }  
            if($maxheight==0){  
                $scale = $maxwidth/$srcwidth;  
                if($scale<1){  
                    $createwidth = $maxwidth;  
                    $createheight = $srcheight*$scale;  
                }else{  
                    $createwidth = $srcwidth;  
                    $createheight = $srcheight;  
                }  
            }  
            $psrc_x = $psrc_y = 0;  
            $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type); //找到不同的圖像處理函數(shù)  
            $srcimg = $createfun($image); //返回圖像標識符  
            if($type != 'gif' && function_exists('imagecreatetruecolor')){  
                $thumbimg = imagecreatetruecolor($createwidth, $createheight); //新建一個真彩色圖像  
            }else{  
                $thumbimg = imagecreate($createwidth, $createheight); //新建一個基于調(diào)色板的圖像  
            }  
            if(function_exists('imagecopyresampled')){ //重采樣拷貝部分圖像并調(diào)整大小,真彩  
                //imagecopyresampled(新圖,源圖,新圖左上角x距離,新圖左上角y距離,源圖左上角x距離,源圖左上角y距離,新圖寬,新圖高,源圖寬,源圖高)  
                imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight, $srcwidth, $srcheight);  
          
            }else{ //拷貝部分圖像并調(diào)整大小,調(diào)色板  
                imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $createwidth, $createheight,  $srcwidth, $srcheight);  
            }  
            if($type=='gif' || $type=='png')  
            {  
                //imagecolorallocate 為一幅圖像分配顏色  
                $background_color  =  imagecolorallocate($thumbimg,  0, 255, 0);  // 給基于調(diào)色板的圖像填充背景色, 指派一個綠色  
                // imagecolortransparent 將某個顏色定義為透明色  
                imagecolortransparent($thumbimg, $background_color);  //  設(shè)置為透明色,若注釋掉該行則輸出綠色的圖  
            }  
            // imageinterlace 激活或禁止隔行掃描  
            if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);  
            $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);  
            //imagejpeg imagegif imagepng  
            $filename  = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type; //獲取文件名  
            //aaa.gif aaa_thumb.gif  
            $imagefun($thumbimg, $filename); //新建圖像  
            imagedestroy($thumbimg); //銷毀縮略圖  
            imagedestroy($srcimg); //銷毀源圖  
            return $filename;  
        }  
        /** 
         * 生成特定尺寸縮略圖 解決原版縮略圖不能滿足特定尺寸的問題 PS:會裁掉圖片不符合縮略圖比例的部分 
         * @static 
         * @access public 
         * @param string $image  原圖 
         * @param string $type 圖像格式 
         * @param string $thumbname 縮略圖文件名 
         * @param string $maxWidth  寬度 
         * @param string $maxHeight  高度 
         * @param boolean $interlace 啟用隔行掃描 
         * @return void 
         */  
        static function thumb2($image, $thumbname, $type='', $maxWidth=200, $maxHeight=50, $interlace=true) {  
            // 獲取原圖信息  
            $info = Image::getImageInfo($image);  
            if ($info !== false) {  
                $srcWidth = $info['width'];  
                $srcHeight = $info['height'];  
                $type = empty($type) ? $info['type'] : $type;  
                $type = strtolower($type);  
                $interlace = $interlace ? 1 : 0;  
                unset($info);  
                $scale = max($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 計算縮放比例  
                //判斷原圖和縮略圖比例 如原圖寬于縮略圖則裁掉兩邊 反之..  
                if($maxWidth / $srcWidth > $maxHeight / $srcHeight){  
                    //高于  
                    $srcX = 0;  
                    $srcY = ($srcHeight - $maxHeight / $scale) / 2 ;  
                    $cutWidth = $srcWidth;  
                    $cutHeight = $maxHeight / $scale;  
                }else{  
                    //寬于  
                    $srcX = ($srcWidth - $maxWidth / $scale) / 2;  
                    $srcY = 0;  
                    $cutWidth = $maxWidth / $scale;  
                    $cutHeight = $srcHeight;  
                }  
          
                // 載入原圖  
                $createFun = 'ImageCreateFrom' . ($type == 'jpg' ? 'jpeg' : $type);  
                $srcImg = $createFun($image);  
          
                //創(chuàng)建縮略圖  
                if ($type != 'gif' && function_exists('imagecreatetruecolor'))  
                    $thumbImg = imagecreatetruecolor($maxWidth, $maxHeight);  
                else  
                    $thumbImg = imagecreate($maxWidth, $maxHeight);  
          
                // 復制圖片  
                if (function_exists("ImageCopyResampled"))  
                    imagecopyresampled($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);  
                else  
                    imagecopyresized($thumbImg, $srcImg, 0, 0, $srcX, $srcY, $maxWidth, $maxHeight, $cutWidth, $cutHeight);  
                if ('gif' == $type || 'png' == $type) {  
                    //imagealphablending($thumbImg, false);//取消默認的混色模式  
                    //imagesavealpha($thumbImg,true);//設(shè)定保存完整的 alpha 通道信息  
                    $background_color = imagecolorallocate($thumbImg, 0, 255, 0);  //  指派一個綠色  
                    imagecolortransparent($thumbImg, $background_color);  //  設(shè)置為透明色,若注釋掉該行則輸出綠色的圖  
                }  
          
                // 對jpeg圖形設(shè)置隔行掃描  
                if ('jpg' == $type || 'jpeg' == $type)  
                    imageinterlace($thumbImg, $interlace);  
          
                // 生成圖片  
                $imageFun = 'image' . ($type == 'jpg' ? 'jpeg' : $type);  
                @chmod(dirname($thumbname),0777);  
                $imageFun($thumbImg, $thumbname);  
                imagedestroy($thumbImg);  
                imagedestroy($srcImg);  
                return $thumbname;  
            }  
            return false;  
        }  
          
          
        /** 
         * 取得圖像信息 
         * @static 
         * @access public 
         * @param string $image 圖像文件名 
         * @return mixed 
         */  
          
        static function getImageInfo($img) {  
            $imageInfo = getimagesize($img);  
            if ($imageInfo !== false) {  
                $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]), 1));  
                $imageSize = filesize($img);  
                $info = array(  
                        "width" => $imageInfo[0],  
                        "height" => $imageInfo[1],  
                        "type" => $imageType,  
                        "size" => $imageSize,  
                        "mime" => $imageInfo['mime']  
                );  
                return $info;  
            } else {  
                return false;  
            }  
        }  
          
        //watermark(源圖,生成文件,生成位置,水印文件,水印文本,背景色)  
        function watermark($source, $target = '', $w_pos = 9, $w_img = '', $w_text = '', $w_font = 12, $w_color = '#cccccc',$x='',$y='')  
        {  
            if( !$this->check($source)) return false;  
            if(!$target) $target = $source;  
            if ($w_img == '' && $w_text == '')  
            $w_img = $this->w_img;  
            $source_info = getimagesize($source);  
            $source_w    = $source_info[0]; //獲取寬  
            $source_h    = $source_info[1]; //獲取高  
            if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false; //寬和高達不到要求直接返回  
            switch($source_info[2]) //新建圖片  
            {  
                case 1 :  
                    $source_img = imagecreatefromgif($source);  
                    break;  
                case 2 :  
                    $source_img = imagecreatefromjpeg($source);  
                    break;  
                case 3 :  
                    $source_img = imagecreatefrompng($source);  
                    break;  
                default :  
                    return false;  
            }  
            if(!empty($w_img) && file_exists($w_img)) //水印文件  
            {  
                $ifwaterimage = 1; //是否水印圖  
                $water_info   = getimagesize($w_img); //水印信息  
                $width        = $water_info[0];  
                $height       = $water_info[1];  
                  
                switch($water_info[2])  
                {  
                    case 1 :  
                        $water_img = imagecreatefromgif($w_img);  
                      
                        break;  
                    case 2 :  
                        $water_img = imagecreatefromjpeg($w_img);  
                        break;  
                    case 3 :  
                        $water_img = imagecreatefrompng($w_img);  
                      
                        break;  
                    default :  
                        return;  
                }  
            }  
            else  
            {  
                $ifwaterimage = 0;  
                //imagettfbbox 本函數(shù)計算并返回一個包圍著 TrueType 文本范圍的虛擬方框的像素大小。  
                //imagettfbbox ( 字體大小, 字體角度, 字體文件,文件 )  
    //             echo $this->fontfile;  
                $temp = imagettfbbox(ceil($w_font*1.6), 0, $this->fontfile, $w_text);//取得使用 truetype 字體的文本的范圍  
                $width = $temp[4] - $temp[6]; //右上角 X 位置 - 左上角 X 位置  
                $height = $temp[3] - $temp[5]; //右下角 Y 位置- 右上角 Y 位置  
                unset($temp);  
            }  
            switch($w_pos)  
            {  
                case 0: //隨機位置  
                $wx = rand(0,($source_w - $width));  
                $wy = rand(0,($source_h - $height));  
                break;  
                case 1: //左上角  
                $wx = 25;  
                $wy = 25;  
                break;  
                case 2: //上面中間位置  
                $wx = ($source_w - $width) / 2;  
                $wy = 0;  
                break;  
                case 3: //右上角  
                $wx = $source_w - $width;  
                $wy = 0;  
                break;  
                case 4: //左面中間位置  
                $wx = 0;  
                $wy = ($source_h - $height) / 2;  
                break;  
                case 5: //中間位置  
                $wx = ($source_w - $width) / 2;  
                $wy = ($source_h - $height) / 2;  
                break;  
                case 6: //底部中間位置  
                $wx = ($source_w - $width) / 2;  
                $wy = $source_h - $height;  
                break;  
                case 7: //左下角  
                $wx = 0;  
                $wy = $source_h - $height;  
                break;  
                case 8: //右面中間位置  
                $wx = $source_w - $width;  
                $wy = ($source_h - $height) /2;  
                break;  
                case 9: //右下角  
                $wx = $source_w - $width;  
                $wy = $source_h - $height ;  
                break;  
                default: //隨機  
                $wx = rand(0,($source_w - $width));  
                $wy = rand(0,($source_h - $height));  
                break;  
                case 10://自定義位置  
                $wx = $x;  
                $wy = $y;  
                            case 11: //底部中間位置--手工客專用  
                $wx = ($source_w - $width) / 2;  
                $wy = $source_h - $height-50;  
                break;  
                break;  
            }  
            if($ifwaterimage) //如果有水印圖  
            {  
                  
                //imagecopymerge 拷貝并合并圖像的一部分  
                //參數(shù)(源圖,水印圖,拷貝到源圖x位置,拷貝到源圖y位置,從水印圖x位置,從水印圖y位置,高,寬,透明度)  
                //imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);  
                  
                imagecopy($source_img,$water_img,$wx,$wy,0,0,$width,$height);   
            }  
            else  
            {  
                if(!empty($w_color) && (strlen($w_color)==7))  
                {  
                    $r = hexdec(substr($w_color,1,2)); //獲取紅色  
                    $g = hexdec(substr($w_color,3,2)); //獲取綠色  
                    $b = hexdec(substr($w_color,5)); //獲取藍色  
                }  
                else  
                {  
                    return;  
                }  
                //imagecolorallocate 基于調(diào)色板的圖像填充背景色  
                //imagestring 水平地畫一行字符串  
                //imagestring(源圖,字體大小,位置X,位置Y,文字,顏色)  
                //參數(shù)($image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)  
                imagettftext($source_img,$w_font,0,$wx,$wy,imagecolorallocate($source_img,$r,$g,$b),$this->fontfile,$w_text);  
                //imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));  
            }  
              
            //輸出到文件或者瀏覽器  
            switch($source_info[2])  
            {  
                case 1 :  
                    imagegif($source_img, $target); //以 GIF 格式將圖像輸出到瀏覽器或文件  
                    break;  
                case 2 :  
                    imagejpeg($source_img, $target, $this->w_quality); //以 JPEG 格式將圖像輸出到瀏覽器或文件  
                    break;  
                case 3 :  
                    imagepng($source_img, $target); //以 PNG 格式將圖像輸出到瀏覽器或文件  
                    break;  
                default :  
                    return;  
            }  
            if(isset($water_info))  
            {  
                unset($water_info); //銷毀  
            }  
            if(isset($water_img))  
            {  
                imagedestroy($water_img); //銷毀  
            }  
            unset($source_info);  
            imagedestroy($source_img);  
            return true;  
        }  
        //gd庫必須存在,后綴為jpg|jpeg|gif|png,文件存在,imagecreatefromjpeg或者imagecreatefromgif存在  
        function check($image)  
        {  
            return extension_loaded('gd') &&  
            preg_match("/\.(JPG|JPEG|PNG|GIF|jpg|jpeg|gif|png)/i", $image, $m) &&  
            file_exists($image) &&  
            function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));  
            //imagecreatefromjpeg  
            //imagecreatefromgif  
            //imagecreatefrompng  
        }  
    }  
      
    /** 
     縮略圖 
    1.新建一個圖像資源 通過 imagecreatefromgif imagecreatefromjpeg imagecreatefrompng 
    2.imagecopyresampled 拷貝圖像,并調(diào)整大小 
     
    水印:圖片水印,文字水印 
    1. 創(chuàng)建圖像 
    2.加水印 
    圖片水印:imagecopymerge 把2張圖合并在一起 
    文字水。篿magettftext  向圖像寫入文字 
     
     
    */  
    error_reporting(0);  
    $hostdir = dirname(__FILE__);  
    $filesnames = scandir($hostdir.'/desktop');  
    $img = new image();  
    foreach($filesnames as $name){  
        if($name != '.' && $name != '..'){  
            $imgPath = $hostdir.'/desktop/';  
            $imgsize = getimagesize($imgPath.$name);  
            $imgInfo = explode('.',$name);  
            $imginfo2 = explode('_',$imgInfo[0]);  
            if($imginfo2[count($imginfo2)-1] != 'small'){  
                $img_small = $imgPath.$imgInfo[0].'_small.'.$imgInfo[1];  
                $img->thumb2($imgPath.$name,$img_small,'',$imgsize[0]/4,$imgsize[1]/4);  
            }     
        }  
    }  

標簽: 互聯(lián)網(wǎng)

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇: php 把漢字轉(zhuǎn)換為拼音

下一篇:PHP的文件操作