2012-01-16 178 views
0

我有一個程序可以將圖像文件轉換爲二進制文件,並將二進制數據轉換爲圖像文件。我已經完成了第一個,我可以將圖像文件轉換爲二進制文件。但第二個尚未完成。如何將二進制圖像數據轉換爲圖像文件並將其保存在文件夾中php

如何轉換和二進制數據保存到圖像文件

我在PHP。請檢查該幫我

+0

「二元」是什麼意思?你能更好地描述你的問題嗎? – Vyktor 2012-01-16 10:39:32

+0

我已使用file_get_contents並將其轉換爲字符串 – 2012-01-16 11:08:51

回答

3

嘗試imagecreatefromstring方法,它是記錄here

+0

「It Works!」 。還有一個問題我可以用相同的方式轉換視頻文件,我可以如何實現? – 2012-01-16 11:07:02

+0

@Rinto轉換視頻文件意味着什麼?字符串到視頻? – Dau 2012-01-16 11:27:33

+0

「謝謝」。與我用於圖像的方式相同(字符串到圖像)。我從其他非PHP應用程序獲取視頻作爲字符串數據。我需要將它作爲視頻文件保存在我的服務器中,然後在視頻播放器中進行流式傳輸。有任何想法嗎 ? – 2012-01-16 11:37:00

0

您會將您的二進制流(我假設爲r/g/b值)轉換回十進制符號,並使用imagesetpixel()將圖像寫入到您要創建的圖像中。

將圖像數據轉換爲二進制流的幾個原因之一是在隱寫期間隱藏二進制位較低位的附加數據 - 在每個像素的顏色不會在人眼可辨別的水平上改變顏色。

檢查http://www.php.net/manual/en/function.imagesetpixel.php

1

您可以在下面的代碼中使用與saving an imageresizing 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格式做。

希望它對這裏的人有幫助!

相關問題