图像转数据流<->asp&php

来源:vbs小铺

asp图像转数据流:

<%Function ReadGif(sStr)
Dim i, iHex, sPath, oStream
sPath = Server.MapPath(sStr)
Set oStream = Server.CreateObject("Adodb.Stream")
oStream.Type = 1
oStream.Open
oStream.LoadFromFile sPath

For i = 1 To LenB(oStream.Read())
oStream.Position = 0
iHex = Hex(AscB(MidB(oStream.Read(), i, 1)))
If Len(iHex) = 1 Then iHex = "0" & iHex
ReadGif = ReadGif & iHex
Next

oStream.Close
Set oStream = Nothing
End Function
%>
<%=ReadGif("help.gif")%>

图像转数据流转ASP

<%

WriteGif("上边的函数转的字符串全部放进来就可以了")

Sub WriteGif(sStr)
Dim aAry
Response.Clear
aAry = MySplit(sStr)
Response.ContentType = "image/gif"
For i = 0 To UBound(aAry)
Response.BinaryWrite ChrB("&H" & aAry(i))
Next
End Sub

Function MySplit(sStr)
Dim sTmp

For i = 1 To Len(sStr) Step 2
sTmp = sTmp & Mid(sStr, i, 2) & ","
Next
If sTmp <> "" Then sTmp = Left(sTmp, Len(sTmp) – 1)

MySplit = Split(sTmp, ",")
End Function
%>

PHP图像转数据流:

<?
function mgReadFile($fileName)
{
if(is_readable($fileName))
{
$handle = fopen($fileName,'rb');
if(flock($handle,LOCK_SH))
{
$data = fread($handle,filesize($fileName));
fclose($handle);
return $data;
}
else
{
throw new MagikeException(E_FILEOPERATIONFAIL,'Unable To Unblock '.$fileName);
}
}
else
{
throw new MagikeException(E_FILEOPERATIONFAIL,'Unable To Read '.$fileName);
}
return NULL;
}
//$val='c:/1.jpg';
//$data=base64_encode(addslashes(fread(fopen($val, "r"), filesize($val))));
$data=base64_encode(mgReadFile('c:/1.jpg'));
echo $data
?> 
图像转数据流转PHP,调用.php?img=gril

<?php
function showImage($img)
{
$images = array(
'gril' => array(
'type' => 'gif',
'data' => '上边的函数转的字符串全部放进来就可以',
),
);

Header('Content-Type: image/'.$images[$img]['type']);
echo base64_decode($images[$img]['data']);
};
if ($_GET['img']){
showImage($_GET['img']);
}
?>

相关日志

发表评论