我有一个程序,可以将图像文件转换为二进制文件,还可以将二进制数据转换为图像文件。我已经做了第一个,我可以将图像文件转换为二进制文件。但第二个还没有完成。
如何将二进制数据转换并保存为图像文件
我正在PHP中检查这个。请帮助我
尝试imagecreatefromstring
方法,这里有相关文档。
您可以使用以下带有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.';
}
同样的方式,我们可以做JPEG,JPG和GIF格式的图像。
希望它能帮助到这里的人们!
您可以将二进制流(假设r/g/b值)转换回十进制表示法,并将像素写入使用imagesetpixel()创建的图像中。
将图像数据更改为二进制流的为数不多的原因之一是在隐写术期间以二进制位的较低顺序隐藏额外数据-在人眼无法辨别的水平上更改每个像素的颜色
检查http://www.php.net/manual/en/function.imagesetpixel.php