php 如何将image图片转化为字符串(GD库操作及imagick两种实现方式)
前两天研究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 LLLLLLLLLLLLLLLLL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LLLLLLLLLLLL: :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);
php 如何将image图片转化为字符串(GD库操作及imagick两种实现方式)的更多相关文章
- android带有文字的图片按钮的两种实现方式
android带有文字的图片按钮的两种实现方式 1). TextView对Button用相对布局,这要要求按钮的背景图片要留下空白位置给文字.这种方式开发比较简单,适合做一些风格一致的Button. ...
- js中Json字符串如何转成Json对象(4种转换方式)
js中Json字符串如何转成Json对象(4种转换方式) 一.总结 一句话总结:原生方法(就是浏览器默认支持的方法) 浏览器支持的转换方式(Firefox,chrome,opera,safari,ie ...
- C++ 删除字符串的两种实现方式
C++实现删除给定字符串的给定字符串思路主要有这么几种实现方式: 1.KMP算法2.用STL的string的 find,然后用erase3.用C的strstr找到字串位置,然后用strncpy写到新串 ...
- 求字符串长度 strlen(数组指针两种方式)
问题: 求字符串中所含有字符的个数(包括空格),即求字符串长度: #include <stdio.h> #include <assert.h> int _strlen(cons ...
- php和mysql两种不同方式的分割字符串和类型转换
一.sql语句1.分割字符串方法:substring_index(字符串,'分隔符',正数从左数起几位/负数从右数起几位); 例如:subtring_index('aa_bb_cc_dd','_',1 ...
- 前端canvas合并图片两种实现方式
---恢复内容开始--- 需求: 有一个固定的背景图,还有一个是随机生成的二维码,合并成一张图,让用户下载. 实现一:纯手写,这里为了省事生成图片我直接给的base64,其实使用qrcode.js生成 ...
- html5异步单图片多图片上传两种实现方式 后台.net mvc接收
Asp.net mvc上传多张图片后台存储 前台页面通过<file name="img">标签数组上传图片,后台根据Request.Files["img&qu ...
- C# 实现数字字符串左补齐0的两种方法
); MessageBox.Show(sss); return; 代码如上,自动补齐前面的0
- 将图片base64格式转换为file对象并读取(两种方式读取)
两种方式读取,一种URL.createObjectURL,另一种fileReader var base64 = ` data:image/jpeg;base64,/9j/4AAQSkZJRgABA ...
随机推荐
- [oracle]oracle表在什么情况下会被锁住(转载)
DML锁又可以分为,行锁.表锁.死锁 行锁:当事务执行数据库插入.更新.删除操作时,该事务自动获得操作表中操作行的排它锁. 表级锁:当事务获得行锁后,此事务也将自动获得该行的表锁(共享锁),以防止其它 ...
- Invoke-customs are only supported starting with Android O (--min-api 26) Message{kind=ERROR,……
https://www.jianshu.com/p/434928537a90 在我使用构建版本gradle 26但是在将buildtoolsversion更改为27之后,就像这个图像 错误:e ...
- Build Telemetry for Distributed Services之Open Telemetry来历
官网:https://opentelemetry.io/ github:https://github.com/open-telemetry/ Effective observability requi ...
- Spring分页实现PageImpl<T>类
Spring框架中PageImpl<T>类的源码如下: /* * Copyright 2008-2013 the original author or authors. * * Lice ...
- PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)
1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backw ...
- 操作TreeView(咏南工作室)
{*******************************************************}{ ...
- Spring Boot设置定时任务
在 http://start.spring.io/ 中新建一个Group为com.zifeiy,Artifact为task的工程. 然后在TaskApplication中添加注释:@EnableSch ...
- postman+jmeter接口实例
接口基础 一.为什么要单独测试接口? 1. 程序是分开开发的,前端还没有开发,后端已经开发完了,可以提前进入测试2. 接口直接返回的数据------越底层发现bug,修复成本是越低的3. 接口测试能模 ...
- python之selenium多窗口切换
前提: 在页面操作过程中有时候点击某个链接会弹出新的窗口,这就需要主机切换到新打开的窗口上.WebDriver提供了switch_to.window()方法,可以实现在不同的窗口之间切换. 内容: 以 ...
- Reactor系列(七)flatMap映射
#java##reactor##flatMap# 视频讲解: https://www.bilibili.com/video/av79582009/ FluxMonoTestCase.java pack ...