繁体   English   中英

如何将二进制图像数据转换为图像文件并将其保存在php的文件夹中

[英]How to convert binary image data to image file and save it in folder in php

我有一个程序,可以将图像文件转换为二进制文件,也可以将二进制数据转换为图像文件。 我已经完成了第一件事,可以将图像文件转换为binary。 但是第二个还没有完成。

如何将二进制数据转换并保存到图像文件

我正在用PHP检查这个。请帮帮我

尝试imagecreatefromstring方法,该方法在此处记录

您可以将以下代码与saving an imageresizing 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()将像素写入正在创建的图像中。

将图像数据更改为二进制流的几个原因之一是,在隐写术期间以二进制位的较低顺序隐藏其他数据-以人眼无法识别的水平更改每个像素的颜色。

检查http://www.php.net/manual/zh/function.imagesetpixel.php

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM