convert datastring into the jpgimage - php

I am using sqlserver as backend server in that we have "image" datatype
image store as following manner
2nd comment of hex Oxffd is the jpg extension
*Above code is in **IMAGE DATA TYPE itsnot varbinary or binary.***
and we are trying to fecth this data via php and wants to display as image
but got following code as result of the select query "select mainpic from
mytable where id=1"
$url =http://rku.ac.in/library/webopac/photo.php
We are trying to generate image in php
<?php
$data=file_get_contents($url);
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/jpeg');
imagejpg($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}
?>

Related

Save Google Chart image to file

I'm using the Google Charts API to generate some charts and am using an ajax request to send that data to an endpoint that will save the image to disk for use in a PDF. However, I'm not sure how to get the image string into a PHP image resource.
It looks something like this (I'll snip a bunch of the code to save some room):
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAApIAAAEsCAYAAABwoFP+AAAgAElEQVR4XuzdCZcDuXXe/XGcfU/sLI41Glv5/l9JGmskZ99XJ7H9nl/bf71QqUgWyeKOOqdPd5NVKODiAvfBXX/rz//8z//8m3lNCkwKTArsTAFby//7f//v6+c//If/8M3f+Bt/45v//b//9zd/+qd/+s3f//t//5vf+q3f+nrjn/3Zn33d8z/+x//45vd+7/e+Pv+bf/Nv7tyb2dykwKTApMCkwC0o8FsTSN6CrLPNSYHPpsD//b//9TJwxYw7z0NGWIDRtXeSQyxAOi1trkg3pCSmx2MUn75CgHbRTcKMlTnqf+PAhEDRO8Xuk/SW/5KVcOxwmV4Q0Wmzt+1+bVH3Ytk1Fkly339GSMm9fVRMZLGgOma2jnYdtSkR61hBx//Ocffr0aVR0Ieuus1loE9HDP9H85OVcKX2WHDSfrl163ZJ71LmFQA8CRSR7UKpzCoEdEYjGgmZizicv3YjWKaUQk0YIcUxi86GGIxG5zHGtWdc9EdhEgLf5Ky2UCMmS4J8doTpF0zSE8bFEFGkG/S+pgwwC3ggbOTp3aXR1gEgg1VD7OFfGcez7JGgvWf9/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2igggi3wcmaV9HMijgBkJyQXcIJvv378vs/UVJ0iNuRAoBAqBJyJQRPKJ4NetC4GlCPzwww//vQSZRDJFX/sZM7eoblHaf/rTn5Y2X+cXAoVAIbAJAqpy9ea53eSG1cjTECgi+TTo68aFwHIE/vOf//zmX//61xtxTACOVqQF4j/p57t3795M4Xwn6ygECoFCoBAoBPZEoIjknuhW24XATggIuGHORiwRxt/97ndv+SHrKAQKgUKgECgEHolAEclHol33KgQ2RoBWkp8kc3YdhUAhUAgUAoXAoxH4P1ANKGa/fh2tAAAAAElFTkSuQmCC"}';
$decoded = json_decode($data, true);
$resource = imagecreatefromstring($decoded['image_data']);
var_dump($resource);
This throws a warning at the imagecreatefromstring step: Warning: imagecreatefromstring(): Data is not in a recognized format.
What's the right handler for that image format?
This is because your need to get rid of leading data:image/png;base64,
Also your base64 string looks corrupted . I did the following
<?php
$data = '{"image_data":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAADICAYAAABS39xVAAAQhUlEQVR4Xu2dTWxcVxWAz3kzEwKqiCtoG0eCWiCx6IK6Euwq6u5aqB0HCVGJnzoggWji1lmAxApbLGCBVAc7QnQBjlS1YlXHToVAIBwJEAsEk+5QF7ggOa666ARQ0mRm3kH3zZvxeDJjz3jsmTn2N1IWyby577zvnHy697777lPhAwEIQMAJAXUSJ2FCAAIQEIRFEUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEEBY1AAEIuCGAsNykikAhAAGERQ1AAAJuCCAsN6kiUAhAAGFRAxCAgBsCCMtNqggUAhBAWNQABCDghgDCcpMqAoUABBAWNQABCLghgLDcpIpAIQABhEUNQAACbgggLDepIlAIQABhUQMQgIAbAgjLTaoIFAIQQFjUAAQg4IYAwnKTKgKFAAQQFjUAAQi4IYCw3KSKQCEAAYRFDUAAAm4IICw3qSJQCEAAYVEDEICAGwIIy02qCBQCEEBY1AAEIOCGAMJykyoChQAEEBY1AAEIuCGAsNykikAhA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IICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAgiLGoAABNwQQFhuUkWgEIAAwqIGIAABNwQQlptUESgEIICwqAEIQMANAYTlJlUECgEIICxqAAIQcEMAYblJFYFCAAIIixqAAATcEEBYblJFoBCAAMKiBiAAATcEEJabVBEoBCCAsKgBCEDADQGE5SZVBAoBCCAsagACEHBDAGG5SRWBQgACCIsagAAE3BBAWG5SRaAQgADCogYgAAE3BBCWm1QRKAQggLCoAQhAwA0BhOUmVQQKAQggLGoAAhBwQwBhuUkVgUIAAv8H5S4HFJE4mRUAAAAASUVORK5CYII="}';
$decoded = json_decode($data, true);
$final = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $decoded['image_data']));
$im = imagecreatefromstring($final);
if ($im !== false) {
ob_clean();
header('Content-type: image/png');
imagesavealpha($im, true);
imagepng($im);
imagedestroy($im);
}
?>
working fiddle
http://phpfiddle.org/main/code/7u1t-yfbe

Signature input with jSignature to insert in Excel

I'm using jSignature to take on my web site the signature of a client in a form.
Then, when the submit button in a form is clicked, I get the signature in svg64 format and then I pass it with post to a php page.
Here all works, but now I have to convert the string that refers to the svg image into an image that I can put into an Excel file (also this automatically in php).
This is an example of the svg that I have to converter
image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIiB3aWR0aD0iMzc1IiBoZWlnaHQ9IjM4Ij48cGF0aCBmaWxsPSJub25lIiBzdHJva2U9IiMwMDAwMDAiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBkPSJNIDEgMSBjIDAuOTggMC4wMyAzNi43MyAwLjg4IDU2IDIgYyAxNi4yMiAwLjk1IDMwLjk2IDMuMDYgNDcgNCBjIDcuMTQgMC40MiAxMy45OSAtMC40IDIxIDAgYyA0LjcyIDAuMjcgOS4yMSAxLjcyIDE0IDIgYyAzMC4xNyAxLjc2IDU4LjQ3IDIuMDggODkgNCBjIDEzLjA5IDAuODIgMjQuOTIgMi45MSAzOCA0IGMgNy41NCAwLjYzIDE1LjI5IC0wLjA4IDIyIDEgYyAzIDAuNDggNS45MiAzLjAyIDkgNCBjIDQuMTggMS4zMyA4LjU3IDIuMzEgMTMgMyBjIDYuMzUgMC45OSAxMi42OCAwLjg0IDE5IDIgYyAxMC4xOCAxLjg3IDIwLjE5IDUuMDkgMzAgNyBjIDEuOTEgMC4zNyA0LjE1IC0wLjM3IDYgMCBsIDkgMyIvPjwvc3ZnPg==
How can I do? Is possibile to obtain an Image that could be automatically saved in an Excel file?
At least, I solved it using this
//Acquisizione dati dal form
$name = $_POST['name'];
echo "$name";
$signature = $_POST['signature_value'];
$data = str_replace('data:image/png;base64,', '' , $signature);
$data = base64_decode($data);
$output_file="$name.png";
$im = imagecreatefromstring($data);
if ($im !== false) {
imagealphablending($im, false);
imagesavealpha($im, true);
//header('Content-Type: image/png'); รจ superfluo, siccome l'immagine non viene mostrata qua
imagepng($im, $output_file );
//imagepng($im);
imagedestroy($im);
}
else {
echo 'An error occurred.';
}

Save image file in MySql

I am new to php & mysql I was trying to to save user image to the MySql datbase.
I have seen this solution on php.net
<?php
$data = file_get_contents('path/to/image.jpg');
$data = base64_encode($data);
//after reading data would be like this
$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.';
}
?>
it works but i am not getting how to store value of '$data' in MySql database. Should I take varchar datatype for my field and what would be the maximum limit of varchar??
Is this correct way or there are better ways then this if yes then what are they??
You could use text.
Then you don't need to define maximum length. since the base64 encoded string could be quite long i its a big image.
An other solution would be to store the image path instead.

PHP View Picture From Base64 String

I am connecting to Active-Directory and getting the thumbnailPhoto attribute successfully.
I have stored the file in the DB using Base64 encoding which makes the result look like:
/9j/4AAQSkZJRgABAQEAYABgAAD/4RHoRXhpZgAATU0AKgAAAAgABQEyAAIAAAAUAA ...
(Full Base64 encoded string: http://pastebin.com/zn2wDEmd)
Using a simple Base64 Decoder and decoding the string into a binary file and rename that to jpeg and open with an image viewer (here: Irfan View) I get the correct picture - see yourself:
How do I achieve this through PHP - I have tried using:
<?php
$data = '/9j/4A...'; //The entire base64 string - gives an error in dreamweaver
$data = base64_decode($data);
$fileTmp = imagecreatefromstring($data);
$newImage = imagecreatefromjpeg($fileTmp);
if (!$newImage) {
echo("<img src=".$newImage."/>");
}
?>
I'm just getting a blank page!
Your problem is that imagecreatefromstring() doesn't return a file, but rather an image in memory that should be output with the correct headers.
$data = base64_decode($data);
// Create image resource from your data string
$imgdata = imagecreatefromstring($data);
if ($imgdata) {
// Send JPEG headers
header("Content-type: image/jpeg");
// Output the image data
imagejpeg($imgdata);
// Clean up the resource
imagedestroy($imgdata);
exit();
}

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