0

0

图像生成器

PHP中文网

PHP中文网

发布时间:2016-05-25 17:06:11

|

1454人浏览过

|

来源于php中文网

原创

图像生成器

AI卡通生成器
AI卡通生成器

免费在线AI卡通图片生成器 | 一键将图片或文本转换成精美卡通形象

下载
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工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法

本专题汇总了包子漫画官网和网页版入口,提供最新章节抢先看方法、正版免费阅读指南,以及稳定访问方式,帮助用户快速直达包子漫画页面,无广告畅享全集漫画内容。

47

2026.02.10

MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法
MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法

本专题汇总了MC.JS官网入口和网页版快速畅玩方法,提供免安装访问、不同版本(1.8.8、1.12.8)在线体验指南,以及正版网页端操作说明,帮助玩家轻松进入MC.JS世界,实现即时畅玩与高效体验。

33

2026.02.10

谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程
谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程

本专题汇总了谷歌邮箱网页版的最新登录入口和注册方法,详细提供官方账号快速访问方式、网页版操作教程及安全登录技巧,帮助用户轻松管理Gmail邮箱账户,实现高效、安全的邮箱使用体验。

25

2026.02.10

铁路12306订票与退改全攻略_高效购票与座位选取技巧
铁路12306订票与退改全攻略_高效购票与座位选取技巧

本专题全面汇总铁路12306订票、退票、改签及候补订单操作技巧,提供车厢座位分布参考、抢票攻略和高铁安检注意事项,帮助新手用户快速掌握高效购票与退改流程,提高出行效率和体验。

27

2026.02.10

TensorFlow2深度学习模型实战与优化
TensorFlow2深度学习模型实战与优化

本专题面向 AI 与数据科学开发者,系统讲解 TensorFlow 2 框架下深度学习模型的构建、训练、调优与部署。内容包括神经网络基础、卷积神经网络、循环神经网络、优化算法及模型性能提升技巧。通过实战项目演示,帮助开发者掌握从模型设计到上线的完整流程。

0

2026.02.10

Vue3组合式API与组件开发实战
Vue3组合式API与组件开发实战

本专题讲解 Vue 3 组合式 API 的核心概念与应用技巧,深入分析响应式系统、生命周期管理、组件设计与复用策略。通过完整项目案例,指导前端开发者实现高性能、结构清晰的 Vue 应用,提升开发效率与代码可维护性。

4

2026.02.10

Go语言微服务架构与gRPC实战
Go语言微服务架构与gRPC实战

本专题面向有 Go 基础的开发者,系统讲解微服务架构设计与 gRPC 的高效应用。内容涵盖服务拆分、RPC 通信、负载均衡、错误处理、服务注册与发现等关键技术。通过实战案例,帮助开发者搭建高性能、可扩展的 Go 微服务系统。

1

2026.02.10

React 18状态管理与Hooks高级实践
React 18状态管理与Hooks高级实践

本专题专注于 React 18 的高级开发技术,详细讲解 useState、useEffect、useReducer、useContext 等 Hooks 的使用技巧,以及 Redux、Zustand 等状态管理工具的集成与优化方法。通过真实案例,帮助前端开发者构建可维护、性能优良的现代 React 应用。

4

2026.02.10

Node.js后端开发与Express框架实践
Node.js后端开发与Express框架实践

本专题针对初中级 Node.js 开发者,系统讲解如何使用 Express 框架搭建高性能后端服务。内容包括路由设计、中间件开发、数据库集成、API 安全与异常处理,以及 RESTful API 的设计与优化。通过实际项目演示,帮助开发者快速掌握 Node.js 后端开发流程。

2

2026.02.10

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
ASP 教程
ASP 教程

共34课时 | 4.7万人学习

ECMAScript6 / ES6---十天技能课堂
ECMAScript6 / ES6---十天技能课堂

共25课时 | 2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号