php 如何将image图片转化为字符串

2019-06-13 09:04:08来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

前两天研究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);

 


原文链接:https://www.cnblogs.com/itsuibi/p/10997258.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Yii2 框架跑脚本时内存泄漏问题分析

下一篇:【php设计模式】装饰器模式