如何使用 php 和文件上传将图像转换为二进制字符串?
How to convert image to binary string using php and file upload?
我正在尝试将图像转换为二进制数据,但未在 php 中获得解决方案。
请帮忙
试图实现这样的目标url。
https://www.dcode.fr/binary-image
1。上传图片并转换。
2.这会将我的图像转换为二进制 0 和 1 格式。
尝试在 php 中使用简单的上传表单做同样的事情。
任何帮助将不胜感激。
互联网上有很多 upload image tutorials,所以请您自己找出一个。
用于将图像转换为二进制。您基本上遍历图像并查看每个像素,然后根据其颜色输出 1 或 0。
function img_to_bin($file, $scale = 100, $fudge = 0) {
if (!is_int($scale)) {
throw new InvalidArgumentException('Scale argument invalid expecting int, got: '.gettype($scale));
}
$info = getimagesize($file);
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image_create = 'imagecreatefromjpeg';
break;
case 'image/png':
$image_create = 'imagecreatefrompng';
break;
case 'image/gif':
$image_create = 'imagecreatefromgif';
break;
default:
throw new InvalidArgumentException('Unsupported image type: '.$mime);
}
$return = null;
$img = $image_create($file);
$img = imagescale($img, $scale, $scale);
$width = imagesx($img);
$height = imagesy($img);
for ($y=0; $y < $height; $y++) {
$line = [];
for ($x=0; $x < $width; $x++) {
// get current pixel colour
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8 ) & 0xFF;
$b = $rgb & 0xFF;
$pixel = ($r + $g + $b)/3;
// value above 0(white) is 1
if ($pixel > $fudge) {
$line[] = "1";
} else {
$line[] = "0";
}
}
$return .= implode('', $line).PHP_EOL;
}
return $return;
}
echo img_to_bin("./face.png", 50).PHP_EOL;
所以对于像这样的图像:
将输出:
00000000000000000000000000000000000000000000000000
00000000000000000000011111111100000000000000000000
00000000000000000011111111111111100000000000000000
00000000000000011111111111111111111100000000000000
00000000000000111111111111111111111110000000000000
00000000000011111111111111111111111111100000000000
00000000000111111111111111111111111111110000000000
00000000001111111111111111111111111111111000000000
00000000011111111111111111111111111111111100000000
00000000111111111111111111111111111111111110000000
00000001111111111111111111111111111111111111000000
00000011111111111111111111111111111111111111100000
00000111111111111111111111111111111111111111110000
00001111111111111111111111101111111111111111111000
00001111111111100111111111000111111111111111111000
00001111111111000111111111000111111111111111111000
00011111111111000111111110000111111111111111111100
00011111111111000111111110001111111111111111111100
00011111111111101111111111011111111111111111111100
00011111111111111111111111111111111111111111111100
00011110011111111111111111111111111111011111111100
00011110011111111111111111111111111111001111111100
00001111101111111111111111111111111110011111111000
00001111110111111111111111111111111101111111111000
00000111111011111111111111111111111011111111110000
00000111111100111111111111111111110111111111110000
00000011111110011111111111111111001111111111100000
00000011111111100111111111111110111111111111100000
00000001111111111001111111111001111111111111000000
00000000111111111110000000001111111111111110000000
00000000011111111111111111111111111111111100000000
00000000001111111111111111111111111111111000000000
00000000000111111111111111111111111111110000000000
00000000000011111111111111111111111111100000000000
00000000000000111111111111111111111110000000000000
00000000000000011111111111111111111100000000000000
00000000000000000011111111111111100000000000000000
00000000000000000000001111111000000000000000000000
00000000000000000000000000000000000000000000000000
我正在尝试将图像转换为二进制数据,但未在 php 中获得解决方案。 请帮忙
试图实现这样的目标url。 https://www.dcode.fr/binary-image
1。上传图片并转换。 2.这会将我的图像转换为二进制 0 和 1 格式。 尝试在 php 中使用简单的上传表单做同样的事情。
任何帮助将不胜感激。
互联网上有很多 upload image tutorials,所以请您自己找出一个。
用于将图像转换为二进制。您基本上遍历图像并查看每个像素,然后根据其颜色输出 1 或 0。
function img_to_bin($file, $scale = 100, $fudge = 0) {
if (!is_int($scale)) {
throw new InvalidArgumentException('Scale argument invalid expecting int, got: '.gettype($scale));
}
$info = getimagesize($file);
$mime = $info['mime'];
switch ($mime) {
case 'image/jpeg':
$image_create = 'imagecreatefromjpeg';
break;
case 'image/png':
$image_create = 'imagecreatefrompng';
break;
case 'image/gif':
$image_create = 'imagecreatefromgif';
break;
default:
throw new InvalidArgumentException('Unsupported image type: '.$mime);
}
$return = null;
$img = $image_create($file);
$img = imagescale($img, $scale, $scale);
$width = imagesx($img);
$height = imagesy($img);
for ($y=0; $y < $height; $y++) {
$line = [];
for ($x=0; $x < $width; $x++) {
// get current pixel colour
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8 ) & 0xFF;
$b = $rgb & 0xFF;
$pixel = ($r + $g + $b)/3;
// value above 0(white) is 1
if ($pixel > $fudge) {
$line[] = "1";
} else {
$line[] = "0";
}
}
$return .= implode('', $line).PHP_EOL;
}
return $return;
}
echo img_to_bin("./face.png", 50).PHP_EOL;
所以对于像这样的图像:
将输出:
00000000000000000000000000000000000000000000000000
00000000000000000000011111111100000000000000000000
00000000000000000011111111111111100000000000000000
00000000000000011111111111111111111100000000000000
00000000000000111111111111111111111110000000000000
00000000000011111111111111111111111111100000000000
00000000000111111111111111111111111111110000000000
00000000001111111111111111111111111111111000000000
00000000011111111111111111111111111111111100000000
00000000111111111111111111111111111111111110000000
00000001111111111111111111111111111111111111000000
00000011111111111111111111111111111111111111100000
00000111111111111111111111111111111111111111110000
00001111111111111111111111101111111111111111111000
00001111111111100111111111000111111111111111111000
00001111111111000111111111000111111111111111111000
00011111111111000111111110000111111111111111111100
00011111111111000111111110001111111111111111111100
00011111111111101111111111011111111111111111111100
00011111111111111111111111111111111111111111111100
00011110011111111111111111111111111111011111111100
00011110011111111111111111111111111111001111111100
00001111101111111111111111111111111110011111111000
00001111110111111111111111111111111101111111111000
00000111111011111111111111111111111011111111110000
00000111111100111111111111111111110111111111110000
00000011111110011111111111111111001111111111100000
00000011111111100111111111111110111111111111100000
00000001111111111001111111111001111111111111000000
00000000111111111110000000001111111111111110000000
00000000011111111111111111111111111111111100000000
00000000001111111111111111111111111111111000000000
00000000000111111111111111111111111111110000000000
00000000000011111111111111111111111111100000000000
00000000000000111111111111111111111110000000000000
00000000000000011111111111111111111100000000000000
00000000000000000011111111111111100000000000000000
00000000000000000000001111111000000000000000000000
00000000000000000000000000000000000000000000000000