how to decode json with twig? - php

I'm creating a website where users can uploads some images, URL images are saved into json and inserted into database so when I select all of the rows in the table it gives me something this.
{
"sroxrafm72pgipmpa4nz":
"http:\/\/res.cloudinary.com\/dok4nvr2e\/image\/upload\/v1444610510\/sroxrafm72pgipmpa4nz.jpg"
},
{
"egrrhg4umqgyjoltxrky":
"http:\/\/res.cloudinary.com\/dok4nvr2e\/image\/upload\/v1444610511\/egrrhg4umqgyjoltxrky.jpg"
},
{
"rfurg40o5af2h6s55thb":
"http:\/\/res.cloudinary.com\/dok4nvr2e\/image\/upload\/v1444610511\/rfurg40o5af2h6s55thb.jpg"
}
I need to decode it with twig to get the url of each id to display the images, but I just can't figure out to do this. I hope you guys can help me.

I'd say this is a clone of: Decoding JSON in Twig
What might be easier is to base64 encode the images and store the string inside of the database. I use the following:
In HTML:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="image" />
</form>
upload.php:
function getImageBase64($image_path) {
$image = imagecreatefromfile($image_path);
ob_start();
imagepng($image);
$contents = ob_get_contents();
ob_end_clean();
$image_string = 'data:image/png;base64,' . base64_encode($contents);
imagedestroy($image);
return $image_string;
}
if (getimagesize($_FILES["image"]["tmp_name"]) !== false) {
$image = getImageBase64($_FILES["image"]["tmp_name"]);
//Add to database...
}

Related

Post multiple URL

I want to post multiple images by posting the url's but I don't know how to do this because I am new to PHP.
<?PHP
if(isset($_POST['post_image']))
{
$image_url=$_POST['image_path'];
$data = file_get_contents($image_url);
$new = '../images/myimage.jpg';
$upload =file_put_contents($new, $data);
if($upload) {
echo "<img src='../images/myimage.jpg'>";
}else{
echo "Please upload only image files";
}
}
?>
The problem with your script is, that you are not saving the file anywhere. So you have no access to it when trying to return it to the client.
In order to save an image using a POST you need to make sure you have set the following enctype inside your form.
<form action="upload.php" method="post" enctype="multipart/form-data">
Then you can use this function to save the img
move_uploaded_file($_FILES["myImg"], "/path/to/imgDir")
And later show it to the client using:
echo "<img src='/path/to/imgDir/myImg'>"
For more information about the topic i advice you to consult the following URL
https://www.w3schools.com/php/php_file_upload.asp

How to use "pdftotext" on multiple pdf files?

Can someone here can help me on my problem ? I have this code on displaying output of pdf file on web using pdftotext
include ( 'PdfToText-master/PdfToText.phpclass' ) ;
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pdf_file" multiple="multiple">
<input type="submit" name="SubmitButton"/>
</form>
if(isset($_POST['SubmitButton'])){
$pdf = new PdfToText ($_FILES['pdf_file']['tmp_name']) ;
text = $pdf -> Text;
echo $text;
}
it works fine, But what im trying is to upload multiple files and display it on web all the files uploaded. I can't find any solution. Can you help me guys ? :(
PS: sorry my english is not that good. THank youuu!
where you upload multiple file. you will get the files in array.
Therefore, you need to loop over the tmp files
function show_pdf_text($file){
$pdf = new PdfToText ($file) ;
$text = $pdf->Text;
echo $text;
}
if(isset($_POST['SubmitButton'])){
if(is_array($_FILES['pdf_file']['tmp_name'])){
foreach($_FILES['pdf_file']['tmp_name'] as $temp){
show_pdf_text($temp);
}
}else show_pdf_text($_FILES['pdf_file']['tmp_name']);
}

php base64 iphone picture to webservice

i need your help please and i haven't found some answers for my problem.
I want to take a picture (or take it from the gallery) on the iphone/ipad and then encode the picture to a base64 string with php and send the base64 string to a webservice.
If i try it on the pc, everything is alright.
if i want to do this on the ipad, it seems like nothing or a incorrect base64 string is send to the webservice but i have no idea why or so?!
picture.php
<form action="picture.php?action=upload" method="post" enctype="multipart/form-data">
<input type="file" name="datei" accept="capture=camcorder">
<br/><br/>
<input type="submit" value="up">
</form>
<?
if(isset($_GET['action']))
{
$tmp_name = $_FILES["datei"]["tmp_name"];
$name = $_FILES["datei"]["name"];
$name = substr($name,0,-4);
$name.="_".time().".jpg";
move_uploaded_file($tmp_name, "upload/".$name);
$content = file_get_contents ( "upload/".$name );
$imageBase = base64_encode( $content );
$success = savePicture($imageBase);
}
function savePicture()
function savePicture($pic_base64)
{
ini_set("soap.wsdl_cache_enabled", "0");
$pageURL = 'http://....';
$page = new NTLMSoapClient($pageURL);
$params = array("pRecordID" => "1",
"pFieldID" => 70000,
"pUserID" => "153",
"pContent" => $pic_base64
);
$result = $page->SetBLOBValue($params);
if($result->return_value != "ERROR")
{
return true;
}
else
{
return false;
}
}
Any Ideas why this didn't work on ipad/iphone?
i hope anybody could help me.
I think that the iPhone/iPad has some problems with capture=camcorder.
Please try this:
<input type="file" name="datei" accept="image/*;capture=camera">
instead of
<input type="file" name="datei" accept="capture=camcorder">
For more information check this:
http://www.html5rocks.com/de/tutorials/getusermedia/intro/
SOLVED
The Problem is that the SOAP Service had a Max-Size for the Files for 1MB!
That was the Problem why i can't upload Base64 Strings who are bigger than 1MB!
Now we increase the Max-Size for Files to 10MB and now it works!
And that was the reason why i cant upload some pictures because some pictures was bigger than 1mb in the base64 string.
Thx for help to everybody!

Displaying an image from an SQL BLOB with PHP

I have two files, one to upload an image and another to retrieve it from the database and present it on the web page. However the image only appears as broken image icon. I do not understand why.
Thanks for any help in advance.
The HTML form:
<form action="index.php" method="POST" enctype="multipart/form-data">
<label for="image">File:</label>
<input type="file" name="image">
<input type="submit" value="Upload">
</form>
The php to upload it(The connection to the DB is left out below but it works perfectly):
//file stuff
$file= $_FILES['image']['tmp_name'];
if(!isset($file))
echo "Please select an image";
else {
$image=addslashes(file_get_contents($_FILES['image']['tmp_name']));
$imageName=addslashes($_FILES['image']['name']);
$imageSize=getimagesize($_FILES['image']['tmp_name']);
if(!$imageSize)
echo "Thats not an image you mong";
else {
//upload
$query="INSERT INTO store VALUES('','$imageName','$image')";
$sendQuery=mysql_query($query);
if(!$sendQuery)
echo "This is embarressing. It didn't work";
else {
$lastid=mysql_insert_id();
echo "Image was uploaded. <br>Your image:";
echo "<img src=get.php?id=$lastid/>";
}
}
}
The PHP to retrieve the image(Again the DB connection is left out below but works perfectly):
$id=addslashes($_REQUEST(['id']));
$imageQuery="SELECT * FROM store WHERE id=$id;";
$sendImageQuery=mysql_query($imageQuery);
$image=mysql_fetch_assoc($sendImageQuery);
$image=$image['image'];
header("Content-type: image/jpeg");
echo $image;
Make sure the the opening php tag is the very first line of the file. If the opening php tag is not on the first line of the file then content has already been written to the response causing calls to the header() function to be ignored.
<?php // this has to be on line 1
// do database connections stuff, no output can be sent to the response.
$id=addslashes($_REQUEST(['id']));
$imageQuery="SELECT * FROM store WHERE id=$id;";
$sendImageQuery=mysql_query($imageQuery);
$image=mysql_fetch_assoc($sendImageQuery);
$image=$image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
Maybe one of the magic quote setting (magic_quotes_gpc), try:
$image=stripslashes($image['image']);
You should be using mysql_real_escape_string() instead of addslashes() and only if magic quotes are not enabled.

why isnt this php working, i get undefined index...?

I am trying to upload an image to the server.
I hava a form with a couple of inputs...
INSIDE this form, I have an Iframe which contains another form for the upload, this because I dont want the original form to submit().
The original form basically:
<form> ...... bla bla bla alot of inputs....<iframe src="pic_upload.html"></iframe></form>
Here is pic_upload.html:
</head>
<body><form name="pic_form" id="pic_form" action="bincgi/imageUpload.php" method="post" target="pic_target"><input name="pic_file" type="file" id="pic_file" size="35" onchange="this.form.submit();"></form><div id="pic_target" name="pic_target" style="width:80px; height:80px;"></div></body>
</html>
Here is imageUpload.php:
<?php
$destination_path = getcwd().DIRECTORY_SEPARATOR;
//echo $destination_path;
$result = 0;
$target_path = $destination_path . basename($_FILES['pic_file']['name']);
if(#move_uploaded_file($_FILES['pic_file']['tmp_name'], $target_path)) {
$result = 1;
}
?>
I get this error: Undefined index: pic_file on line 5 in imageUpload.php
ALSO, if I get this to work, is there a good way to display the uploaded image inside the target of the form?
Thanks alot!
PS: I will add javascript to the tags because maybe thats what I need here!
Don't forget about form attribute
enctype="multipart/form-data"
The form needs to be like this
<form enctype="multipart/form-data" action="__URL__" method="POST">
Check out this PHP.net documentation for more information.
You first need to test if $_FILES has an item with the key pic_file. So use the isset function to do so:
$destination_path = getcwd().DIRECTORY_SEPARATOR;
//echo $destination_path;
$result = 0;
if (isset($_FILES['pic_file'])) {
$target_path = $destination_path . basename($_FILES['pic_file']['name']);
if (#move_uploaded_file($_FILES['pic_file']['tmp_name'], $target_path)) {
$result = 1;
}
}

Categories