热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

图像生成器-PHP源码

图像生成器
图像生成器

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)
                        )
        );

推荐阅读
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • Matplotlib,带有已保 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文描述了作者第一次参加比赛的经历和感受。作者是小学六年级时参加比赛的唯一选手,感到有些紧张。在比赛期间,作者与学长学姐一起用餐,在比赛题目中遇到了一些困难,但最终成功解决。作者还尝试了一款游戏,在回程的路上感到晕车。最终,作者以110分的成绩取得了省一会的资格,并坚定了继续学习的决心。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
author-avatar
会唱歌的高跟鞋
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有