• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

php 如何将image图片转化为字符串(GD库操作及imagick两种实现方式) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

前两天研究php中的 imagick 扩展的时候,突发奇想实现的一个小功能感觉挺有意思,在这里记录一下:

将一张image图片转化为字符串的形式,先上一张效果图。(运行笔记中的代码需要先安装 php_imagick 扩展 教程可以看这里:PHP扩展之 Imagick安装

 

                                                                                                                                              
                                                                                                                                              
                                                                                                                                              
                                                          ,;+:               ,+:.                                                             
                                                   .fLLLLLLLLLf             fLLLLLLLLLf.                                                      
                                                .LLLLCLLLLLLLCL*           *LLLLLLLLLLLLLL.                                                   
                                              .LLLLLLLLLLLLLLLCL.         .LLLLLLLLLLLLLLLLL.                                                 
                                             +LLLLLLLLLLLLLLCLLLL         LLLLLLLLLLLLLLLLLLL*                                                
                                            tLLLLLLLLLLLLLLLLLLLL:       :LLLLLLLLLLLLLLLLLLLLt                                               
                                           .LLLLLLLLLLLLLLLLLLLLLL       LLLLLLLLLLLLLLLLLLCLLL:                                              
                                           LCLLLLLLLLLLLLLLLLLLLLL.     .LLLLLLLLLLLLLLLLLCLLLLL                                              
                            +LL.           LLLLLLLLLLLLLLLLLLLLLLLt     tLLLLLLLLLLLLLLLLLLLLLLL           .LL*                               
                          .LLLCLt          LLLLLLLLLLLLLLLLLLLLLLLL     LLLLLLLLLLLLLLLLLLLLLLLL          tLLLLL,                             
                         fLLLLLLLL.        fLLLLLLLLLLLLLLLLLLLLLLL     LLLLLLLLLLLLLLLLLLLLLLLf        .LLLLLLLLf                            
                        LLLLLLLLLLLf       ;LLLLLLLLLLLLLLLLLLLLLLL:   :LLLLLLLLLLLLLLLLLLLLLLL;       tLLCLLLLLLLL.                          
                      .LLLLLLLLLLLLLL.      LLLLLLLLLLLLLLLLLLLLLLLt   tLLLLLLLLLLLLLLLLLLLLLLL      .LLLLLLLLLLLLLL,                         
                      LLLLLLLLLLLLLLLL*     ,LLLLLLLLLLLLLLLLLLLLLLf   fLLLLLLLLLLLLLLLLLLLLLL:     *LLLLLLLLLLLLLLLC.                        
                     LLLLLLLLLLLLLLLLLLL     fCLLLLLLLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLLLCLLLLf     LLLCLLLLLLLLLLLLLLL                        
                    :LCLLLLLLLLLLLLLLLLLL:    LLLLLLLLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLLLCLLLL    ,LLLLLLLLLLLLLLLLLLLL:                       
                    fLLLLLLLLLLLLLLLLLLLLLf    LLLLLLLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLLLLLLL    fLCLLLLLLLLLLLLLLLLLLLf                       
                    fLLLLLLLLLLLLLLLLLLLLLLL    LCLLLLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLLLLLL.   LLLLLLLLLLLLLLLLLLLLLLLf                       
                    :LLLLLLLLLLLLLLLLLLLLLLLC,  .LLLLLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLLLLL.  ,LLLLLLLLLLLLLLLLLLLLLLLL:                       
                     LLLLLLLLLLLLLLLLLLLLLLLLLt  .LLLLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLLLL,  *LLLLLLLLLLLLLLLLLLLLLLLLL                        
                      LLLLLLLLLLLLLLLLLLLLLLLLLL  .LLLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLLL.  LLLLLLLLLLLLLLLLLLLLLLLLLL                         
                       LLLLLLLLLLLLLLLLLLLLLLLLLL.  LLLLLLLLLLLLLLLL   LLLLLLLLLLLLLLLC.  LLLCLLLLLLLLLLLLLLLLLLLLLL                          
             +.         ,LLLLLLLLLLLLLLLLLLLLLLLLL, .LLLLLLLLLLLLLLL   LLLLLLLLLLLCLLL  .LLLLLLLLLLLLLLLLLLLLLLLLL,         ,*                
            :LLCLf        ,LLLLLLLLLLLLLLLLLLLLLLCL+  LLLLLLLLLLLLLL   LLLLLLLLLLLLLL  ;CLLCLLLLLLLLLLLLLLLLLLLL,       .fLLLL;               
            fLLLLLLLL+       fLLLLLLLLLLLLLLLLLLLLLLt  fLLLLLLLLLLLL   LLLLLLLLLLLLf  *LLLLLLLLLLLLLLLLLLLLLLL       +LLLLLLLLL               
            LLLLLLLLLLLLL,     :LLLLLLLLLLLLCLLLLLLLLf  +CLLLLLLLLLL   LLLLLLLLLLL+  fLLLLLLLLLLLLLCLLLLLLL:     ,LLLLLLLLLLLLL               
            LLLLLLLLLLLLLLLLt     +LLLLLLLLCLCLLLLCLLLL  ,LLLLLLLLLf   fLLLLLLLLL,  LLLLLLLLLLLLLLLLLLLL+     tLLLLLLLLLLLLLLLL               
            +LLLLLLLLLLLLLLLLLLL:    ;LLLLLLLLLLLLLLLLLL  .LLLLLLLLt   tLLLLLLLL   LLLCLLLLLLLLLLLLLL;    ;LLLLLLLLLLLLLLLLLLL*               
             LLLLLLLLLLLLLLLLLLLLLLf    ,LLLLLLLLLLLLLLLL   fLLLLLL;   ;LLLLLLL   LLLLLLLLLLLLLLLL,   .fLCLLLLLLLLLLLLLLLLLLLL                
             ;LLLLLLLLLLLLLLLLLLCLLLLLL:   .fLLLLLLLLLCLLL   +LLLLL.    LCLLL+   LLLLLLLLLLLLLf.   :LLLLLLLLLLLLLLLLLLLLLLLLL;                
              +LLLLLLLLLLLLLLLLLLCLLLLLLLLt   .:LLLLLLLLLLL   .CLLL     LLLL.   LLLLLLLLLLL:    tLLLLLLLLLLLLLLLLLLLLLLLLLLL*                 
               ,LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL,    tCLLLLLLL    LLL     LLL    LLLLLLLLt    ,LLLLLLLLLLLLLLLLLLLLLLLLLLLLLL:                  
                 fLLLLLLLLLLLLLLLLLLLLLLLLLCLLLLL;    .fLLLLL    ,:     ::    LLLLLf.    ;LLLCLLLLLLLLLLLLLLLLLLLLLLLLLLLf                    
                   fLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLt     ,LLL               fLL,     tLLLLLLLLLLLLLLLLLLLLLLLCLLLLLLLLf                      
                     ,LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLf                           fLLLLLLLLLLLLLLLLLLLLLLLLCLLLLLLLL,                        
                         :fLLLLLLLLLLLLLLLLLLLLLCLLLLLLLLLL                     fCLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLf:                            
                                           .    ..  . ...                        .  ....                                                      
                               ,,:;++*ttfffffLLLLLLLLLLLLL;                     :CLLLLLLLLLLLLfffftt**++::,.                                  
                       fLLLLLLLLLLLLLLLLLLLLLLLLLLCLLLLL:                         :LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL                          
                        +LLLCLLLLLLLLLLLLLLLLLLLLLLLL;                               ;LLLLCLLLLLLLLLLLLLLLLLLLLLLL+                           
                          fLLLLLLLLLLLLLLLLLLLLLLL:                                     :LLLLLLLLLLLLLLLLLLLLLLLL                             
                            fLLLLLLLLLLLCLLLLCL,                                           ,LLLLLLLLLLLLLLLLLLf                               
                              .fLLLLLLLLLLL;                                                   ;LLLLCLLLLLLL.                                 
                                                                                                                                              
                                                                                                                                              
                                                                                                                                              
                                                                                                                                              
                                                                                                                                              
                                                                                                                                              
            +GGG+        fG0G,    +GGG+        LGGG.        .GGGGt      tG0G*       L0GG;       GGGG   ;GGG0GGGGG00GGL    ,GGGt               
            +GGG+        L0GG,    +GGG+        LGGG.        GG00GG+      C0G0,     +G0GGG      tG0G;   ;GGGLtffffffff*    ,GGGt               
            +GGG*        fGGG,    +GGG+        LGGG.       CGGGfG0G;     ,GGGC    .GGG0GGt    :0GGf    ;GGG*              ,GGGt               
            +GGG*........LGGG,    +GGG+        LGGG.      LGG0. LGG0,     *GGG+   LGGGtGGG:   GGGG     ;GGGf++++++,       :GGGt               
            +GGGG0G0GGG0GGG0G,    +GGG+        LGG0.     tG0G:   CGGG.     C0GG. +0GG: CGGG  tGGG:     ;GG0GGGGG0G*       ,GGGt               
            +GGGL********C0GG,    +GGG*        CGG0.    *GG0*,,,,:GGGG     .GGGL.0GGt  ,G0Gt,GGGt      ;G0G*......        ,GGGt               
            +GGG*        LGGG,    ,GGGG       :GGGC    ;G0GGG0GGGGG0GGC     +0G0GGGC    +GGGGG0C       ;0GG+              ,GGGt               
            +GGG+        fGGG,     *G0GGf:.,;CGGG0,   :GGGL;;;;;;;;*0GGL     LGG00G,     LGGGGG,       ;0GGt,,,,,:,::,    ,GGGt               
            +GGG+        fGGG,      ,GG0000GG0GGL    .0GGC          *G00t     0GGG*       0G0G+        ;GGGGGGGGG0GGGG.   ,G0Gt               
            .,,,.        ,,,:           .,:,,.       ....            ....      ...         ...          ..............     ...                
                                                                                                                                              
                                                                                                                                              
                                                                                                                                              

 

                       

原图:

 

<?php
/**
* 图片转换为字符图
* @param string $imagePath 图片地址
* @param int $width x轴方向缩放比例
* @param int $height y轴方向缩放比例
* @return string 生成的字符串图
*/
function img2String($imagePath,$width=1,$height=1)
{
    //检查是否安装 php_imagick 扩展
    if(!extension_loaded("imagick")){
        exit("请先安装 imagick 模块");
    }
    $imagick = new \Imagick(realpath($imagePath));
    //将生成的imagick对象转化为一个像素迭代器,可以从该迭代器中获取没个像素的 rgb 的值。不知道什么是rgb的请问度娘
    $imageIterator = new \ImagickPixelIterator($imagick);
     $str='O80GCLft*+;:,. ';
    foreach ($imageIterator as $rows => $pixels) {
            if($rows%$width == 1 || $width == 1){
                echo "\n";
                foreach ($pixels as $column => $pixel) {
                    if($column%$height == 1 || $height == 1){
                        //灰度计算公式  某个像素点的灰度值 = 红色值*0.3 + 绿色值 * 0.59 + 蓝色值 * 0.11
                        $gray = $pixel->getColor()['r']*0.3 + $pixel->getColor()['g']*0.59 + $pixel->getColor()['b']*0.11;
                        //由于 rgb 三原色的数值范围在 [0,255] 所以求出的灰度值的范围也是 [0,255],所以我们计算像素的 "饱和度" = 灰度值/255;
                        $r = $gray/255;
                        //根据像素的 "饱和度" 选择合适的字符
                        $offset=(int)ceil($r*(strlen($str)-1));    
                        if($offset==(strlen($str)-1)){
                            echo " ";
                        }else{
                            echo $str[$offset];  
                        }
                    }         
                }
                $imageIterator->syncIterator();
            }    
    }
}

img2String('huawei.jpg',2,1);

 GD实现效果(不需要安装imagick)

/**
* 图片转为字符串
* @param string $img_src 图片地址
* @param int $width_index 横向缩放比例
* @param int $height_index 纵向缩放比例
* @return string 生成的字符串
**/
function img2str($img_src,$width_index,$height_index){
    $resource = imagecreatefromjpeg($img_src);
    $width = imagesx($resource);
    $height = imagesy($resource);
    imagefilter($resource, IMG_FILTER_GRAYSCALE);

    for ($i=0; $i < $height; $i++) {
        if($i%$height_index==0){
            for ($j=0; $j < $width; $j++) {
                if($j%$width_index==0){
                    $color_index = imagecolorat($resource, $j, $i);
                    $rgb = imagecolorsforindex($resource,$color_index);
                    $gray = $rgb['red'];
                    $str='O80GCLft*+;:,. ';
                    $r = $gray/255;
                    $offset=(int)ceil($r*(strlen($str)-1));
                    echo $str[$offset];
                }
            }
            echo "\n";
        }
    }
}

img2str("./huawei.jpg",1,2);

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php知识点发布时间:2022-07-10
下一篇:
PHP常用必备函数发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap