php 如何将二进制图像数据转换为图像文件并将其保存在php的文件夹中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8878781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert binary image data to image file and save it in folder in php
提问by Rinto George
I have a program that convert image file to binary and also it converts the binary data to image file . I have done the first, I could convert the image file to binary . But the second one not done yet.
我有一个程序可以将图像文件转换为二进制文件,并将二进制数据转换为图像文件。我做了第一个,我可以将图像文件转换为二进制文件。但是第二个还没有完成。
How to convert and save binary data to image file
如何将二进制数据转换并保存到图像文件
I am checking this in PHP .Please help me
我正在 PHP 中检查这个。请帮帮我
回答by Dau
回答by Aditya P Bhatt
You can use below code with option of saving an image
and resizing saved png image without losing its transparent/white effect
:
您可以在下面的代码中使用与选项saving an image
和resizing saved png image without losing its transparent/white effect
:
$data = 'binary data of image';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
// assign new width/height for resize purpose
$newwidth = $newheight = 50;
// Create a new image from the image stream in the string
$thumb = imagecreatetruecolor($newwidth, $newheight);
if ($im !== false) {
// Select the HTTP-Header for the selected filetype
#header('Content-Type: image/png'); // uncomment this code to display image in browser
// alter or save the image
$fileName = $_SERVER['DOCUMENT_ROOT'].'server location to store image/'.date('ymdhis').'.png'; // path to png image
imagealphablending($im, false); // setting alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
// Generate image and print it
$resp = imagepng($im, $fileName);
// resizing png file
imagealphablending($thumb, false); // setting alpha blending on
imagesavealpha($thumb, true); // save alphablending setting (important)
$source = imagecreatefrompng($fileName); // open image
imagealphablending($source, true); // setting alpha blending on
list($width, $height, $type, $attr) = getimagesize($fileName);
#echo '<br>' . $width . '-' . $height . '-' . $type . '-' . $attr . '<br>';
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$newFilename = $_SERVER['DOCUMENT_ROOT'].'server location to store image/resize_'.date('ymdhis').'.png';
$resp = imagepng($thumb,$newFilename);
// frees image from memory
imagedestroy($im);
imagedestroy($thumb);
}
else {
echo 'An error occurred.';
}
Same way, we can do for JPEG, JPG and GIF format of images.
同理,我们可以对JPEG、JPG和GIF格式的图片进行处理。
Hope it helps to people here!
希望对这里的人有所帮助!
回答by DevCoDesign
You would convert your binary stream (of i assume r/g/b values) back into decimal notation and write the pixel to the image you are creating using imagesetpixel().
您可以将您的二进制流(我假设 r/g/b 值)转换回十进制表示法,并将像素写入您使用 imagesetpixel() 创建的图像。
One of the few reasons to change your image data into a binary stream is to hide additional data in the lower order of binary bits during steganography - altering the color of each pixel at a level not discernable to the human eye.
将图像数据更改为二进制流的少数原因之一是在隐写术期间以二进制位的较低顺序隐藏其他数据 - 在人眼无法辨别的级别上改变每个像素的颜色。
Check http://www.php.net/manual/en/function.imagesetpixel.php