php将字符串(流)转换为图像

时间:2016-01-06 05:09:55

标签: php css image-processing

我使用的是php 5.6。 并将Json数据发送到Delphi Datasnap Middleware。

这是数据样本

71,73,70,56,57,97,211,1,60,0,85,0,0,33,249,4,1,0,0,47,0,44,0,0,0,0,211,1,60,0,133,255,255,255,0,0,0,153,102,0,102,204,255,102,153,255,255,255,204,102,51,51,255,204,102,153,255,255,255,255,153,255,153,102,204,255,255,51,102,204,0,102,153,204,102,51,102,0,102,102,51,0,0,0,102,51,51,102,102,0,0,0,0,51,0,51,102,51,0,0,51,0,102,102,102,102,102,0,51,153,102,51,51,0,51,102,51,102,102,102,153,153,255,153,51,102,153,204,102,102,153,204,102,102,102,204,204,153,153,0,102,102,153,102,102,0,51,51,51,51,51,51,102,102,102,204,204,204,204,255,153,153,102,204,255,153,102,102,0,153,153,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,255,64,128,112,72,44,26,143,200,164,114,201,108,58,159,208,168,116,74,173,90,175,216,172,118,203,237,122,191,224,176,120,76,46,155,207,232,180,122,205,110,187,223,240,184,124,78,175,219,239,248,188,126,207,239,251,255,128,129,130,131,132,67,5,16,4,87,5,6,3,81,2,137,83,143,80,139,141,145,144,78,148,150,106,7,18,11,84,153,80,146,133,73,160,154,79,165,163,88,7,22,1,1,12,134,140,138,177,161,151,71,135,173,1,21,158,66,162,75,148,168,70,14,184,17,8,68,189,77,192,78,199,69,2,184,184,186,85,14,175,156,187,71,9,19,206,184,13,0,201,67,210,70,203,169,176,149,73,194,173,196,198,181,76,221,226,90,236,68,171,217,1,232,192,183,206,232,188,234,68,215,175,66,14,248,225,226,81,168,244,107,214,17,1,208,0,40,24,56,100,89,60,121,219,222,93,211,150,238,200,42,125,11,201,45,57,176,129,220,55,106,77,14,92,40,54,46,73,184,112,198,92,229,121,184,173,164,201,132,25,27,234,83,242,14,73,179,126,71,20,224,228,243,80,30,129,255,154,200,12,10,41,149,224,129,7,108,173,102,10,81,208,82,102,190,34,28,27,41,160,199,136,157,72,146,75,155,162,204,90,196,106,199,134,9,81,126,59,50,150,9,72,127,211,58,53,17,86,139,221,33,125,91,189,41,133,19,19,128,128,166,239,174,18,97,234,116,31,82,103,12,129,146,157,171,179,29,55,161,74,198,58,216,214,44,0,67,34,68,31,96,141,203,183,162,93,117,2,112,102,62,60,192,234,7,168,106,47,39,169,236,18,92,91,68,79,141,172,218,233,239,49,19,5,9,1,124,12,173,228,26,6,124,156,145,172,106,57,49,41,77,14,88,233,188,53,132,58,183,238,207,240,66,47,43,26,28,50,226,117,192,71,179,230,201,234,28,73,193,92,1,104,32,87,207,32,243,190,182,32,224,252,71,242,88,130,12,228,96,47,40,168,17,222,200,189,90,231,58,136,109,220,52,228,226,141,93,55,132,24,42,246,236,106,72,92,211,192,33,9,177,35,0,10,146,49,51,23,0,103,213,209,160,108,45,229,245,222,16,164,45,151,160,45,207,109,68,155,17,133,253,161,255,151,16,81,113,3,65,43,250,89,212,73,2,28,120,146,95,123,145,77,182,160,93,207,84,99,222,133,12,118,194,222,75,187,212,37,90,120,184,213,7,213,87,188,132,245,98,21,215,12,212,91,90,0,170,102,65,132,16,4,150,225,84,8,204,7,96,92,10,77,183,68,145,3,92,67,204,53,244,173,51,226,43,10,204,67,18,105,217,213,132,80,142,174,89,216,92,105,78,116,136,132,155,126,25,57,193,150,19,116,137,198,135,12,126,133,29,0,204,61,244,74,61,35,222,115,20,46,67,218,84,203,119,32,86,55,80,77,230,136,105,25,135,17,180,208,86,134,66,244,22,64,83,59,122,1,91,11,227,33,137,196,45,234,100,148,76,93,103,130,119,196,118,82,20,192,129,7,15,132,176,65,10,28,132,48,33,20,219,45,6,10,153,10,69,72,169,55,195,4,167,230,167,129,150,152,4,170,210,125,186,106,171,175,198,58,235,26,61,225,35,88,152,1,152,96,1,125,123,154,74,156,60,184,12,100,30,122,20,234,114,35,23,215,16,0,37,155,86,52,134,109,54,86,14,17,255,238,184,178,121,58,5,48,2,232,231,192,99,113,33,10,5,39,24,32,48,21,6,3,168,23,69,81,43,76,51,225,131,139,13,181,235,20,246,118,117,176,17,9,115,152,46,190,250,70,192,175,191,118,84,251,233,194,246,93,73,99,106,13,105,22,81,85,24,227,105,151,149,135,180,84,170,143,251,108,220,104,161,145,152,12,205,127,91,20,160,194,18,148,97,250,132,3,39,36,34,128,9,34,180,43,133,2,36,116,224,15,94,197,137,8,9,118,34,111,86,68,195,228,46,129,171,195,72,224,172,51,207,62,223,81,173,61,206,164,203,32,114,25,215,182,113,166,32,126,21,211,183,74,176,203,241,16,187,17,71,85,123,234,94,104,174,51,54,27,77,197,201,4,46,0,179,18,111,103,35,44,202,103,23,225,0,203,204,68,4,129,183,69,63,33,0,64,181,144,138,23,198,10,245,24,142,165,232,90,252,183,211,233,222,37,34,225,128,131,193,116,20,23,129,211,35,218,92,43,200,68,195,225,64,251,24,217,1,102,128,65,83,252,44,61,129,62,5,179,179,57,77,133,63,145,255,128,11,81,187,155,106,134,221,108,69,201,8,73,94,201,109,231,34,35,83,92,195,44,57,199,54,195,170,179,62,129,214,23,47,31,61,240,197,50,223,8,241,203,174,49,251,205,36,103,120,64,232,82,144,94,40,234,70,28,242,202,225,197,28,248,181,47,148,218,107,233,222,195,113,113,247,238,203,91,92,35,11,29,4,175,4,72,254,50,5,130,244,186,9,13,83,20,48,190,133,153,207,46,232,80,223,154,104,194,56,16,73,0,127,250,91,138,149,248,7,13,255,1,176,12,219,115,66,231,152,97,39,6,85,7,91,113,99,216,215,168,52,142,188,88,0,39,50,179,156,142,214,209,190,4,45,166,23,15,26,74,160,64,72,133,249,69,161,27,246,11,83,143,86,241,57,34,140,197,114,178,81,9,218,44,208,67,111,140,231,82,152,56,216,106,12,161,2,21,238,45,122,234,154,211,2,21,226,168,148,156,75,136,63,108,137,48,160,135,193,191,200,163,136,54,241,73,26,72,168,48,0,82,143,56,77,129,19,251,164,119,36,62,253,229,137,98,136,225,41,120,55,67,121,116,255,112,127,153,195,227,23,106,114,70,25,166,145,139,202,51,140,32,7,73,200,61,16,11,11,135,44,164,34,23,201,200,70,106,238,117,89,8,151,35,39,73,201,74,90,242,146,152,204,164,38,55,201,201,78,122,242,147,160,12,165,40,71,73,202,82,154,242,148,168,76,165,42,87,201,202,86,186,242,149,176,140,165,44,103,73,203,90,218,242,150,184,204,165,46,119,153,10,251,145,49,60,121,252,101,28,132,105,45,59,16,19,108,77,56,38,50,121,105,24,95,190,200,79,77,235,88,215,246,160,204,128,124,48,27,33,188,15,182,140,180,62,154,233,195,82,152,170,102,48,243,200,204,72,8,17,106,83,72,30,185,176,102,157,98,6,5,128,39,33,167,47,48,80,141,2,208,211,9,141,10,64,45,150,227,197,42,46,51,9,223,179,2,211,242,134,196,190,121,51,39,36,106,207,50,216,217,10,205,200,179,156,81,152,92,78,0,153,19,122,45,142,69,222,49,202,95,148,210,147,108,252,164,125,19,136,155,50,205,210,51,180,149,148,9,39,107,157,65,151,70,35,107,158,43,155,255,126,75,136,148,248,180,190,68,138,206,112,24,65,71,136,220,233,55,135,66,84,12,170,154,162,26,141,119,137,248,117,135,28,246,26,41,223,252,65,2,110,61,74,11,255,35,66,84,71,183,49,68,145,176,65,1,217,80,20,40,166,52,166,217,244,169,249,244,40,50,207,147,158,112,14,169,44,74,253,169,6,181,74,33,138,130,6,64,5,67,89,139,120,90,169,55,18,36,88,26,137,138,217,254,89,5,123,214,243,158,84,13,206,135,72,88,150,101,118,20,48,23,4,209,132,68,225,85,120,102,206,33,180,41,202,160,124,179,4,180,62,84,173,175,209,218,80,69,164,18,29,142,201,102,149,57,106,219,92,148,4,73,86,138,91,201,216,224,188,200,209,24,48,98,73,75,8,224,82,146,8,184,151,162,126,137,138,184,73,233,243,232,234,163,180,238,239,61,241,235,77,108,190,186,31,127,194,168,21,102,45,2,153,74,225,144,107,54,148,183,152,173,2,113,37,56,172,1,216,106,22,184,10,45,98,24,26,129,201,234,179,180,78,229,19,106,17,19,166,196,9,145,140,170,98,255,149,171,96,37,171,230,148,32,56,239,45,66,173,6,36,148,124,238,115,65,184,90,209,113,175,136,11,232,145,85,188,149,104,44,60,136,152,62,117,28,48,72,187,208,111,174,2,217,55,57,66,55,186,225,235,38,117,173,241,128,128,49,104,96,27,138,171,51,105,151,45,130,8,197,175,120,27,18,196,246,213,47,250,36,64,104,234,66,113,219,44,92,60,147,204,165,120,190,61,200,21,187,187,190,188,197,102,25,20,51,14,100,171,33,217,141,190,213,36,237,132,48,22,158,182,151,116,1,13,197,113,149,155,12,143,214,192,7,99,40,177,211,140,90,206,236,66,181,194,42,36,113,161,10,218,208,112,26,32,72,222,116,192,47,21,168,132,93,172,220,216,128,98,57,225,37,211,142,201,44,100,45,72,244,77,149,67,220,94,44,26,205,128,30,100,72,4,133,41,103,177,169,12,193,97,174,8,241,29,66,160,27,34,103,101,204,164,188,119,126,130,157,53,20,188,205,109,110,161,16,208,138,144,32,85,140,157,54,216,185,9,229,107,155,111,200,136,62,42,151,100,199,251,154,58,255,13,182,188,69,127,217,34,39,4,199,222,16,237,55,48,170,107,120,22,72,132,200,78,188,15,21,47,185,82,95,219,211,68,148,226,82,26,154,133,173,142,238,38,74,204,149,92,14,101,250,169,201,188,236,166,209,246,192,252,193,217,68,57,106,0,109,89,88,106,240,113,47,60,86,178,50,116,41,232,9,11,14,97,170,66,0,183,3,163,61,109,82,55,65,64,33,86,13,91,21,189,238,210,142,153,22,142,24,231,178,175,224,89,43,158,171,31,101,17,134,158,25,12,21,76,83,4,160,169,102,198,170,191,219,234,41,26,145,23,90,60,39,136,135,2,216,131,251,163,160,209,52,69,114,34,24,146,118,7,232,221,92,134,55,45,96,59,111,63,216,79,25,242,128,30,67,207,57,135,145,199,72,227,96,222,38,148,15,155,53,107,244,243,30,6,143,50,74,229,221,241,154,219,252,230,56,207,185,206,119,206,243,158,251,252,231,64,15,186,208,135,78,244,162,27,253,232,72,79,186,210,151,206,244,166,59,253,233,80,143,186,212,167,78,245,170,91,253,234,88,207,186,214,3,241,44,130,174,123,253,235,96,15,187,216,199,78,246,178,155,253,236,104,79,187,218,215,206,246,182,187,253,237,112,143,187,220,231,78,247,186,219,253,238,120,207,123,215,131,0,0,59

此数据是将流(或字符串)转换为blob图像(仅限gif)。

Attachment Image

所以,我为这些数据创建了gif, 如何将Image转换为此数据?

这是我的PHP代码获取json数据

$fullcurl = "http://addr";
$result = file_get_contents($fullcurl);
$im = imagecreate(200, 200);
$bg = imagecolorallocate($im, $result);
header("Content-type: image/gif");
imagepng($im);

此代码无效。 另一个尝试

$fullcurl = "http://addr";
$result = file_get_contents($fullcurl);
<img src="data:image/gif;base64,<?=base64_encode($result['Ask'])?>" /> 

不工作。

1 个答案:

答案 0 :(得分:0)

我认为您的数据样本不正确。

我将以下gif转换为base64,它的工作方式如下所示。

http://netdna.webdesignerdepot.com/uploads/2008/11/sample-graphic.gif

<?php
$image = 'R0lGODlhJgKQAdUAAElNS////wCZ/xt8u2/F/5mZmWtubDJlg8/s/8bHxyCm/xKG0pDT/0VRVkCz
/+/5/4GEg6/f/6+xsN3d3VRXVjxbbCVzpQmP6AWU9DCs/1ZfYXZ5d7q8u9HS0n/M/1C5/7/l/xeB
xyB4sEBWYRCf/9/y/zdgd+jp6PT09I2Pjp/Z/w6L3aSmpSlumS5pjnDG/19jYZTW/4DM/973/wAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAAHAP8ALAAAAAAmApABAAb/wIBw
SCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs/v+/+AgYKD
hIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8
vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T1
9vf4+fr7/P3+/wADChxIsKDBgwgTKlzIsKH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gURyg/aXg2xg
g9KHgz5YgxGYexYyhHkQg8A3g0h4ByrCfC/ShHwggu9CglJ4BxpCeh9yhX/AI493hFwICFSoK1YY
hnsQIONiIGZICA8whpHyApSyhoHwHsBCH3KYCOaRKutxh4zwHYoyHnzoCH7IJoAYiJDAHGAiHYZY
CSUgA8u3IAogA7u3iJaAAC/wiOmhAC/Qg5QoCanRfuEBG5zYiZegGZyBHqNRGqTICoghAy8gGaEh
GS8gA5axirZ4i7iYi7q4i7zYi/+++IvAGIzCOIzEWIzGeIzImIzKuIzM2IzO+IzQGI3SOI3UWI3W
eI3YmI3auI3c2I3e+I3gGI7iOI7kWI7meI7omI7quI7s2I7u+I7wGI/yOI/0WI/2eI/4mI/6uI/8
2I/++I8AGZACOZAEWZAGeZAImZAKuZAM2ZAO+ZAQGZESOZEUWZEWeZEYmZEauZEc2ZEe+ZEgGZIi
OZIkWZImeZIomZIquZIs2ZIu+ZIwGZMyOZM0WZM2eZM4mZM6uZM82ZM++ZNAGZRCOZREWZRGeZRI
mZRKuZRM2ZRO+ZRQGZVSOZVUWZVWeZVYmZVauZVc2ZVe+ZVgGZZiOZZkWZZmeZYGaJmWbBAEADs=';


echo '<img alt="sample.gif" src="data:image/gif;base64,' . $image .'" />';

或者如果您正在阅读文件。

<?php
$data = file_get_contents('sample.gif');
echo '<img alt="sample.gif" src="data:image/gif;base64,' . base64_encode($data) .'" />';