欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP扩展之 Imagick安装

程序员文章站 2022-03-20 13:05:50
前两天研究php中的 imagick 扩展的时候,突发奇想实现的一个小功能感觉挺有意思,在这里记录一下: 将一张image图片转化为字符串的形式,先上一张效果图。(运行笔记中的代码需要先安装 php_imagick 扩展 教程可以看这里:PHP扩展之 Imagick安装) ,;+: ,+:. .fL ......

前两天研究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扩展之 Imagick安装

 

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

 

上一篇: 这是第一篇

下一篇: 小技巧