我有一個程序可以將圖像文件轉換爲二進制文件,並將二進制數據轉換爲圖像文件。我已經完成了第一個,我可以將圖像文件轉換爲二進制文件。但第二個尚未完成。如何將二進制圖像數據轉換爲圖像文件並將其保存在文件夾中php
如何轉換和二進制數據保存到圖像文件
我在PHP。請檢查該幫我
我有一個程序可以將圖像文件轉換爲二進制文件,並將二進制數據轉換爲圖像文件。我已經完成了第一個,我可以將圖像文件轉換爲二進制文件。但第二個尚未完成。如何將二進制圖像數據轉換爲圖像文件並將其保存在文件夾中php
如何轉換和二進制數據保存到圖像文件
我在PHP。請檢查該幫我
您會將您的二進制流(我假設爲r/g/b值)轉換回十進制符號,並使用imagesetpixel()將圖像寫入到您要創建的圖像中。
將圖像數據轉換爲二進制流的幾個原因之一是在隱寫期間隱藏二進制位較低位的附加數據 - 在每個像素的顏色不會在人眼可辨別的水平上改變顏色。
您可以在下面的代碼中使用與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格式做。
希望它對這裏的人有幫助!
「二元」是什麼意思?你能更好地描述你的問題嗎? – Vyktor 2012-01-16 10:39:32
我已使用file_get_contents並將其轉換爲字符串 – 2012-01-16 11:08:51