很多站长朋友们都不太清楚php转成byte,今天小编就来给大家整理php转成byte,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 求指教!,php如何把字符串转化为字节数组呢? 2、 php如何将图片转成字节流 3、 php 如何将图片转换成java中Byte[]的 求指教!,php如何把字符串转化为字节数组呢?将一个字符串分解成一个字符串数组,这种分割可能是基于某个字符,比如说是空格,逗号,分号之类的话,你可以用PHP的字符串分割函数 explode(),语法是PHP code?
1、array explode ( string separator, string string [, int limit])
函数的第一个参数是分割符,第二个就是字符串了,具体可以参考一下PHP手册
2、
<?php
function string2bytes($str){
$bytes=array();
for ($i=0; $i < strlen($str); $i++) {
$tmp=substr($str, $i,1);
$bytes[]=bin2hex($tmp);
}
return $bytes;
}
$b=string2bytes("昆山二手车ello,world");
var_dump($b);
php如何将图片转成字节流在php中,有文件上传,那么php服务器端可以接收到请求参数 $_FILES,在 $_FILES数组中读取到上传文件的form表单名,然后在读取的数组中可以获取上传文件的源文件,使用 fopen()函数就可以获取文件的字节流了。
具体来点代码看看吧:(假定上传文件的表单名为 “formname”)
if($_FILES isset($_FILES["formname"])){
//获取上传的文件的属性数组
$_fileinfo = $_FILES["formname"];
//获取上传文件的原文件名
$_filename = $_fileinfo["name"];
//获取上传文件的大小
$_filesize = $_fileinfo["size"];
//获取上传文件的临时文件名(长文件名)
$_filesource = $_fileinfo["tmp_name"];
//以读写方式打开文件,并将资源绑定到一个流上
$_filestream = fopen($_filesource,"ab");
//实现上传文件,其实质是把临时文件移动到制定的保存文件的位置
$_newfilename = "mypath/filename.exp"; //自定义文件名,包含路径,可以是相对路径
move_uploaded_file($_filesource, $_newfilename);
}
php 如何将图片转换成java中Byte[]的按照你的要求编写的Java程序如下:( 要注意的地方见语句后面的注释)
import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;public class ImageWithArray { public static void main(String[] args) { // 读取图片到BufferedImage BufferedImage bf = readImage("c:\\tmp\\6\\female.png");//这里写你要读取的绝对路径+文件名 // 将图片转换为二维数组 int[][] rgbArray1 = convertImageToArray(bf); // 输出图片到指定文件 writeImageFromArray("c:\\tmp\\2.png", "png", rgbArray1);//这里写你要输出的绝对路径+文件名 System.out.println("图片输出完毕!"); } public static BufferedImage readImage(String imageFile){ File file = new File(imageFile); BufferedImage bf = null; try { bf = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); } return bf; } public static int[][] convertImageToArray(BufferedImage bf) { // 获取图片宽度和高度 int width = bf.getWidth(); int height = bf.getHeight(); // 将图片sRGB数据写入一维数组 int[] data = new int[width*height]; bf.getRGB(0, 0, width, height, data, 0, width); // 将一维数组转换为为二维数组 int[][] rgbArray = new int[height][width]; for(int i = 0; i < height; i++) for(int j = 0; j < width; j++) rgbArray[i][j] = data[i*width + j]; return rgbArray; } public static void writeImageFromArray(String imageFile, String type, int[][] rgbArray){ // 获取数组宽度和高度 int width = rgbArray[0].length; int height = rgbArray.length; // 将二维数组转换为一维数组 int[] data = new int[width*height]; for(int i = 0; i < height; i++) for(int j = 0; j < width; j++) data[i*width + j] = rgbArray[i][j]; // 将数据写入BufferedImage BufferedImage bf = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); bf.setRGB(0, 0, width, height, data, 0, width); // 输出图片 try { File file= new File(imageFile); ImageIO.write((RenderedImage)bf, type, file); } catch (IOException e) { e.printStackTrace(); } }}
运行结果:
图片输出完毕!
原图:
输出图:
关于php转成byte的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php转成byte php转成vue的详细内容...