[英]How to convert binary image data to image file and save it in folder in php
我有一個程序,可以將圖像文件轉換為二進制文件,也可以將二進制數據轉換為圖像文件。 我已經完成了第一件事,可以將圖像文件轉換為binary。 但是第二個還沒有完成。
如何將二進制數據轉換並保存到圖像文件
我正在用PHP檢查這個。請幫幫我
嘗試imagecreatefromstring
方法,該方法在此處記錄 。
您可以將以下代碼與saving an image
和resizing saved png image without losing its transparent/white effect
選項一起使用, 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()將像素寫入正在創建的圖像中。
將圖像數據更改為二進制流的幾個原因之一是,在隱寫術期間以二進制位的較低順序隱藏其他數據-以人眼無法識別的水平更改每個像素的顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.