imagecreatefrompng() from base64 code? - php

is there a way to create an image with imagecreatefrompng() with this base 64 PNG code in PHP?

This works for me
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>

Related

Does imgjpeg function automatically take care of verification.

I am receiving multiple image files from android and I am using imgjpeg function. My question is do I have to verify that received file is an image file and is not corrupt or does this function take care of this verification (image + not corrupt) stuff.
Here is my code.
$data = file_get_contents("php://input");
$imageName = str_replace( array('[', ']', '"') ,"" ,$_SERVER['HTTP_IMAGE']);
if($imageName){
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/jpeg');
imagejpeg($im, $imageName, 100 );
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
}
else{
echo "Image name is not there";
}

Save byte to file with php

I have a c# program with some bitmap datatypes which i would like to upload to my web server with HTTP.
So i am thinking of converting the bitmap to the datatype bytes and then post it as text to the server, the sever would then save it as a image file.
How would i do this in PHP?
I am guessing something like this? but then how would i specify the save path location?
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
file_put_contents('img.png', base64_decode($data));

How can I show an image using a byte array created using C# in php?

I have a byte array and I got this from a C# developer, now I have to show a image using
this byte array:
I have tried with this example which is working fine:
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
But when I replace the value of $data with this array it's not showing any image. Also the format of array is very different.
Byte array: it's very long so I omitted some numbers in between.
[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,129,0,0,0,130,8,2,0,0,0,238,86,60,169,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0,0,4,103,65,77,65,0,0,177,143,11,252,97,5,0,0,0,9,112,72,89,115,0,0,14,195,0,0,14,195,1,199,111,168,100,0,0,163,175,73,68,65,84,120,94,156,189,133,95,155,105,187,239,187,255,129,189,222,153,169,226,238,174,193,221,221,221,221,165,208,82,74,105,105,169,64,139,123,72,32,64,32,78,92,112,135,226,110,117,119,119,135,115,61,161,157,119,214,218,231,236,181,62,39,243,235,61,119,158,164,246,251,222,151,61,72,255,87,245,197,139,109,245,13,216,234,218,150,154,186,214,218,122,76,77,61,22,81,3,182,166,14,91,93,131,169,174,105,70,84,139,169,174,7,53,87,213,98,170,234,64,141,101,85,213,231,74,170,207,94,108,186,84,217,82,81,219,82,94,219,82,81,211,92,94,217,88,122,185,254,66,73,237,185,11,53,69,197,213,167,139,170,79,21,213,156,62,93,123,246,116,195,249,51,232,146,98,76,105,113,75,233,249,214,203,66,149,159,199,85,156,235,40,63,215,89,113,161,163,162,164,163,226,82,87,69,37,177,172,134,80,94,217,89,121,25,95,81,214,89,94,217,85,94,221,85,89,211,89,85,211,81,85,141,175,170,236,170,169,38,212,84,147,170,170,136,85,85,164,154,26,98,109,77,87,109,117,103,77,101,87,117,5,136,80,243,111,117,86,151,225,171,74,241,85,37,160,142,202,139,237,21,23,64,109,229,231,91,46,157,197,150,156,105,40,62,89,127,166,160,163,242,114,55,166,129,209,218,196,211,167,79,255,87,83,101,21,185,173,157,208,210,210,133,109,233,196,96,65,176,233,194,180,34,123,44,236,177,157,205,24,124,115,51,190,169,17,184,117,52,212,119,212,215,181,215,214,180,213,80,123,67,54,135,244,130,109,164,99,155,152,173,104,38,164,114,108,19,164,111,54,100,112,88,155,235,89,200,217,71,146,15,0,96,99,26,121,232,166,62,12,102,186,157,192,44,186,124,209,51,44,15,229,144,103,96,95,100,238,113,193,198,191,194,49,172,209,43,30,27,144,74,141,63,49,123,1,243,140,52,240,99,104,105,119,98,237,21,111,116,30,215,53,208,140,238,195,160,123,91,49,2,28,134,47,4,176,39,40,227,127,35,17,116,180,252,34,209,218,200,109,169,231,128,176,181,28,108,29,136,213,92,195,104,170,234,174,47,39,84,149,180,151,67,46,170,134,92,52,220,77,24,134,172,2,43,100,36,97,34,250,133,129,9,0,246,178,255,191,25,252,173,97,54,121,128,67,238,103,147,123,89,68,38,17,75,108,169,37,182,212,80,219,26,24,8,0,8,130,6,38,190,158,1,43,2,0,195,34,52,179,73,88,54,169,5,86,97,53,198,50,9,136,216,164,86,14,69,24,7,85,197,5,237,53,165,173,149,23,218,161,141,171,42,33,212,94,38,55,84,144,234,202,201,245,229,148,134,10,106,83,21,181,177,154,138,48,0,235,235,104,77,13,180,166,186,110,72,223,66,79,225,104,119,183,52,64,21,21,170,137,222,186,199,0,34,160,1,250,31,54,8,83,207,106,134,191,127,3,68,3,228,31,38,186,142,131,110,234,105,198,12,183,224,48,201,199,210,212,108,195,15,105,134,237,87,14,253,83,62,228,47,185,136,67,138,49,162,170,81,34,170,97,135,148,35,68,212,146,101,13,79,161,92,91,2,146,250,143,93,92,175,106,189,77,160,175,146,41,99,237,184,190,22,140,160,165,89,0,109,146,48,8,4,237,45,72,131,212,209,202,71,54,88,1,244,69,29,136,120,173,77,252,214,6,16,15,26,48,108,61,187,185,22,196,66,87,211,27,43,9,213,208,10,159,167,161,171,6,169,29,195,116,136,0,68,35,96,61,228,31,33,0,232,142,16,6,76,97,222,103,254,146,208,250,127,99,24,224,16,251,56,132,62,118,23,147,212,76,192,85,17,90,171,40,237,181,221,157,245,244,174,6,6,161,81,40,96,208,204,36,98,192,122,14,177,149,3,24,200,16,13,205,44,68,24,120,9,185,78,17,214,100,244,165,115,80,66,137,13,149,208,201,16,235,171,200,141,72,122,161,52,213,81,192,116,200,51,104,164,204,66,27,3,237,13,100,30,120,103,55,114,240,133,117,21,210,31,152,142,28,246,102,86,43,150,137,109,134,13,187,5,195,194,162,89,88,8,2,40,161,77,44,108,35,11,146,79,75,3,114,42,49,141,172,166,122,110,19,122,10,79,108,59,90,24,163,104,24,117,88,51,236,144,106,208,33,57,255,67,50,126,135,36,65,129,135,164,2,14,201,250,28,146,119,219,47,227,240,135,132,205,191,196,236,254,148,240,16,83,136,215,54,170,247,13,219,108,192,62,233,29,88,163,49,230,186,200,19,248,174,190,54,92,111,11,102,160,189,173,175,163,163,175,163,173,7,223,186,215,152,246,224,113,61,29,208,209,98,32,107,129,184,45,104,14,146,18,225,143,1,25,169,138,214,88,65,172,189,220,81,121,142,138,46,31,160,182,14,211,219,71,152,29,35,204,206,33,228,176,147,32,203,35,250,157,247,255,41,72,77,66,33,65,0,26,100,19,251,217,248,126,118,39,88,73,194,213,145,133,234,198,131,245,104,58,17,234,45,226,62,132,8,3,81,11,244,63,72,230,33,227,88,100,28,36,37,68,16,22,20,28,155,42,204,69,45,229,23,193,205,189,204,14,214,67,81,165,128,208,194,186,218,220,36,84,35,148,89,228,37,52,180,149,112,222,33,243,252,147,1,244,251,208,53,162,153,224,59,22,233,91,88,0,3,11,0,16,6,108,72,74,240,151,111,133,38,29,26,155,70,22,186,177,191,165,165,175,186,33,199,220,41,88,92,53,88,68,217,247,160,172,247,33,41,175,67,18,158,7,197,61,64,7,196,221,15,74,184,30,146,114,62,44,99,115,80,210,108,159,152,209,31,135,76,254,58,100,125,72,44,88,74,169,206,37,224,17,154,188,211,63,255,121,108,249,205,212,202,163,209,185,117,70,239,48,142,208,143,195,15,118,224,251,59,218,250,58,91,123,58,91,122,59,91,123,59,97,128,104,233,105,197,242,91,176,92,108,179,80,104,8,74,38,186,134,6,71,173,174,108,143,65,63,165,101,136,142,27,102,180,15,51,241,131,12,164,17,218,51,250,255,180,30,217,11,55,123,0,96,122,24,100,17,251,89,93,3,108,34,151,210,70,105,107,32,181,214,81,218,234,24,132,38,38,0,32,160,89,144,130,16,6,112,222,65,45,108,98,43,0,64,76,167,224,0,6,32,1,65,34,226,208,240,8,131,214,138,18,38,174,113,175,101,4,139,193,122,240,26,36,180,30,77,69,131,154,105,24,52,168,27,138,45,182,25,146,254,175,228,211,218,200,108,109,100,253,173,150,6,54,34,72,65,123,199,31,217,252,82,43,52,236,192,9,65,50,128,193,162,147,50,83,149,245,99,165,212,2,68,229,125,14,74,121,238,23,119,219,39,234,242,151,136,211,126,81,199,3,98,14,7,196,108,247,139,89,31,20,55,63,36,102,116,80,204,224,128,168,246,95,251,117,247,29,176,20,145,136,146,211,164,251,166,252,36,79,238,206,222,221,93,127,186,123,245,213,151,213,167,119,7,22,166,240,221,163,29,164,1,192,208,213,222,219,133,235,237,130,140,212,44,104,199,244,182,97,5,45,88,62,116,83,32,12,116,86,77,156,230,6,122,83,13,185,14,90,163,11,52,116,249,32,13,0,116,12,209,219,135,32,14,88,72,230,17,186,76,217,243,253,159,130,139,32,24,208,132,43,50,190,13,177,73,253,240,83,216,36,62,13,79,109,111,68,234,65,123,29,147,216,200,34,53,177,72,104,16,147,136,240,0,18,32,152,206,56,148,22,22,136,12,130,152,64,24,112,41,237,92,90,23,194,0,87,89,250,55,3,26,6,250,119,0,32,196,208,220,72,193,52,81,32,14,176,104,90,75,51,173,165,9,70,39,161,208,140,150,70,96,192,104,133,56,104,100,9,59,113,102,107,45,179,165,22,25,71,161,27,105,69,146,15,248,206,65,218,118,164,121,231,2,21,28,154,137,107,130,97,106,24,139,173,244,13,45,84,55,201,145,213,138,21,87,140,60,44,23,124,64,202,235,47,81,151,125,162,246,7,196,173,14,136,155,239,23,53,253,75,212,104,159,168,254,126,17,237,3,34,154,7,68,84,247,29,84,249,235,160,218,193,131,22,34,18,25,202,70,179,39,106,118,167,111,239,110,189,220,185,254,126,247,246,183,221,27,31,158,77,174,207,144,56,35,120,242,64,87,87,31,161,189,151,208,218,211,133,17,116,64,205,64,196,199,33,25,137,215,10,127,140,70,54,22,66,161,150,90,95,209,85,93,210,221,92,57,72,107,27,101,117,142,176,186,134,89,4,72,50,123,7,95,232,50,140,196,136,132,214,195,74,19,174,123,0,246,94,162,12,115,40,131,66,1,3,24,202,200,184,122,90,71,3,194,128,216,200,33,162,255,41,46,169,153,71,198,112,169,88,54,21,203,162,96,217,20,136,137,54,136,6,14,5,207,165,10,25,180,86,92,4,55,247,166,39,33,9,104,225,33,26,234,169,152,6,10,182,145,138,109,160,182,212,83,91,26,168,88,88,97,148,133,34,12,201,7,48,32,209,192,64,210,81,61,11,87,199,194,213,2,9,14,228,28,28,8,220,111,18,14,77,208,38,54,243,97,197,161,217,109,205,172,54,52,15,215,60,138,110,174,246,8,184,160,102,92,36,173,117,92,66,53,71,66,53,85,92,37,228,144,172,235,1,73,235,253,18,70,251,69,13,246,137,232,255,37,162,243,215,97,173,191,14,171,255,117,88,117,159,136,226,159,135,20,255,18,81,58,120,72,253,240,126,107,49,169,147,214,158,55,48,244,221,153,219,187,171,207,119,215,223,238,110,190,129,152,120,49,190,113,179,255,202,2,139,63,216,213,57,208,213,62,64,104,237,131,194,208,142,230,183,33,179,2,114,211,162,173,129,141,171,99,180,212,48,160,164,65,241,171,41,165,161,247,24,116,141,176,73,144,91,192,223,33,97,4,236,121,61,202,165,237,121,61,194,1,0,221,123,24,144,27,21,200,77,11,218,40,224,225,80,135,56,221,67,28,154,160,155,64,235,104,38,227,26,104,29,141,76,66,19,155,132,152,14,250,27,0,136,79,193,242,104,45,28,26,150,3,43,181,149,67,133,164,212,198,163,225,121,221,194,123,21,109,213,165,172,54,136,131,223,19,44,114,171,0,2,2,24,212,35,190,35,170,163,182,252,18,173,21,138,1,180,67,255,133,1,34,14,242,87,69,115,133,238,255,102,128,220,57,224,9,73,112,218,48,236,86,76,47,174,165,175,178,178,220,209,189,68,205,176,92,65,175,66,197,232,180,188,110,170,132,106,224,97,89,135,125,98,168,63,14,104,255,177,95,243,207,131,32,237,191,14,106,237,63,164,185,255,144,214,1,81,205,131,18,26,7,37,180,15,138,106,29,60,160,125,248,144,189,152,84,177,141,215,90,121,251,206,240,246,238,216,237,157,161,27,63,199,111,239,206,222,255,190,120,247,26,119,108,186,147,126,165,147,52,217,142,31,105,195,245,227,176,189,56,8,133,70,65,123,3,191,189,142,211,6,193,10,12,170,105,245,21,192,128,218,84,49,64,193,141,48,9,195,44,226,240,111,235,65,127,71,192,222,102,148,219,61,194,65,24,192,10,251,95,12,128,16,183,123,152,75,7,193,136,208,221,129,37,181,54,64,85,160,119,34,201,135,67,110,129,46,72,152,133,154,217,100,68,92,96,208,13,12,192,253,61,33,12,184,212,14,110,119,231,111,6,72,46,2,6,181,194,104,64,154,125,136,6,90,11,4,65,19,8,54,144,136,104,173,141,221,173,141,200,10,147,23,194,0,205,64,58,81,97,174,199,129,132,167,94,216,164,243,218,97,130,21,14,174,200,200,218,204,109,7,0,205,48,214,114,161,135,193,181,80,10,11,139,205,172,202,180,12,235,180,141,235,81,54,5,170,250,193,135,164,109,254,117,192,232,207,3,186,251,14,26,138,137,155,202,200,88,43,41,56,107,168,185,104,130,212,157,212,213,29,84,212,173,228,148,45,37,20,76,68,100,244,69,37,12,15,30,178,253,235,112,161,169,243,253,70,250,46,107,241,11,101,230,39,111,115,119,232,230,238,196,237,143,35,91,79,184,211,247,25,67,15,232,3,183,89,125,11,36,250,72,107,219,96,11,166,191,21,221,135,107,228,67,140,194,148,128,174,238,70,24,92,250,205,96,175,233,252,5,224,111,235,133,238,239,133,2,48,160,11,25,208,199,120,140,127,99,224,117,15,243,105,67,92,26,48,96,224,91,201,173,104,74,91,19,3,38,47,225,45,57,232,253,89,164,22,22,210,143,34,18,50,192,113,187,219,184,52,220,30,3,14,181,141,215,221,193,235,22,214,228,182,42,136,131,166,223,173,14,18,7,136,144,155,104,232,238,22,44,162,86,12,29,135,97,224,144,21,81,107,51,3,186,160,214,102,16,164,23,118,27,26,44,230,130,239,208,173,195,196,212,214,34,20,108,96,98,66,110,100,242,59,224,58,134,11,155,86,44,156,77,246,217,226,139,214,118,37,26,250,101,186,198,5,26,122,161,135,37,236,254,181,207,252,192,33,107,89,89,87,109,237,32,115,243,24,71,187,68,55,199,20,79,231,52,111,151,44,31,247,52,119,215,120,7,251,104,43,219,16,19,91,111,109,51,91,5,53,99,25,25,35,49,177,96,121,141,145,220,146,55,88,193,253,74,250,167,142,169,221,158,107,59,61,219,187,35,8,137,239,195,91,95,251,86,190,141,172,127,154,88,127,210,127,101,133,196,24,107,109,27,104,193,244,225,48,130,86,52,7,83,71,111,168,36,215,93,134,92,52,64,109,31,101,65,34,250,237,59,108,216,123,183,72,105,99,60,196,107,184,56,204,65,34,0,17,183,251,111,6,99,60,250,152,128,62,34,232,30,230,117,247,179,40,204,46,28,181,173,153,130,67,24,128,251,144,238,145,158,135,2,25,31,4,253,79,11,151,218,202,235,110,231,209,58,184,180,118,0,0,36,64,124,122,59,159,33,100,128,171,46,99,182,99,32,203,195,209,102,34,119,210,145,122,11,12,232,200,149,38,184,242,183,88,184,102,228,13,184,38,58,174,145,1,204,219,155,152,29,104,22,190,153,45,20,183,163,153,143,199,8,240,173,130,54,28,168,183,189,77,40,92,95,91,11,244,39,252,78,28,176,153,234,232,28,191,92,83,101,239,85,164,102,148,41,175,233,125,80,220,230,207,191,28,165,196,253,117,53,98,109,204,51,189,220,243,130,131,79,70,69,20,70,133,23,68,134,21,68,134,194,230,100,68,216,177,192,128,163,1,1,25,158,190,73,78,158,193,166,86,174,154,58,142,178,138,225,202,186,130,244,162,219,23,218,166,146,207,223,191,216,249,147,62,187,203,91,219,237,189,186,59,126,127,119,254,217,207,197,167,223,70,174,255,232,93,223,157,188,249,237,202,181,251,253,147,83,4,170,0,185,1,14,125,90,3,163,169,154,84,95,70,69,87,13,211,240,19,44,210,24,139,140,220,142,230,208,70,217,212,49,14,109,76,120,198,127,49,224,209,134,17,81,135,184,148,33,46,21,246,112,101,132,79,31,225,49,144,85,208,61,34,96,0,3,70,87,11,173,173,137,134,171,135,33,153,75,194,240,200,173,124,74,43,143,2,157,15,0,104,133,1,2,54,2,26,174,135,134,19,208,218,120,84,28,143,10,1,209,46,160,119,242,24,123,125,17,194,0,11,217,134,14,12,224,128,227,32,195,64,213,69,250,78,176,27,121,42,20,28,121,208,63,246,205,236,246,102,118,7,134,211,129,225,226,177,136,58,48,188,14,12,31,223,202,239,104,227,183,35,18,192,220,212,14,48,90,145,169,181,163,101,176,163,99,174,141,48,94,92,137,247,143,63,165,106,226,247,135,152,221,126,17,47,101,165,72,11,227,20,55,219,163,1,30,69,49,17,165,233,169,101,57,217,165,217,89,37,233,233,37,169,169,151,83,211,42,211,50,170,83,210,107,83,51,106,226,83,42,35,226,207,250,6,29,177,182,139,83,215,57,162,105,52,148,82,112,175,184,105,37,163,228,214,89,236,103,252,192,14,123,113,135,191,241,179,255,250,206,212,163,221,181,55,187,171,175,127,78,222,251,220,191,253,117,248,234,206,204,173,119,147,107,139,20,86,47,182,133,135,69,211,27,171,9,181,101,148,166,202,145,238,142,73,22,121,2,249,128,193,47,6,227,28,218,184,16,192,158,70,249,112,210,169,35,124,240,29,54,200,138,248,14,43,191,123,136,71,27,226,211,134,249,244,126,22,21,26,77,90,91,35,21,87,199,236,108,16,22,225,22,96,240,111,12,80,30,128,1,181,181,151,214,218,219,221,222,211,221,193,135,128,160,118,252,191,48,128,228,243,139,129,80,12,28,154,222,214,204,104,195,128,232,184,95,27,16,19,135,97,227,90,216,109,45,156,246,86,16,108,184,237,56,110,7,142,215,209,202,199,99,193,107,216,32,194,227,224,236,243,58,97,211,202,107,71,62,14,51,219,73,89,106,234,98,167,20,48,194,178,234,45,124,142,104,152,68,104,235,103,56,56,156,8,240,45,10,15,46,141,143,169,205,72,111,202,201,197,30,47,192,28,61,217,146,115,2,159,91,72,59,86,196,58,118,134,149,93,200,201,44,232,201,44,232,203,56,65,137,76,110,118,15,168,182,114,110,182,113,219,60,90,252,189,158,240,173,137,246,173,133,251,147,56,184,195,152,217,229,174,252,20,108,254,24,188,177,59,253,112,119,237,245,238,246,135,157,165,151,223,71,110,255,132,28,53,115,239,243,212,181,21,2,175,175,9,7,227,58,185,174,18,226,96,136,218,62,193,36,1,131,49,142,48,2,0,0,167,123,140,11,121,255,23,3,112,95,120,246,25,35,60,250,30,131,81,1,29,132,32,129,98,32,160,15,11,24,3,108,26,155,212,70,109,111,162,64,127,216,217,196,37,97,247,0,252,23,6,124,42,196,65,91,15,194,0,207,239,198,67,83,212,195,232,226,51,133,125,17,174,166,156,37,100,0,2,223,17,247,33,14,112,141,204,223,0,254,139,88,8,3,12,167,13,187,39,110,251,47,211,249,200,205,50,33,3,48,29,1,208,198,239,2,193,190,173,15,223,181,66,231,222,99,244,173,148,227,88,81,199,122,99,242,153,254,25,13,142,33,231,237,61,171,3,34,154,99,18,219,18,83,200,233,217,236,163,39,5,39,138,135,11,47,79,22,84,78,21,84,76,228,148,14,37,157,238,135,247,135,100,12,135,101,77,39,228,47,166,23,78,197,229,244,5,196,179,92,131,121,238,161,55,142,156,254,90,217,178,219,202,216,237,234,217,33,14,236,80,199,119,232,51,59,172,197,93,193,198,206,224,246,143,233,7,59,27,111,119,183,62,237,46,189,217,157,121,186,51,253,120,119,241,217,155,145,171,211,109,116,94,67,19,173,182,138,218,136,220,47,130,92,52,1,41,72,24,7,123,24,144,143,157,253,250,192,217,94,46,130,204,195,20,98,96,140,242,33,5,1,6,132,196,136,128,62,36,96,12,241,233,125,44,10,139,216,70,198,53,146,90,106,25,120,200,69,88,62,249,87,28,128,239,123,12,96,229,211,218,64,130,238,118,1,173,29,70,10,46,173,3,24,8,24,68,33,131,218,203,108,60,156,116,200,242,77,244,54,168,7,66,1,15,168,189,66,211,233,191,221,135,112,97,180,97,217,72,129,133,86,231,87,41,22,158,125,12,20,3,30,82,123,133,48,16,247,219,123,186,218,5,248,182,94,124,199,4,149,126,119,100,250,199,252,246,107,234,208,114,97,221,68,114,241,250,209,234,137,216,34,146,83,76,135,125,48,193,35,130,22,16,219,23,157,54,157,118,98,245,216,133,205,130,178,173,227,229,219,57,101,155,217,151,87,83,47,44,39,156,93,137,63,189,16,149,183,24,121,108,41,38,111,38,226,200,136,95,114,175,107,36,207,33,164,215,53,98,49,58,231,225,137,210,111,181,157,187,29,220,93,66,207,46,101,100,135,60,254,147,52,185,67,155,254,193,156,251,209,187,241,115,242,193,238,242,43,200,75,59,171,175,127,44,191,216,89,124,177,187,240,252,62,231,202,80,125,43,179,170,150,86,143,212,131,73,14,101,18,57,254,96,189,48,14,160,228,34,69,24,97,128,228,34,96,192,167,15,243,153,67,60,198,16,132,2,178,135,116,68,31,237,97,64,4,236,49,232,231,80,89,164,118,50,174,137,136,173,133,17,129,11,0,160,30,8,75,194,30,131,189,122,128,48,128,74,0,0,232,72,28,112,41,29,252,110,2,159,254,155,1,212,85,72,103,8,128,223,103,159,137,195,178,218,176,96,58,8,162,132,249,155,1,187,163,5,169,1,237,104,68,80,132,59,177,252,46,56,248,80,18,48,130,78,156,160,179,29,1,208,217,214,211,133,239,197,227,135,240,196,101,150,224,245,236,198,238,234,131,157,129,213,123,229,132,197,204,75,11,41,23,174,29,173,94,74,58,207,178,141,238,54,13,224,57,71,142,6,38,47,68,101,47,132,103,94,9,74,157,8,76,27,242,73,236,245,136,235,113,143,23,184,197,243,221,226,122,61,227,251,188,226,250,189,98,7,125,144,149,227,24,214,109,229,207,176,9,30,243,79,221,72,57,117,59,247,226,235,243,77,95,235,73,223,176,140,157,14,193,46,97,120,183,115,8,180,67,24,217,165,94,129,152,248,217,183,249,243,202,157,157,229,39,59,107,47,118,150,158,237,46,60,249,50,178,189,214,198,224,85,214,51,235,106,70,105,157,83,191,24,116,143,33,162,77,112,187,247,234,193,56,31,201,72,72,28,32,181,151,57,204,103,236,9,246,80,135,1,3,194,160,135,57,220,195,26,226,51,97,220,165,118,52,147,90,235,41,237,13,28,164,24,180,241,201,109,176,242,96,8,248,45,126,55,184,223,14,253,232,30,3,30,173,171,135,78,20,48,72,123,12,202,186,33,151,65,87,10,173,78,91,51,210,134,34,142,131,245,24,164,234,238,169,3,246,72,249,69,132,135,21,251,171,20,119,182,64,182,17,16,112,124,2,78,64,104,19,192,217,239,2,12,237,125,157,157,195,120,210,82,183,224,221,220,213,221,245,39,63,135,182,95,99,123,183,142,214,205,199,157,93,74,44,222,74,47,93,75,44,30,114,75,226,217,132,245,186,70,247,121,68,246,185,133,245,58,5,241,28,3,57,206,33,12,199,64,186,99,32,219,37,140,227,26,197,113,137,98,59,71,112,92,194,120,110,161,189,94,17,35,1,241,67,1,241,124,15,184,30,121,37,60,231,254,241,242,135,39,42,64,47,206,161,95,148,180,188,41,195,127,111,160,239,54,115,118,154,217,63,177,156,157,54,193,110,231,192,14,121,236,39,123,238,71,255,250,207,113,40,18,247,118,175,220,221,29,191,241,152,54,60,92,141,229,212,214,140,81,240,87,56,84,96,48,193,165,79,242,153,16,4,176,249,155,1,8,169,201,200,217,255,5,224,55,3,68,224,254,96,47,107,168,135,61,200,103,114,168,93,148,118,52,177,181,142,218,222,200,33,66,16,180,241,72,56,62,9,97,192,163,182,35,162,32,141,233,47,0,244,78,126,55,136,208,199,34,247,48,201,8,131,214,154,75,140,118,52,13,215,136,124,54,71,91,243,222,217,71,78,189,176,243,1,175,247,86,112,124,143,1,187,3,162,161,149,213,209,194,233,196,113,187,218,120,132,182,189,21,242,207,30,3,65,87,71,95,7,254,10,161,251,241,224,236,207,249,219,187,227,55,95,96,251,111,20,182,110,164,150,109,165,150,110,167,151,220,200,190,116,53,249,252,184,79,26,219,38,136,101,31,200,116,240,103,216,251,176,156,124,153,46,1,76,143,16,150,123,48,211,217,159,233,24,200,114,10,97,57,134,48,29,131,89,142,65,28,231,32,182,91,16,203,61,136,235,19,193,241,138,100,186,133,11,188,227,174,132,103,47,199,229,173,38,228,95,207,62,127,39,191,226,81,81,195,155,82,220,183,106,226,247,90,242,247,58,242,247,6,234,78,51,99,23,199,223,197,15,238,16,71,127,80,166,126,242,22,119,135,183,118,71,174,126,226,205,207,55,19,121,53,117,227,164,206,105,14,117,74,104,253,4,159,57,193,99,76,240,16,235,199,5,32,6,108,144,154,44,196,48,218,195,28,21,176,132,238,195,202,6,33,12,32,8,122,216,67,2,22,143,78,132,56,0,6,180,142,38,14,184,15,142,3,6,16,108,168,237,72,28,80,219,249,48,145,209,241,136,144,32,232,20,208,137,80,72,122,152,20,132,1,182,170,132,41,252,44,26,88,133,57,7,203,236,64,242,15,116,159,112,5,74,5,27,143,101,227,91,216,120,196,119,16,27,233,130,218,120,248,118,46,30,73,59,2,66,7,114,246,187,32,255,116,64,46,18,16,218,249,4,252,0,129,184,193,232,249,50,182,182,59,176,241,21,63,250,236,34,241,81,81,235,147,211,152,251,39,106,174,101,93,188,153,93,178,145,84,52,236,17,199,179,246,29,112,11,27,244,137,26,244,143,18,248,132,177,60,130,186,221,252,169,174,1,4,39,95,156,173,7,198,198,173,201,218,165,206,194,177,198,220,177,218,220,177,210,204,169,210,212,185,214,194,173,193,194,173,205,214,151,238,30,198,241,140,232,241,142,26,240,141,157,10,207,88,76,205,223,206,191,240,240,98,253,251,154,142,239,77,228,157,70,234,151,26,226,151,90,210,143,90,234,78,35,99,23,203,221,197,9,126,18,250,119,88,19,187,3,107,59,131,27,215,58,185,130,170,166,9,34,105,150,195,156,130,179,15,101,0,41,185,172,49,62,120,205,24,237,5,129,239,221,195,2,42,52,163,80,129,199,4,204,177,30,214,104,15,107,100,47,255,32,214,35,36,70,16,18,12,46,147,72,237,196,146,218,26,201,237,141,200,205,56,106,7,34,74,59,27,68,133,161,172,157,75,195,11,186,193,119,2,82,3,186,9,130,110,34,159,65,238,101,83,5,44,234,239,154,220,213,66,239,64,35,159,222,132,76,194,72,73,128,142,147,13,24,132,0,56,157,112,228,91,225,212,3,6,16,7,105,67,219,129,1,52,60,252,189,254,103,175,6,116,182,35,34,116,244,118,118,76,18,169,247,57,195,223,120,243,239,219,6,95,86,116,191,171,162,191,186,68,124,120,26,125,253,200,165,141,212,179,107,41,69,51,81,71,33,165,80,29,252,216,94,225,116,239,48,188,171,127,147,141,91,165,169,237,57,93,227,124,53,221,28,21,237,12,69,141,116,37,141,52,21,205,52,85,157,84,53,157,100,144,186,94,166,150,81,150,166,81,142,38,234,164,158,249,5,99,219,114,51,199,42,11,167,90,11,231,22,7,95,178,87,24,39,40,118,36,38,115,62,45,127,227,232,249,231,151,176,31,234,201,175,171,9,175,203,59,223,85,18,62,213,80,190,53,179,191,180,112,190,116,8,118,24,83,187,67,27,207,89,99,125,85,232,9,2,113,134,11,12,152,227,60,6,50,3,3,0,62,83,104,55,99,180,135,62,210,211,13,36,198,122,25,200,72,204,71,94,69,82,144,48,11,33,12,122,0,0,71,24,13,12,46,139,72,37,180,82,58,154,201,109,77,116,2,150,69,134,241,184,29,132,220,17,218,19,36,34,26,228,31,98,47,147,220,195,32,9,232,164,255,196,160,173,174,156,67,104,101,224,225,212,67,237,197,194,73,71,234,48,210,254,183,8,39,47,88,91,17,117,252,45,28,15,223,193,239,236,64,24,192,244,251,15,6,189,208,14,117,182,195,44,182,76,160,191,102,141,126,232,232,127,122,137,242,170,130,254,182,150,249,176,24,183,149,83,190,152,88,52,29,157,63,24,154,69,117,139,170,183,112,63,173,103,82,160,107,124,92,207,56,67,67,63,73,85,39,81,77,39,69,203,32,203,200,60,199,202,54,223,209,245,148,187,215,25,191,128,75,225,81,229,49,241,151,163,99,75,34,99,138,67,194,78,120,122,103,217,216,71,107,235,5,201,41,5,73,203,135,201,42,70,43,170,167,168,235,230,105,27,149,24,90,53,152,59,183,217,122,117,56,248,50,3,226,22,143,93,220,62,93,179,85,88,253,160,4,251,162,162,243,117,13,249,125,3,237,83,19,243,75,171,96,151,117,229,147,224,202,68,125,203,120,23,225,10,7,242,15,115,82,192,158,16,48,39,144,252,131,164,32,88,199,32,26,224,248,247,48,198,123,128,10,11,193,179,87,6,4,172,225,30,246,112,47,103,79,131,61,236,129,30,38,143,129,196,1,165,3,67,129,166,70,120,187,226,119,41,134,68,132,228,34,164,36,208,160,18,32,12,122,153,36,1,131,44,96,82,254,201,160,130,67,192,49,240,45,8,3,232,124,240,112,222,219,88,194,249,11,48,252,45,112,31,186,254,127,50,128,218,187,231,254,94,25,128,8,64,90,210,206,142,177,78,194,45,42,239,85,103,207,139,42,234,187,10,250,231,58,238,227,18,194,205,130,166,153,196,51,44,239,228,86,187,160,203,198,46,199,85,77,98,100,213,194,21,84,142,90,218,150,6,133,158,11,12,61,27,20,90,30,151,220,148,125,180,253,196,169,174,51,103,73,231,46,144,47,150,144,47,93,102,86,213,176,106,235,25,213,181,212,138,74,252,197,11,205,133,39,107,114,114,78,133,134,198,91,90,250,105,104,120,40,40,184,203,202,187,75,201,248,72,72,71,203,43,29,213,208,187,128,178,170,181,114,65,219,121,50,2,226,199,146,242,123,34,51,135,227,143,109,22,148,223,187,128,126,114,169,245,101,101,199,219,90,210,135,22,214,15,238,216,118,7,245,74,39,97,154,195,156,224,179,38,248,108,168,201,147,124,168,204,72,73,64,246,189,236,201,30,0,195,18,230,40,4,195,8,18,7,172,97,200,63,66,247,135,132,26,0,6,2,22,159,69,238,238,106,161,182,99,104,144,192,59,161,19,69,76,255,141,1,217,195,96,44,232,238,2,6,72,34,98,16,5,204,61,6,180,95,12,240,141,85,92,66,27,163,3,203,64,170,49,100,252,54,22,30,7,12,96,250,229,180,227,144,49,248,223,238,35,27,56,254,220,142,118,97,73,0,225,246,220,71,68,236,224,146,160,30,116,76,147,105,247,41,252,123,213,196,103,151,9,95,26,216,47,203,40,183,10,49,87,146,138,219,29,194,139,52,173,210,20,245,194,165,85,3,101,85,163,117,13,79,251,5,118,228,23,112,202,171,152,149,213,221,229,213,204,234,6,118,109,19,187,182,145,89,93,7,166,51,170,234,144,43,13,104,86,61,154,94,211,64,169,168,106,191,112,177,165,168,168,185,176,176,254,216,209,203,169,201,167,66,67,142,120,186,39,219,216,68,24,24,250,170,169,122,200,203,248,202,201,196,169,168,157,208,51,174,177,114,37,123,134,15,70,166,243,130,19,168,94,225,220,224,196,153,140,147,215,78,151,221,47,169,127,124,185,249,73,69,219,39,2,247,5,93,176,74,162,205,114,88,83,16,4,8,0,230,148,128,1,12,166,64,2,214,84,15,123,170,135,59,37,224,142,243,217,123,12,198,122,216,163,61,156,209,94,238,104,31,119,164,143,55,220,199,5,240,81,52,244,182,246,7428,210,58,81..........................................................
......,165,213,59,105,130,20,86,192,235,41,38,213,118,131,216,5,179,86,31,105,245,160,224,218,103,35,178,15,193,33,151,223,104,117,27,198,91,85,2,123,18,52,26,32,133,210,168,214,91,181,38,59,40,141,209,166,212,91,192,151,30,95,88,78,228,195,24,167,53,90,181,128,151,140,22,189,205,255,125,252,23,29,96,240,255,248,199,63,254,15,240,1,160,248,239,227,191,228,0,131,255,167,63,253,233,255,2,28,223,108,55,211,50,174,162,0,0,0,0,73,69,78,68,174,66,96,130]
$imgData = your byte array
echo "<img src='data:image/png;base64, ".$imgData."' />";
Note : No need to do any base64_decode here...Directly pass the string without decoding it
Code :
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
echo "<img src='data:image/png;base64, ".$data."' />";
?>

Convert binary byte array to image in PHP

I have a web service in Java which returns the requested file contents in byte[] (byte array) form. Sometimes these files are HTML files and sometimes they're images. There is no restriction of which file type it is. I'm looking for a way to convert this array to a valid image/string. My working for text files is below:
$bytes = getArray();
$string = implode(array_map("chr", $bytes));
echo $string;
This is the output of the echo $string;
‰PNG IHDR‘h6 pHYsÄÄ•+ OiCCPPhotoshop ICC profilexÚSgTSé=÷ÞôBKˆ€”KoR RB‹€‘&*! Jˆ!¡ÙQÁEEÈ ˆŽŽ€ŒQ,Š Øä!¢Žƒ£ˆŠÊûá{£kÖ¼÷æÍþµ×>ç¬ó³ÏÀ–H3Q5€©BàƒÇÄÆáä.# $p³d!sý#ø~<<+"À¾xÓÀM›À0‡ÿêB™\€„Àt‘8K€#zŽB¦#F€˜&S `ËcbãP-`'æÓ€ø™{[”! ‘ eˆDh;¬ÏVŠEX0fKÄ9Ø-0IWfH°·Àβ0Qˆ…){`È##x„™FòW<ñ+®ç*x™²<¹$9E[-qWW.(ÎI+6aaš#.Ây™24àóÌ ‘àƒóýxήÎÎ6Ž¶_-ê¿ÿ"bbãþåÏ«p#át~Ñþ,/³€;€mþ¢%îh^ u÷‹f²#µ éÚWópø~<ß5°j>{‘-¨]cöK'XtÀâ÷ò»oÁÔ(€hƒáÏwÿï?ýG %€fI’q^D$.Tʳ?ÇD *°AôÁ,ÀÁÜÁü`6„B$ÄÂBB d€r`)¬‚B(†Í°*`/Ô#4ÀQh†“p.ÂU¸=púažÁ(¼ AÈa!ÚˆbŠX#Ž™…ø!ÁH‹$ ɈQ"K‘5H1RŠT UHò=r9‡\Fº‘;È2‚ü†¼G1”²Q=ÔµC¹¨7„F¢Ðdt1š ›Ðr´=Œ6¡çЫhÚ>CÇ0Àè3Äl0.ÆÃB±8, “c˱"¬«Æ°V¬»‰õcϱwEÀ 6wB aAHXLXNØH¨ $4Ú 7 „QÂ'"“¨K´&ºùÄb21‡XH,#Ö/{ˆCÄ7$‰C2'¹I±¤TÒÒFÒnR#é,©›4H#“ÉÚdk²9”, +È…ääÃä3ää!ò[ b#q¤øSâ(RÊjJåå4åe˜2AU£šRݨ¡T5ZB­¡¶R¯Q‡¨4uš9̓IK¥­¢•Óhh÷i¯ètºÝ•N—ÐWÒËéGè—èôw †ƒÇˆg(›gw¯˜L¦Ó‹ÇT071ë˜ç™™oUX*¶*|‘Ê •J•&•*/T©ª¦ªÞªUóUËT©^S}®FU3Sã© Ô–«UªPëSSg©;¨‡ªg¨oT?¤~Yý‰YÃLÃOC¤Q ±_ã¼Æ c³x,!k «†u5Ä&±ÍÙ|v*»˜ý»‹=ª©¡9C3J3W³Ró”f?ã˜qøœtN ç(§—ó~ŠÞï)â)¦4L¹1e\kª–—–X«H«Q«Gë½6®í§¦½E»YûAÇJ'\'GgÎçSÙSݧ §M=:õ®.ªk¥¡»Dw¿n§î˜ž¾^€žLo§Þy½çú}/ýTýmú§õGX³$ÛÎ<Å5qo</ÇÛñQC]Ã#C¥a•a—á„‘¹Ñ<£ÕFFŒiÆ\ã$ãmÆmÆ£&&!&KMêMîšRM¹¦)¦;L;LÇÍÌÍ¢ÍÖ™5›=1×2ç›ç›×›ß·`ZxZ,¶¨¶¸eI²äZ¦Yn…Z9Y¥XUZ]³F­­%Ö»­»§§¹N“N«žÖgðñ¶É¶©·°åØÛ®¶m¶}agbg·Å®Ã“}º}ý= ‡Ù«Z~s´r:V:ޚΜî?}Åô–é/gXÏÏØ3ã¶Ë)ÄiS›ÓGgg¹sƒóˆ‹‰K‚Ë.—>.›ÆÝȽäJtõq]ázÒõ›³›Âí¨Û¯î6îiî‡ÜŸÌ4Ÿ)žY3sÐÃÈCàQåÑ?Ÿ•0k߬~OCOgµç#/c/‘W­×°·¥wª÷aï>ö>rŸã>ã<7Þ2ÞY_Ì7À·È·ËOÃož_…ßC#ÿdÿzÿѧ€%g‰A[ûøz|!¿Ž?:Ûeö²ÙíAŒ ¹AA‚­‚åÁ­!hÈì­!÷ç˜Î‘Îi…P~èÖÐaæa‹Ã~'…‡…W†?ŽpˆXÑ1—5wÑÜCsßDúD–DÞ›g1O9¯-J5*>ª.j<Ú7º4º?Æ.fYÌÕXXIlK9.*®6nl¾ßüíó‡ââã{˜/È]py¡ÎÂô…§©.,:–#LˆN8”ðA*¨Œ%òw%Ž yÂÂg"/Ñ6шØC\*NòH*Mz’쑼5y$Å3¥,幄'©¼L LÝ›:žšv m2=:½1ƒ’‘qBª!M“¶gêgæfvˬe…²þÅn‹·/•Ék³¬Y- ¶B¦èTZ(×*²geWf¿Í‰Ê9–«ž+Íí̳ÊÛ7œïŸÿíÂá’¶¥†KW-X潬j9²‰Š®Û—Ø(Üxå‡oÊ¿™Ü”´©«Ä¹dÏfÒféæÞ-ž[–ª—æ—n ÙÚ´ ßV´íõöEÛ/—Í(Û»ƒ¶C¹£¿<¸¼e§ÉÎÍ;?T¤TôTúT6îÒݵa×ønÑî{¼ö4ìÕÛ[¼÷ý>ɾÛUUMÕfÕeûIû³÷?®‰ªéø–ûm]­NmqíÇÒý#¶×¹ÔÕÒ=TRÖ+ëGǾþïw- 6 UœÆâ#pDyäé÷ ß÷ :ÚvŒ{¬áÓvg/jBšòšF›Sšû[b[ºOÌ>ÑÖêÞzüGÛœ499â?rýéü§CÏdÏ&žþ¢þË®/~øÕë×Îјѡ—ò—“¿m|¥ýêÀë¯ÛÆÂƾÉx31^ôVûíÁwÜwï£ßOä| (ÿhù±õSЧû“““ÿ˜óüc3-Û cHRMz%€ƒùÿ€éu0ê`:˜o’_ÅFìIDATxÚÁIoUàyËŒ§3žÅN°/Ávš BÛiQ¥J=ô€¢^*nHpAøHÜøœzABÀ$$$TRDQ$$C’62‰—8Nâ‰ñÄÛxÞìoæñ} qðûÁ^Ã%¤T]ÆXl5O¿ÿqǸüo:J²ª)ÊÖÃ{Ÿ}úØš9öÌ~çÞ›èñö»\ˆ)atð²ùõ“Ÿ..NgF£B(§•ÆÑIä;•bŽã\Ô¥J)¿V)®ä–êõÃѳm‹±øÕB‡ÜÀ8ŸL®ž=¯Ó(,&–…çvE>ÇM’ÎIÏuü‚þÉ£íoßùcw÷×?ŸwÍQ»Ý1Œa±£1ÃÖtц>ä(cqÓ·>úà=”’²²tÿöíF·óÍoÏ\׉CŸ"Œ9 0f²`2ªZ-¬ü°³;š«ù…ÀΦ¶šÉ6»ƒ¬¦jXÂæpŒH‰Â5m]¿þBVÿíµùî[FKMK«Çýé|k£¦¨RL#<™â0E{£VãÓZVOï7FSëã÷·oÞ¸ùùW_Ôª¥‡÷ïò¼à{!øùÉ—–íÙsWH+¼ÏOϪÅ%âÞÜ*è‹Š¾Ð1z⢬æ5Ïá¤xàêºf†Oÿn•u­Z)ëZ˜Ñ:³ÑÑÑêu>;íÞÜñÜñXÂù>Ùk­îåúÝŒ9¾šM“±9JÙ Äñt"h®X­”Pry5kœÛ;- T+}bõGãõJe.ºÔóFn¸ ‹›¯—=xwÉ^s6&6Ç(#xAí•l»×{ZßgIÐ䯓þÍ·6’„
But I have no idea how to do this for image files. I could somehow write this array to a file and load the image but my project doesn't allow me to do that. I have to do this on the fly.
If all you want to do is show the image to the user, you simply need to serve the right content header with the response:
if (substr($string, 0, 4) == "\x89PNG") header('Content-Type: image/png');
else if (substr($string, 0, 2) == "\xFF\xD8") header('Content-Type: image/jpeg');
else if (substr($string, 0, 4) == "GIF8") header('Content-Type: image/gif');
echo $string;
An image is really just a blob of data (like the 'string' you have) plus an indication that it's an image (in HTTP, that would be the MIME header). Since you already have the blob, all you need is the header to make the browser interpret it as an image.
Try
$body = file_get_contents('php://input');
and also refer PHP - get image from byte array
You mean like in the examples shown here: http://us.php.net/manual/en/function.imagecreatefromstring.php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
Using below code we can create image from byte array code.
$userId = $userid;
$encoded_image = $imgurl;
$upload_path = $_SERVER["DOCUMENT_ROOT"]."/projectName/uploads/";
$LoginUserDetails = $this->getUserDetails($userId);
if ($LoginUserDetails->profile_image != "") {
$dbimgname = $LoginUserDetails->profile_image;
unlink($upload_path .$dbimgname);
unlink($upload_path . 'thumb_img/' . $dbimgname);
}
$decoded_image = base64_decode($encoded_image);
$imgname = md5(uniqid()) . ".png";
file_put_contents($upload_path . $imgname, $decoded_image);
$thumbnail = $upload_path . "thumb_img/" . $imgname; // Set the thumbnail name
$actual = $upload_path . $imgname; // Set the actual image name
// Get new sizes
$upload_image = $upload_path.$imgname;
// print_r($actual); print_r($thumbnail); print_r($upload_image); die;
list($width, $height) = getimagesize($upload_image);
$newwidth = 196; // This can be a set value or a percentage of original size ($width)
$newheight = 196; // This can be a set value or a percentage of original size ($height)
// Load the images
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefrompng($upload_image);
//print_r($upload_image); print_r($source); die;
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); // Resize the $thumb image.
imagepng($thumb, $thumbnail, 100); // Save the new file to the location specified by $thumbnail
return $imgname;

Unable to rotate image with PHP's imagerotate

I continue to get this error after trying to retrieve an image from my DB and rotate it:
Warning: imagecreatefromstring() [function.imagecreatefromstring]: Data is not in a recognized format
Here is what I'm calling to convert the blob data back into an image for rotation:
$SQL="SELECT * FROM images WHERE id={$id}";
$rh = mysql_query($SQL);
$image=mysql_result($rh,0,"image");
$source_image=imagecreatefromstring($image);
$rotate_image = imagerotate($source_image, 90, 0);
Am I missing a step?
This is what I ended up doing for it to work:
$SQL="SELECT * FROM images WHERE id={$id}";
$rs = mysql_query($SQL);
//rotate image
$image=mysql_result($rs,0,"image");
$source_image=imagecreatefromstring($image);
$rotate_image = imagerotate($source_image, 270, 0);
ob_start();
imagejpeg($rotate_image, null, 100);
$image_bin = mysql_real_escape_string(ob_get_contents()); data.
ob_end_clean();
I recommend to store image data in base64. No problem with storing character and binary.
Then it's easy as that :
<?php
$data = 'iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl'
. 'BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr'
. 'EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r'
. '8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==';
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>
Source : http://php.net/manual/fr/function.imagecreatefromstring.php

Categories