Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试JPG文件的PHP雕刻_Php - Fatal编程技术网

测试JPG文件的PHP雕刻

测试JPG文件的PHP雕刻,php,Php,我只是为了好玩而做了一个小PHP程序/页面。我的最终目标是允许我自己上传一个图像文件到我的mysql数据库。然后将该文件调用为ASCII格式的字符串,然后为我创建的JPG文件雕刻该文件 为了测试这一点,我使用了FTK Imager程序和我的web服务器,结果不太理想。假设我对PHP相当陌生,所以我知道可能有很多事情我不知道,我做错了。所以我希望得到一些提示:) 我的程序-使用FTK Imager创建和解压缩ad1图像文件。我创建了一个包含多个文件的目录映像。docx、.pdf、.jpeg等。然后

我只是为了好玩而做了一个小PHP程序/页面。我的最终目标是允许我自己上传一个图像文件到我的mysql数据库。然后将该文件调用为ASCII格式的字符串,然后为我创建的JPG文件雕刻该文件

为了测试这一点,我使用了FTK Imager程序和我的web服务器,结果不太理想。假设我对PHP相当陌生,所以我知道可能有很多事情我不知道,我做错了。所以我希望得到一些提示:)

我的程序-使用FTK Imager创建和解压缩ad1图像文件。我创建了一个包含多个文件的目录映像。docx、.pdf、.jpeg等。然后我将此图像文件与测试页面一起添加到我的Web服务器目录中。然后我通过浏览器窗口调用该页面

我受到一位单身汉的欢迎。这是我的输出,一个6。我尝试在执行代码的过程中回显一些错误,再加上一些错误报告,据我所知,$soff由于这样或那样的原因没有得到预期的值(没有确切的值)

为什么不能得到正确的值?事实上,我知道这些是正确的ASCII头/尾签名

这是我的脚本,包括回音和错误部分

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
// Search Criterium
$jpgs = "ÿØÿà";
echo "yoya Start<br />" . $jpgs;
$jpgeoff = "702";
echo "<br />" . $jpgeoff;
$jpge = "ÿÙ";
echo "<br />" . $jpge;
// Input file to string
$ipfile = file_get_contents('testimage.ad1');
// Input file length for math
$m1 = strlen($ipfile);
echo "<br />" . $m1;
// Check for empty file
if(isset($ipfile)) { } else { echo "ERROR - Empty File!<br />"; }
// Set starting offset with first criterium find
$soff = strpos($ipfile, $jpgs);
echo "<br />" . $soff;
// Do math to find where to start substr to cut first part off beginning of string.
$x1 = ($soff - 1);
echo "<br />" . $x1;
$x2 = ($m1 - $soff);
echo "<br />" . $x2;
// Execute the final math into cuts.
$ipfile = substr($ipfile, $x1, $x2);
// New input file length for more math.
$m2 = strlen($ipfile);
echo "<br />" . $m2;
// Set ending offset with jpgeoff to skip false positives for jpegs, then jpge to find real trailer character.
$eoff = strpos($ipfile, $jpge, $jpgeoff);
echo "<br />" . $eoff;
// Do math to find where to start substr to cut second part off end of string. Start at 0 for beginning of jpg, keep chars via math, cuts rest off.
$y1 = "0";
echo "<br />" . $y1;
$y2 = ($eoff + 1);
echo "<br />" . $y2;
// Execute the final math into cuts.
$ipfile = substr($ipfile, $y1, $y2);
// The $ipfile string should now contain the ASCII string of only the JPG file.
echo $ipfile;
?>
我从这里引用了我的文件签名信息,并通过在FTK Imager中查看我的jpg文件验证了这一点

我能想到的最好办法是,我应该用另一种方式告诉我的脚本在我的图像中查找标题/签名信息的偏移量

欢迎提供任何信息!这只是为了好玩和学习

谢谢:)

编辑-7/22

我一直在断断续续地编写代码。我做了一些编辑,以便在以十六进制格式查看JPEG文件时更好地反映其结构,这就是我将搜索的内容。我遇到一个错误,strpos似乎没有“看到”0?我的程序返回的结果几乎一目了然,标题和所有内容看起来都很棒,但是strpos或其他东西。。。。在jpeg文件的十六进制中的这些点处发现假阳性

当我需要FFD9紧挨着两个零的时候,它正在读取这两个零两边的FFD9

这是我的最新代码

<?php
ini_set('display_errors',1); 
error_reporting(E_ALL);
// HEX/STR Functions for converting string to hex and vice versa
function strhex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}

function hexstr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
// Search Criterium
$jpgs = "ffd8ff";
echo "yoya Start<br />" . $jpgs;
echo "<br />jpgoff " . $jpgeoff;
$jpge = "ffd9";
echo "<br />jpge " . $jpge;
// Input file to string
$ipfileg = file_get_contents('ti.ad1');
// Turn to hex
$ipfile = strhex($ipfileg);
// Input file length for math
$m1 = strlen($ipfile);
echo "<br />m1 " . $m1;
// Check for empty file
if(isset($ipfile)) { } else { echo "ERROR - Empty File!<br />"; }
// Set starting offset with first criterium find
$soff = strpos($ipfile, $jpgs);
echo "<br />soff " . $soff;
// Do math to find where to start substr to cut first part off beginning of string.
$x1 = $soff;
echo "<br />x1 " . $x1;
$x2 = ($m1 - $soff);
echo "<br />x2 " . $x2;
// Execute the final math into cuts.
$ipfile = substr($ipfile, $x1, $x2);
// New input file length for more math.
$m2 = strlen($ipfile);
echo "<br />m2 " . $m2;
// Set ending offset. My jpeg test files had three hits for FFD9, so I need to skip two.
$eoff1 = strpos($ipfile, $jpge);
$eoff2 = ($eoff1 + 1);
$eoff3 = strpos($ipfile, $jpge, $eoff2);
$eoff4 = ($eoff3 + 1);
$eoff = strpos($ipfile, $jpge, $eoff4);
echo "<br />eoff " . $eoff;
// Do math to find where to start substr to cut second part off end of string. Start at 0 for beginning of jpg, keep chars via math, cuts rest off.
$y1 = "0";
echo "<br />y1 " . $y1;
$y2 = ($eoff + 4);
echo "<br />y2 " . $y2;
// Execute the final math into cuts.
$ipfile = substr($ipfile, $y1, $y2);
// Convert hex to ASCII string.
$ipfile = hexstr($ipfile);
// The $ipfile string should now contain the ASCII string of only the JPG file.
echo "<br />final " . $ipfile;
// Create JPG file.
file_put_contents("test.jpg", $ipfile);
?>

问题当然不是strpos()函数的问题,下面是一个测试:

$needle = "ÿÙ";
$haystack1 = "afafjkaskÿ\0Ùasdf";
$haystack2 = "afafjkaskÿÙasdf";
var_dump( strpos( $haystack1, $needle ) );
var_dump( strpos( $haystack2, $needle ) );
结果:

bool(false)
int(9)

strpos()
在这里的工作与预期完全一样。

问题当然不是
strpos()函数的问题,这里有一个测试:

$needle = "ÿÙ";
$haystack1 = "afafjkaskÿ\0Ùasdf";
$haystack2 = "afafjkaskÿÙasdf";
var_dump( strpos( $haystack1, $needle ) );
var_dump( strpos( $haystack2, $needle ) );
结果:

bool(false)
int(9)

strpos()
在这里的工作与预期完全一样。

我不确定是否理解您的示例。在我最后编辑的脚本中,我的指针是“FFD9”,但是strpos在两个零的每一侧找到字符串“FFD9”,而它不应该在那里找到它,因为00是需要考虑的字符。我明白为什么strpos没有在一号干草堆中找到针,但在二号干草堆中找到了。然而,经过更多的思考,我开始怀疑strhex函数的作用是从我的get_file_内容字符串中删除零,从而使FF00D9变成FFD9,那么也许strhex函数会将零重新填充?我不确定我是否理解您的示例。在我最后编辑的脚本中,我的指针是“FFD9”,但是strpos在两个零的每一侧找到字符串“FFD9”,而它不应该在那里找到它,因为00是需要考虑的字符。我明白为什么strpos没有在一号干草堆中找到针,但在二号干草堆中找到了。然而,经过更多的思考,我开始怀疑strhex函数的作用是从我的get_file_内容字符串中删除零,以便FF00D9变成FFD9,那么strhex函数可能会将零补回?