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

📄 image.class.php

📁 网络硬盘_支持1GB文件上传和续传_无错版
💻 PHP
📖 第 1 页 / 共 2 页
字号:
        case 'bmp':
			$imagetype = "jpg";
            $filename = $this->convertFiletype($filename,$imagetype);
            $this->converted_filename = $filename;
            $s_img = @imagecreatefromjpeg($filename);
			break;
        default:

	    }

        return $s_img;
    }
    
    function output($imgrs,$imagetype=-1,$file='')
    {
        if($imagetype==-1)
        {
            $type = getimagesize($filename);
            $imagetype = $type[type];
        }
        
        switch($imagetype)
        {
        case 1:
        case 'gif':
            $ok = imagegif($imgrs,$file);
			break;
        case 2:
        case 'jpg':
        case 'jpeg':
            $ok = imagejpeg($imgrs,$file);
			break;
        case 3:
        case 'png':
            $ok = imagepng($imgrs,$file);
			break;
        case 'tif':
        case 'tiff':
        case 6:
        case 'bmp':
            $ok = imagejpeg($imgrs,$file);
            if(strlen($this->converted_filename))@unlink( $this->converted_filename);
			break;
        default:

	    }
        return $ok;
    }
    
    function getExt($filename,$sep='.')
    {
        $ext = substr(strtolower(strrchr($filename,$sep)),1);
        #if($ext==''&&strpos($name,$sep)!==false) return ' ';
        return $ext;
    }
    /**
    * no dot . for newtype
    */
    function convertFiletype($file,$newtype,$virtual=0)
    {
        $type    = $this->getExt($file);
        $newfile = ($type?substr($file,0,-strlen($type)):$file.'.').$newtype;
        
        if(!$virtual)
        {
            if(strlen($this->tmpdir)) $newfile=$this->tmpdir.'/'.basename($newfile);
            $cmd = $this->IM_PATH."convert $file $newfile";

            system($cmd);
            return  $newfile;
        }
        else
        {
            return  $newfile;
        }
    }

    function getWH($width,$height)
    {
        # if fix size is allowed
        if($this->thumb_type == 'fix') return array($this->thumb_width,$this->thumb_height);

        # get the thumb width and height by the limtation
        $maxx = $this->thumb_width ==-1 ? $width  : $this->thumb_width;
        $maxy = $this->thumb_height==-1 ? $height : $this->thumb_height;


        # too small,not to genarate thumb:(
        if($maxx>$width&&$maxy>$height) return array($width,$height);
        if(($height/$width)>($maxy/$maxx))
        {
           $new_y=$maxy;
           $new_x=$new_y*($width/$height);
        }
        else
        {
           $new_x=$maxx;
           $new_y=$new_x*($height/$width);
        }
        return array($new_x,$new_y);
    }
    function getBarInfo($width,$height,$strlen,$type='GD')
    {
        if($type=='GD')
        {
            $str_width  = ImageFontWidth($this->font) * $strlen;
            $str_height = ImageFontHeight($this->font);
        }
        else
        {
            $str_width  = 6 * $strlen;
            $str_height = 12;
        }

        if($width>$height||$type=='IM')
        {
            $new_x=$width+2;
            $new_y=$height+$this->info_height+1;

            $str_x=($width-$str_width)/2;
            
            if($type=='GD')
            $str_y=$height+($this->info_height-$str_height)/2;
            else
            $str_y=$height+($this->info_height+$str_height)/2;
            
            $imagestring='imagestring';
        }
        else
        {
            $new_x=$width+$this->info_height+1;
            $new_y=$height+2;
            
            if($type=='GD')
            $str_x=$width+($this->info_height-$str_height)/2;
            else
            $str_x=$width+($this->info_height+$str_height)/2;
            
            $str_y=$height-($new_y-$str_width)/2;
            $imagestring='imagestringup';
        }
        return array($new_x,$new_y,$str_x,$str_y,$imagestring);
    }

    /**
    * extract a frame from video files by mplayer
    */
    function extractFrameByMplayer($movie,$destfile)
    {
        $work_dir=$this->tmpdir.'/'.time().'/';
        //mkdir($work_dir); //the below comand line will create the workdir
        $cmd = $this->MPLAYER_PATH."mplayer \"$movie\" -ss $this->play_time -nosound -vo jpeg:outdir=\"$work_dir\" -frames 1";
        exec($cmd,$output,$ret);

        if(file_exists($work_dir.'00000001.jpg'))
        {
            copy($work_dir.'00000001.jpg',$destfile);
        }
        else
        {
            copy($work_dir.'00000002.jpg',$destfile);
        }
       
        unlink($work_dir.'00000001.jpg');
        unlink($work_dir.'00000002.jpg');
        rmdir($work_dir);
    }
    /**
    * extract a frame from vedio files by mmfpeg
    * note:it doesn't work with some .wmv
    */
    function extractFrameByFFmpeg($movie,$destfile)
    {
        $cmd = $this->FFMPEG_PATH."ffmpeg -an -t 0:0:$this->play_time -i \"$movie\" -f singlejpeg \"$destfile\"";
        exec($cmd,$output,$ret);
    }
    /**
    * resize the images
    * @mode:done,preview.
    */
    function resizeImage($info,$mode='done')
    {
        $s_file = $this->srcdir.'/'.$info[file];
        # 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];

        $return = array();
        $return[width]  = ($width=intval($info[width]))<=0 ? $image[width] : $width;
        $return[height] = ($height=intval($info[height]))<=0 ? $image[height] : $height;

        /**
        * 1:create source images
        */
        $filetype=$this->getExt($info[file]);
        $s_img = $this->input($s_file,$filetype);

        # 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:create dest images and resized to it
        */
        $d_img = @imagecreatetruecolor($return[width], $return[height]);
        $black = @ImageColorAllocate($d_img, 0, 0 ,0 );
        @imagecopyresized($d_img, $s_img, 0, 0, 0, 0, $return[width], $return[height], $size[0], $size[1]);

        /**
        * 3:output croped image info
        */
        if($mode=='done')
        {
            $success = $this->output($d_img,$filetype,$s_file);
        }
        if($mode=='preview')
        {
            $success = $this->output($d_img,$filetype);
            ImageDestroy($s_img);
            ImageDestroy($d_img);
            exit;
        }
        
        ImageDestroy($s_img);
        ImageDestroy($d_img);
        
        # checking error
        if(!$success)
        {
            $this->setError(sprintf('%s can\'t saved correctly',$s_file));
            return -1;
        }
        
        # get the status of thumbnails, fail or ok?
        if(!is_file($s_file))
        {
            $this->setError(sprintf('Unknown error: image %s doesn\'t exist!',$s_file));
            return -1;
        }
        
        /**
        * 4:return resized image info
        */
        return $return;
    }
    /**
    * crop the images
    * @mode:done,preview.
    */
    function cropImage($info,$mode='done')
    {
        $s_file = $this->srcdir.'/'.$info[file];
        # checking error
        if(!is_file($s_file))
        {
            $this->setError(sprintf('%s doesn\'t exist',$s_file));
            return -1;
        }
        
        $size = getimagesize($s_file);
        if($info[w]>$size[0]) $info[w]=$size[0];
        if($info[h]>$size[1]) $info[h]=$size[1];
        if($info[x]>$size[0]) $info[x]=0;
        if($info[y]>$size[1]) $info[y]=0;
        
        $return = array();
        $return[width]  = $width=intval($info[w]-$info[x]);
        $return[height] = $height=intval($info[h]-$info[y]);
        
        /**
        * 1:create source images
        */
        $filetype=$this->getExt($info[file]);
        $s_img = $this->input($s_file,$filetype);

        # 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:create dest images and croped to it
        */
        $d_img = @imagecreatetruecolor($width, $height);
        $black = @ImageColorAllocate($d_img, 0, 0 ,0 );
        @imagecopyresized($d_img, $s_img, 0, 0, $info[x], $info[y], $width, $height, $width, $height);

        /**
        * 3:output croped image info
        */
        if($mode=='done')
        {
            $success = $this->output($d_img,$filetype,$s_file);
        }
        if($mode=='preview')
        {
            $success = $this->output($d_img,$filetype);
            ImageDestroy($s_img);
            ImageDestroy($d_img);
            exit;
        }

        ImageDestroy($s_img);
        ImageDestroy($d_img);

        # checking error
        if(!$success)
        {
            $this->setError(sprintf('%s can\'t saved correctly',$s_file));
            return -1;
        }

        # get the status of thumbnails, fail or ok?
        if(!is_file($s_file))
        {
            $this->setError(sprintf('Unknown error: image %s doesn\'t exist!',$s_file));
            return -1;
        }
        
        /**
        * 4:return croped image info
        */
        return $return;
    }
    function watermark($file)
    {
        require_once(dirname(__FILE__)."/transparentWatermark.inc.php");
        $WM=new  transparentWatermark($this->watermark_type);

        $WM->setOptions(array('stamp'=>$this->watermark_stamp,'font'=>$this->watermark_font,'color'=>$this->watermark_color));

	    // set logo's position (optional)
	    $WM->setStampPosition ( transparentWatermarkOnRight, transparentWatermarkOnBottom);

	    // create new image with logo
	    if (!$WM->markImageFile ($file,$file))
        die("Error:".$WM->getLastErrror()."\r\n");
    }
    
    function setError($err)
    {
        $this->errors .= $err."\n";
    }
    
    function convertsize($size,$mode=0)
    {
        $times = 0;
        $comma = '.';
        while ($size>1024){
           $times++;
           $size = $size/1024;
        }
        $size2 = floor($size);
        $rest = $size - $size2;
        $rest = $rest * 100;
        $decimal = floor($rest);

        $addsize = $decimal;
        if ($decimal<10) {$addsize .= '0';};

        if ($times == 0){$addsize=$size2;} else
        {$addsize=$size2.$comma.substr($addsize,0,2);}

        switch ($times) {
          case 0 : $mega = " Byte"; break;
          case 1 : $mega = " KB"; break;
          case 2 : $mega = " MB"; break;
          case 3 : $mega = " GB"; break;
          case 4 : $mega = ' TB'; break;
        }
        if($mode==1&&(($pos=strrpos($addsize,'.')))!==false)$addsize=substr($addsize,0,$pos);
        $addsize .= $mega;
        return $addsize;
    }
    function display($image,$name='')
    {
        @ob_end_clean();
        
        $name = $name == '' ? $image : $name;
        $content_type = $this->GetMime('.'.$this->getExt($image));
        header ("Content-type: $content_type");

        if(in_array($filetype,array('jpg','jpeg','gif','png','bmp')))
        {
            header("Content-Disposition: inline; filename=\"$name\"");
        }
        else
        {
            header("Content-Disposition: attachment; filename=\"$name\"");
        }
        
        $fp = fopen($this->srcdir .'/'.$image,'r');
        while($data=fread($fp,1024))
        {
           echo $data;
        }
        fclose($fp);
        exit;
    }
    function GetMime($type)
    {
        global $mime_mapping;
        return in_array($type, array_keys($mime_mapping)) ? $mime_mapping[$type]:$mime_mapping['.'];

    }
}
?>

⌨️ 快捷键说明

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