好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

PHP生成PNG图片,文字换行问题,和解析XML中带有CD

出院后我负责开发的应用马上就要上线了 开发几天遇到这样几个问题,都解决了,以后在遇到类似的问题就不会这么麻烦了 以下是我实际应用中写的几个函数,可以重复利用 :) 问题一: 图片通过GD库生成会出现文字漂白问题,或者不显示,调试浪费了我半天的时间 合成图片颜色是真问题,困扰我很久最后解决了 原来imagecreate不支持更多的色彩 imagecreatetruecolor解决了这个问题 本函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。  

代码如下

        /**

         * @desc 创建结果图片

         * @param

         * @return file

         */

        public function create_userimg ( ) {

           

                $user_name = addslashes ( $this -> input -> get ( 'name' ) ) ;

                $user_name = urldecode ( $user_name ) ;

                $user_score = ( int ) $this -> input -> get ( 'score' ) ;

               

            if ( preg_match ( "/^[x7f-xff]+$/" , $user_name ) ) {

                $user_name_len =   mb_strlen ( $user_name ) ;

                    $user_name_len = ceil ( $user_name_len / 3 ) ;

                    $nickleft = 30 + 60 * $user_name_len ;

                } else {

                    $user_name_len =   strlen ( $user_name ) ;

                   

                    $nickleft = 120 + 13 * $user_name_len ;

                }                

 

                $font = '.' . PATH_FONT . '/yahei.ttf' ;

                $font_kaiti = '.' . PATH_FONT . '/SIMLI.TTF' ;

                $size = 25 ;

               

                //$bgimg = 'resource/default/images/lohas/result_bg.png';

                $bgimg = 'resource/default/images/shishang/result_show.png' ;

                $nick = 'resource/default/images/shishang/shishang_nick.png' ;

               

                $im = @ imagecreatetruecolor ( 506 , 432 ) or die ( "Cannot Initialize new GD image stream" ) ;

        $white = imagecolorallocate ( $im , 255 , 255 , 255 ) ;

        imagefill ( $im , 0 , 0 , $white ) ;

            imagecopy ( $im , $this -> Image , 0 , 0 , 0 , 0 , 506 , 432 ) ;

           

                $bgimg = imagecreatefrompng ( $bgimg ) ;

                imagecopy ( $im , $bgimg , 0 , 0 , 0 , 0 , 506 , 432 ) ;

               

                $nick = imagecreatefrompng ( $nick ) ;

                imagecopy ( $im , $nick , $nickleft , 136 , 0 , 0 , 77 , 14 ) ;

               

               

                $green = imagecolorallocate ( $im , 68 , 160 , 28 ) ;

                $hei = imagecolorallocate ( $im , 0 , 0 , 0 ) ;

                $bai = imagecolorallocate ( $im , 255 , 255 , 255 ) ;

       

                if ( preg_match ( "/^[x7f-xff]+$/" , $user_name ) ) {

                    imagettftext ( $im , '25' , 0 , 62 , 150 , $hei , $font_kaiti , $user_name ) ;

                } else {

                    imagettftext ( $im , '21' , 0 , 62 , 150 , $hei , $font , $user_name ) ;

                }

               

                imagettftext ( $im , '12' , 0 , 70 , 209 , $green , $font , $text_1 ) ;

               

                imagettftext ( $im , '11' , 0 , 170 , 238 , $green , $font , $text_2 ) ;

               

                imagettftext ( $im , '12' , 0 , 360 , 210 , $green , $font , $score ) ;

               

                imagettftext ( $im , '11' , 0 , 95 , 237 , $green , $font , $user_vs ) ;

               

                imagettftext ( $im , '10' , 0 , 330 , 340 , $hei , $font , $date_text ) ;

 

               

       

                header ( "Content-type: image/png" ) ;

                imagepng ( $im ) ;

                imagedestroy ( $im ) ;

        }

  问题二: PHP中文换行问题,因为文字要写在图片里,类似css的自动换行, php对英文有wordwrap()函数支持换行,但不支持中文,下面函数解决了PHP中文换行问题

代码如下

/**

     * @desc GD库生成图片中文自动换行

     * 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度

     * */

    public function autowrap ( $fontsize , $angle , $fontface , $string , $width ) {

        $content = "" ;

 

        // 将字符串拆分成一个个单字 保存到数组 letter 中

        for ( $i = 0 ; $i < mb_strlen ( $string ) ; $i ++ ) {

            $letter [ ] = mb_substr ( $string , $i , 1 ) ;

        }

       

        foreach ( $letter as $l ) {

            $teststr = $content . "" . $l ;

            $testbox = imagettfbbox ( $fontsize , $angle , $fontface , $teststr ) ;

            // 判断拼接后的字符串是否超过预设的宽度

            if ( ( $testbox [ 2 ] > $width ) && ( $content !== "" ) ) {

                $content .= "n" ;

            }

            $content .= $l ;

        }

       

        $content = mb_convert_encoding ( $content , "html-entities" , "utf-8" ) ;

       

        return $content ;

    }

查看更多关于PHP生成PNG图片,文字换行问题,和解析XML中带有CD的详细内容...

  阅读:44次