php教程 PHP源码 图像生成器

图像生成器

May 25, 2016 pm 05:06 PM
이미지 생성기

图像生成器

Width = $width;
                $this->Height = $height;
                $this->BackColor = $backColor;
                
                $this->_image=imagecreatetruecolor($this->Width, $this->Height);
                
                if (headers_sent()){
                        throw new RuntimeException('Header sent.');
                }

                // 初始化背景
                $backColor=imagecolorallocate($this->_image,
                        (int)($this->BackColor%0x1000000/0x10000),
                        (int)($this->BackColor%0x10000/0x100),
                        $this->BackColor%0x100);
                
                imagefilledrectangle($this->_image, 0, 0, $this->Width, $this->Height, $this->BackColor);
                
                imagecolordeallocate($this->_image, $this->BackColor);

                if($transparent){
                        // 设置透明
                        imagecolortransparent($this->_image, $this->BackColor);
                }
        }
        
        /**
         * 呈现的图片或文本,数组,按定义的顺序。
         * 元素为 x, y, type=text|image, content=图片路径或文本
         * @param $renders 要呈现的内容。
         * 模式如下:
         * array(
         *                 array(
         *                         'x'                => 0,
         *                         'y'                => 0, 
         *                         'type'        => 'text', 
         *                         'content'=>'图片路径或文本内容',
         *                         // 下面是文本的附加属性 
         *                         'font'        => null,
         *                         'fontsize'        => 30,
         *                         'angle'                => 0,
         *                         'color'                => 0x000000
         *                 )
         * );
         */
        public function Render(array $renders){
                foreach ($renders as $r){
                        if (!is_array($r)|| !isset($r['type'])|| !isset($r['content']))        {
                                continue;
                        }
                        
                        $type = $r['type'];
                        $x = isset($r['x'])?$r['x']:0;
                        $y = isset($r['y'])?$r['y']:0;
                        $ResizeRate = isset($r['ResizeRate'])?$r['ResizeRate']:1;
                        $AutoResize = isset($r['AutoResize'])?$r['AutoResize']:0;
                        $content = $r['content'];

                        if ($type==='text'){
                                $fontsize = isset($r['fontsize'])?$r['fontsize']:30;
                                $angle = isset($r['angle'])?$r['angle']:0;
                                $foreColor = isset($r['color'])?$r['color']:0x000000;
                                $fontfile = isset($r['font'])?$r['font']:null;

                                $this->RenderText($content, $x, $y, $fontsize, $angle, $foreColor, $fontfile);
                        }
                        else{
                                $this->RenderImage($content, $x, $y,$ResizeRate,$AutoResize);
                        }
                }
        }
        
        /**
        * 呈现文本。
        * @param string $text 文本。
        * @param integer $x 横坐标。
        * @param integer $y 纵坐标。
        * @param integer $size 字体尺寸,像素。
        * @param integer $angle 角度。
        * @param integer $foreColor 文本颜色。
        * @param string $fontfile 字体文件路径。 
        */
        public function RenderText($text, $x=0, $y=0, $size=30, $angle=0, $foreColor=0x000000, $fontfile=null){
                $c = func_num_args();
                if ($c===1 && is_array($text)){
                        extract($text);
                }
                
                if ($text===''){
                        return;
                }
                
                $fontfile = is_null($fontfile)?$this->FontFile:$fontfile;
                
                $foreColor=imagecolorallocate($this->_image,
                        (int)($foreColor%0x1000000/0x10000),
                        (int)($foreColor%0x10000/0x100),
                        $foreColor%0x100);

                imagettftext($this->_image, $size, $angle, $x, $y, $foreColor, $fontfile, $text);
                imagecolordeallocate($this->_image, $foreColor);
        }
        
        /**
         * 呈现图像,按 1:1 大小简单输出。
         * @param string $src 图像文件。
         * @param integer $x 横坐标。
         * @param integer $y 纵坐标。
         */
        public function RenderImage($src, $x=0, $y=0,$ResizeRate=1,$AutoResize=1){
                $c = func_num_args();
                if ($c===1 && is_array($src)){
                        extract($src);
                }

                if ($src===''){
                        return;
                }

                $meta = getimagesize($src);
                $w = $meta[0];
                $h = $meta[1];
                $new_width=$w/$ResizeRate;
                $new_height=$h/$ResizeRate;

                switch($meta[2]){
                    case IMAGETYPE_GIF:
                            $image=imagecreatefromgif($src);
                            break;
                    case IMAGETYPE_PNG:
                            $image=imagecreatefrompng($src);
                            break;
                    
                        case IMAGETYPE_JPEG:
                    default:
                            $image=imagecreatefromjpeg($src);
                            break;
            }

            //imagecopy($this->_image, $image, $x, $y, 0, 0, $w, $h); //简单输出
                if($AutoResize==1){
                        $this->Width;//当前底图的宽
                        $this->Height;//当前底图的高
                        $MaxSizeWidth = $this->Width-(2*$x);//缩小后的图片的最大宽度
                        $MaxSizeHeight = $this->Height-(2*$y);//缩小后的图片的最大高度
//                        echo $MaxSizeWidth;
//                        echo $MaxSizeHeight;
                        if( ($MaxSizeWidth>=$w) && ($MaxSizeHeight>=$h))//宽高都小,可以不缩小的情况,直接居中{
                                $outputSx=($this->Width-$w)/2;
                                $outputSy=($this->Height-$h)/2;
                                imagecopyresized($this->_image, $image,  $outputSx, $outputSy, 0, 0, $w, $h, $w, $h);
                        }
                        else{
                                $DstWHRate= $MaxSizeWidth/$MaxSizeHeight;//缩小后的长宽比
                                $SrcWhRate= $w/$h;//需要缩小的图片
                                if ($DstWHRate>=$SrcWhRate){
                                        $ResizeRate=($h/$MaxSizeHeight);//获得缩放比例                                        
                                }
                                else{
                                        $ResizeRate=($w/$MaxSizeWidth);//获得缩放比例
                                }
                                $new_width=ceil($w/$ResizeRate);
                                $new_height=ceil($h/$ResizeRate);
                                $outputSx=ceil(($this->Width-$new_width)/2);
                                $outputSy=ceil(($this->Height-$new_height)/2);
                                imagecopyresized($this->_image, $image,  $outputSx, $outputSy, 0, 0, $new_width, $new_height, $w, $h);
                        }
//                        echo $w;
//                        echo $h;
                }
                else{
                        imagecopyresampled($this->_image, $image,  $x, $y, 0, 0, $new_width, $new_height, $w, $h);
                }

            imagedestroy($image);
        }
        
        /**
         * 输出内容到浏览器。
         */
        public function Flush(){
                switch (strtoupper($this->Type)){
                        case 'JPG':
                                imagejpeg($this->_image);
                                break;
                        case 'GIF':
                                imagegif($this->_image);
                                break;
                        default:
                                imagepng($this->_image);
                                break;
                }
        }
        
        /**
         * 输出到文件或返回内容。
         * @param string $filename
         * @Return void 如果未提供文件名,则返回图像内容。如果提供了文件名则输出到文件中。
         */
        public function Output($filename=null){
                if (!empty($filename)){
                        switch (strtoupper($this->Type)){
                                case 'JPG':
                                        imagejpeg($this->_image, $filename);
                                        break;
                                case 'GIF':
                                        imagegif($this->_image, $filename);
                                        break;
                                default:
                                        imagepng($this->_image, $filename);
                                        break;
                        }
                }
                else{
                        ob_start();
                        switch (strtoupper($this->Type)){
                                case 'JPG':
                                        imagejpeg($this->_image);
                                        break;
                                case 'GIF':
                                        imagegif($this->_image);
                                        break;
                                default:
                                        imagepng($this->_image);
                                        break;
                        }
                        $r = ob_get_contents();
                        ob_end_clean();
                        return $r;
                }
        }
        
        /**
         * 释放资源,当对象实例卸载时也被隐式调用。
         */
        public function Dispose(){
                if (!is_null($this->_image)){
                        imagedestroy($this->_image);
                        $this->_image = null;
                }
        }

        public function __destruct(){
                $this->Dispose();
        }
        
        /**
         * 直接呈现。
         * @param integer $width 图像宽度。
         * @param integer $height 图像高度。
         * @param integer $backColor 背景颜色。
         * @param array $renders 呈现内容,同 Render 方法定义。
         */
        public static function DirectRender($width=100, $height=50, $backColor=0xFFFFFF, array $renders, $type="jpg"){
                header('Pragma: public');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Content-Transfer-Encoding: binary');
                header("Content-type: image/".($type==='JPG'?'jpeg':strtolower($type)));                
                $b = new self($width, $height, $backColor);
                $b->Render($renders);
                $b->Flush();
                $b->Dispose();
        }
        
        /**
         * 呈现到文件或返回图像数据。
         * @param integer $width 图像宽度。
         * @param integer $height 图像高度。
         * @param integer $backColor 背景颜色。
         * @param array $renders 呈现内容,同 Render 方法定义。
         * @param string $filename 呈现到的文件名,不提供则直接返回内容。
         */
        public static function RenderTo($width=100, $height=50, $backColor=0xFFFFFF, array $renders, $filename=null){
                $b = new self($width, $height, $backColor);
                $b->Render($renders);
                $r = $b->Output($filename);
                $b->Dispose();                
                return $r;
        }        
        
}

// 简单组合文本和图片,并且返回图像数据。
   ImageBuilder::DirectRender(900, 700, 0xF0F0F0, array(        
                   array('x'=>0,'y'=>0,'type'=>'image', 'content'=>'bg/asw.gif'),
                        array('x'=>260,'y'=>30,'type'=>'image', 'content'=>'1.jpg'),
                        array('x'=>75, 'y'=>54, 'type'=>'text', 'content'=>'大水车', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>75, 'y'=>86, 'type'=>'text', 'content'=>'中性', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>155, 'y'=>86, 'type'=>'text', 'content'=>'宅族', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>75, 'y'=>116, 'type'=>'text', 'content'=>'1980', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>135, 'y'=>116, 'type'=>'text', 'content'=>'11', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>170, 'y'=>116, 'type'=>'text', 'content'=>'11', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>75, 'y'=>146, 'type'=>'text', 'content'=>'湖北省仙桃市靠山屯村', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>75, 'y'=>166, 'type'=>'text', 'content'=>'六蛋七巷 8 弄 110 号', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000),
                        array('x'=>145, 'y'=>219, 'type'=>'text', 'content'=>'312306198011113298', 'fontsize'=> 12, 'font'=>'ARIALUNI.TTF', 'color'=>0x000000)
                        )
        );
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
2 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)