⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.class.php

📁 网络硬盘_支持1GB文件上传和续传_无错版
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php
/*
+--------------------------------------------------------------------------
|   Mega File Hosting Script v1.2
|   ========================================
|   by Stephen Yabziz
|   (c) 2005-2006 YABSoft Services
|   http://www.yabsoft.com
|   ========================================
|   Web: http://www.yabsoft.com
|   Email: ywyhnchina@163.com
+--------------------------------------------------------------------------
|
|   > Script written by Stephen Yabziz
|   > Date started: 1th March 2006
+--------------------------------------------------------------------------
*/
/**
* @package MFHS
* Image class
*/
class Image {
    var $GD_INSTALLED      = true;
    var $IM_INSTALLED      = false;
    var $MPLAYER_INSTALLED = false;
    var $FFMPEG_INSTALLED  = false;
    
    var $IM_PATH           = '';
    var $MPLAYER_PATH      = '';
    var $FFMPEG_PATH       = '';
    
    var $USE_MPLAYER       = true;
    var $USE_IM            = false;

    var $thumb_width       = 150;
    var $thumb_height      = 150;
    var $thumb_type        = 'fix';
    var $small_thumb       = 'continue';
    
    var $bar_info          = '{width}x{height} {size}';
    var $font              = 3;
    var $info_height       = 15;

    var $watermark_type    = 'TEXT';
    var $watermark_font    = 'TEXT';
    var $watermark_color   = 'TEXT';
    var $watermark_stamp   = 'Powered by Image class';
    var $watermark_postion = 'TEXT';
    
    var $play_time         = 0.001;

    var $errors            = null;

    var $tmpdir            = 'temp';
    var $srcdir            = 'upload';
    var $thbdir            = 'thumb';
    
    # image gd can
    var $ImageFile         = array('jpg','jpeg','gif','png');
    # image imagemagic can
    var $ImageFile2        = array('bmp','tiff','tif');
    //var $ThumbFile         = array('jpg','jpeg','bmp','tiff','tif','gif','png','mpeg','mpg','avi','wmv');
    var $VideoFile         = array('mpeg','mpg','avi','wmv');
    var $FlashFile         = array('swf');
    var $AduioFile         = array();
    var $AchiveFile        = array();

	/**
	* image object constructor
	*/
	function Image()
    {
        $this->GD_INSTALLED = extension_loaded('gd');
	}
 
    function setWorkDirs($src='files',$thb='thumb',$tmp='tmp')
    {
        is_dir($src) ? $this->srcdir = $src : die('Source directory doesn\'t exists:'.$src);
        is_dir($thb) ? $this->thbdir = $thb : die('Thumb directory doesn\'t exists:'.$thb);
        is_dir($tmp) ? $this->tmpdir = $tmp : die('Temporary directory doesn\'t exists:'.$tmp);
    }
    
    function setModuleStatus($im=0,$mplayer=0,$ffmpeg=0)
    {
        $this->IM_INSTALLED      = $im;
        $this->MPLAYER_INSTALLED = $mplayer;
        $this->FFMPEG_INSTALLED  = $ffmpeg;
    }
    
    function canThumb($name,$sep='.')
    {
        return (($this->GD_INSTALLED||$this->IM_INSTALLED)&&in_array($this->getExt($name,$sep),$this->ImageFile))
               ||($this->IM_INSTALLED&&in_array($this->getExt($name,$sep),$this->ImageFile2))
               ||(($this->MPLAYER_INSTALLED||$this->FFMPEG_INSTALLED)&&in_array($this->getExt($name,$sep),$this->VideoFile));
    }
    
    function isImage($name,$sep='.')
    {
        if($this->USE_IM)
        return in_array($this->getExt($name,$sep),$this->ImageFile)||in_array($this->getExt($name,$sep),$this->ImageFile2);
        else
        return in_array($this->getExt($name,$sep),$this->ImageFile);
    }
    
    function isVideo($name,$sep='.')
    {
        return in_array($this->getExt($name,$sep),$this->VideoFile);
    }
    
    function isAduio($name,$sep='.')
    {
        return in_array($this->getExt($name,$sep),$this->AduioFile);
    }
    
    function isFlash($name,$sep='.')
    {
        return in_array($this->getExt($name,$sep),$this->FlashFile);
    }
    
    function isAchive($name,$sep='.')
    {
        return in_array($this->getExt($name,$sep),$this->AchiveFile);
    }
    
    function createThumb($image,$info=1)
    {
        if($this->USE_IM)
        {
            $return =  $this->createThumbByIM($image,$info);
        }
        else
        {
            $return =  $this->createThumbByGD($image,$info);
        }
        
        return $return;
    }
    
    function createThumbByGD($image,$info=1)
    {
        # detecting gd
        if(!$this->GD_INSTALLED)
        {
            $this->errors .= 'GD is not installed!';
            return -1;
        }
        /**
        *0:prepare the var,...
        */
        # convert non normal image to jpg format:
        if(!in_array($this->getExt($image[name],'_')?$this->getExt($image[name],'_'):$this->getExt($image[name]),$this->ImageFile)) $image[thumb] = $this->convertFiletype($image[thumb],'jpg',1);
        
        $s_file  =$this->srcdir.'/'.$image[name];
        $d_file  =$this->thbdir.'/'.$image[thumb];

        # checking error
        if(!is_file($s_file))
        {
            $this->setError(sprintf('%s doesn\'t exist',$s_file));
            return -1;
        }
        
        # get image info
        $imginfo = getimagesize($s_file);
        $image[width]  = $imginfo[0];
        $image[height] = $imginfo[1];
        $image[type]   = $imginfo[2];
        $image[size]   = filesize($s_file);
        
        /**
        *1:create the image source
        */
        $s_img = $this->input($s_file,$image[type]?$image[type]:-1);
        
        # checking error
        if(!is_resource($s_img))
        {
            $this->setError(sprintf('%s can\'t handled, file corruption or by gd libery issue!',$s_file));
            return -1;
        }

        /**
        *2:get the thumb width and height by the limitation
        */
        list($new_x,$new_y)=($this->getWH($image[width],$image[height]));

        # processing thumbnail of small images
        if($new_x==$image[width] && $new_y==$image[height])
        {
            if($this->small_thumb=='copy')
            {
                if(!copy($s_file,$d_file))
                {
                    $this->setError(sprintf('%s can\'t be copied to %s!',$s_file,$d_file));
                    return -1;
                }
                return $image;
            }
            if($this->small_thumb=='return')
            {
                return $image;
            }
            // continue
        }
        
        /**
        * 3:generate the thumb images
        */
        if($info&&strlen($this->bar_info))
        {
            /**
            *3.1:build the info text and position
            */
            $string = str_replace('{width}',$image[width],$this->bar_info);
            $string = str_replace('{height}',$image[height],$string);
            $string = str_replace('{imagename}',$image[name],$string);
            $string = str_replace('{size}',$this->convertsize($image[size],1),$string);

            list($new_x2,$new_y2,$str_x,$str_y,$imagestring) = $this->getBarInfo($new_x,$new_y,strlen($string));

            $d_img = imagecreatetruecolor($new_x2, $new_y2);
            $black = ImageColorAllocate($d_img, 0, 0 ,0 );
            $white = ImageColorAllocate($d_img, 255, 255 ,255 );
            $imagestring ($d_img, $this->font, $str_x, $str_y,  $string, $white);
            imagecopyresized($d_img, $s_img, 1, 1, 0, 0, $new_x, $new_y, $image[width], $image[height]);
        }
        else
        {
            $d_img = imagecreatetruecolor($new_x, $new_y);
            imagecopyresized($d_img, $s_img, 0, 0, 0, 0, $new_x, $new_y, $image[width], $image[height]);
        }

        /**
        *5:store the thumb images to file!
        */
        $success = $this->output($d_img,$image[type],$d_file);
        # checking error
        if(!$success)
        {
            $this->setError(sprintf('%s can\'t saved correctly',$d_file));
            return -1;
        }
        
        ImageDestroy($s_img);
        ImageDestroy($d_img);
        
        # get the status of thumbnails, fail or ok?
        if(!is_file($d_file))
        {
            $this->setError(sprintf('Unknown error: thumbnail %s doesn\'t exist!',$d_file));
            return -1;
        }
        
        return $image;
    }
    
    function createThumbByIM($image,$info=1)
    {
        /**
        *prepare the var,...
        */
        # convert non normal image to jpg format:
        if(!in_array($this->getExt($image[name],'_')?$this->getExt($image[name],'_'):$this->getExt($image[name]),$this->ImageFile)) $image[thumb] = $this->convertFiletype($image[thumb],'jpg',1);

        $s_file  = $this->srcdir.'/'.$image[name];
        $d_file  = $this->thbdir.'/'.$image[thumb];

        # checking error
        if(!is_file($s_file))
        {
            $this->setError(sprintf('%s doesn\'t exist',$s_file));
            return -1;
        }
        
        # get image info
        $imginfo = getimagesize($s_file);
        $image[width]  = $imginfo[0];
        $image[height] = $imginfo[1];
        $image[type]   = $imginfo[2];
        $image[size]   = filesize($s_file);

        /**
        *2:get the thumb width and height by the limtation
        */
        list($new_x,$new_y)=($this->getWH($image[width],$image[height]));

        # processing thumbnail of small images
        if($new_x==$image[width] && $new_y==$image[height])
        {
            if($this->small_thumb=='copy')
            {
                if(!copy($s_file,$d_file))
                {
                    $this->setError(sprintf('%s can\'t be copied to %s!',$s_file,$d_file));
                    return -1;
                }
                return $image;
            }
            if($this->small_thumb=='return')
            {
                return $image;
            }
            // continue
        }
        
        # font file
        $this->font_file='fonts/arialbd.ttf';

        /**
        *3:build the info text and position
        */
        if($info&&strlen($this->bar_info))
        {
            $string = str_replace('{width}',$image[width],$this->bar_info);
            $string = str_replace('{height}',$image[height],$string);
            $string = str_replace('{imagename}',$image[name],$string);
            $string = str_replace('{size}',$this->convertsize($image[size],1),$string);

            list($info_x,$info_y,$str_x,$str_y,$imagestring) = $this->getBarInfo($new_x,$new_y,strlen($string),'IM');

            $cmd = $this->IM_PATH."convert -size {$info_x}x{$info_y} xc:black -font $this->font_file -pointsize 11 -fill white -draw \"text $str_x, $str_y '$string'\" -draw \"image over 0,0 $new_x,$new_y $s_file\" $d_file";

        }
        else
        {
            $cmd = $this->IM_PATH."convert -size {$new_x}x{$new_y} xc:black -draw \"image over 0,0 $new_x,$new_y $s_file\" $d_file";
        }
        system($cmd);
        
        # get the status of thumbnails, fail or ok?
        if(!is_file($d_file))
        {
            $this->setError(sprintf('Unknown error: thumbnail %s doesn\'t exist!',$d_file));
            return -1;
        }

        return $image;
    }
    
    function createVideoThumb($image,$info=1)
    {
        /**
        *0:prepare the var,...
        */
        # convert non normal image to jpg format:
        if(!in_array($this->getExt($image[name],'_')?$this->getExt($image[name],'_'):$this->getExt($image[name]),$this->ImageFile)) $image[thumb] = $this->convertFiletype($image[thumb],'jpg',1);

        $s_file  = $this->srcdir.'/'.$image[name];
        $d_file  = $this->thbdir.'/'.$image[thumb];

        /**
        *1:extracting a frame from video file
        */
        if($this->USE_MPLAYER)
        {
            $this->extractFrameByMplayer($s_file,$d_file);
        }
        else
        {
            $this->extractFrameByFFmpeg($s_file,$d_file);
        }

        if(!file_exists($d_file))
        {
            $this->setError(sprintf('Frame %s can\'t be extracted from video!',$d_file));
            return -1;
        }
        /**
        *2:thumbnailing the extracted frame of video files
        */
        # resetting source dir
        $tmp = $this->srcdir;
        $this->srcdir = $this->thbdir;
        
        $newimage = array('name'=>$image[thumb],'thumb'=>$image[thumb]);
        if($this->USE_IM)
        {
            $return =  $this->createThumbByIM($newimage,$info);
        }
        else
        {
            $return =  $this->createThumbByGD($newimage,$info);
        }
        
        # restoring source dir
        $this->srcdir = $tmp;
        
        return $return;
    }
    
    function input($filename,$imagetype=-1)
    {
        if($imagetype==-1)
        {
            $type = @getimagesize($filename);
            $imagetype = $type[2];
        }

        switch($imagetype)
        {
        case 1:
        case 'gif':
            $imagetype = "gif";
            if( function_exists("imagecreatefromgif") )
         	{
			    $s_img = @imagecreatefromgif($filename);
			    break;
			}
        case 2:
        case 'jpg':
        case 'jpeg':
			$imagetype = "jpg";
            $s_img = @imagecreatefromjpeg($filename);
			break;
        case 3:
        case 'png':
			$imagetype = "png";
            $s_img = @imagecreatefrompng($filename);
			break;
        case 'tif':
        case 'tiff':
        case 6:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -