converting PDF to PNG with imagick and HTML input - html

I am trying to upload a Document (PDF) using HTML , and using imagick to convert it to a PNG if some one can look at my code and help me out I would greatly appreciate it, Thank You!!!
Also when I try to upload my document PDF thru I get a error message
Parse error: syntax error, unexpected '$image' (T_VARIABLE) in
C:\projects\magick2.php on line 27
Line 27 is associated with this code $image->setImageFormat('png')
<!DOCTYPE htlm>
<html>
<head>
<title>TEST!!!</title>
</head>
<div id = "cont">
<form method = "POST" enctype = "multipart/form-data" action = "magick2.php">
<div id = "choose">
File:
<input type="file" name="image" size = "40" id = "fimage" accept = "application/pdf">
<input type="submit" value="UpLoad" name = "wang">
</div>
</form>
</div>
<?php
$image = new Imagick();
$image->readImage(string ($_POST['wang'])
$image->setImageFormat('png');
$image->writeImage('C:\projects\matt.png');
$img = $image;
echo "<img src = matt.png >";
$img->clear();
$img->destroy();
?>
</html>

I am not going in to much details, but the actual reason for that error message is 'missing semi colon on the line above it'.
$image->readImage(string ($_POST['wang'])

you should receive "image" instead of "wang"
$image->readImage(string ($_POST['image'])

Related

Upload and retrieve image in Laravel

I need to save the image for an avatar.
Can anyone give me a simple code to save and retrieve image?
I need to:
Save image in folder
Save image name in DB
Finally retrieve on image tag; I have to do it by Query Builder
Form:
<form action="" method="post" role="form" multiple>
{{csrf_field()}}
<legend>Form Title</legend>
<div class="form-group">
<label for="">Your Image</label>
<input type="file" name="avatar">
</div>
<button type="submit" class="btn btn-primary">save</button>
back
</form>
<img name="youravatar" src="">
</div>
Route:
Route::get('pic','avatarController#picshow');
Route::post('pic','avatarController#pic');
Controller:
I have the avatarController, but it is empty because I don't know what to do.
Database:
Table name: avatar
Fields: name id, imgsrc, created_at, Updated_at
Other:
I found this code but I can't find out anything:
if ($request->hasFile('avatar')) {
$file = array('avatar' => Input::file('avatar'));
$destinationPath = '/'; // upload path
$extension = Input::file('avatar')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('avatar')->move($destinationPath, $fileName);
}
First, make sure you have the encrypt attribute in your form
<form action="#" method="post" enctype="multipart/form-data">
You can use something similar to this in your controller
public function save(Request $request)
{
$file = $request->file('file');
// rename your file
$name = $file->getClientOriginalName();
\Storage::disk('local')->put($name, \File::get($file));
return "file saved";
}
Yes, you should store the file route in your database as well.
Make sure you are using a consistent path for your images like
Finally you have to create a route to give public access to your image file, like so:
Route::get('images/{file}', function ($file) {
$public_path = public_path();
$url = $public_path . '/storage/' . $file;
// file exists ?
if (Storage::exists($archivo))
{
return response()->file($pathToFile);
}
//not found ?
abort(404);
});
Check the docs about Laravel Responses
I hope this gives you an Idea of what to do.
Upload Image in laravel 5.4
check if request has image
$request->hasFile('image')
OR
$request->file('image')->isValid()
Now Save Image
$request->inputname->store('folder-name') return image path 'folder name/created image name
$request->image->store('images')
Check if image exits
Storage::disk('local')->exists('image name');
Delete Image
Storage::delete('image');
This is my code
if ($request->hasFile('image') && $request->file('image')->isValid())
{
$path = $request->image->store('images');
if(!empty($path)){
$edit = Model::FindOrFail($id);
// Delete old image
$exists = Storage::disk('local')->exists($edit->image);
if($exists){
Storage::delete($edit->image);
}
$edit->image = $path;
$edit->save();
}
}
Reference

Make html text not transform into html

So I have made a guestbook (http://guestbook.ssdfkx.ch) which has a bug I can't get rid of myself. When you submit an entry and write in HTML text, it is converted into HTML and not plain text. This leads to the problem that one person can mess up the whole website in seconds.
I have tried it with the <plaintext> tag. But if I do so, even when I close the tag again, everything from the tag down turns into plain text.
Help is appreciated. The following is my code:
while ($row = mysqli_fetch_object($ergebnis)) {
$message = $row->message;
$name = $row->name;
$date = $row->date;
$id = $row->id;
$datetime = new DateTime($date);
$formatteddate = $datetime->format('d.m.Y');
$formattedmessage = nl2br($message);
if ($_SESSION['logged_in'] == true) {
$entryfeld = '<article>
<div>
<main>
<div class="innerdiv">
<p>'.$formattedmessage.'</p>
</div>
</main>
<div class="innerleft">
<form method="POST">
<input name="id" type="hidden" value="'. $id . '"/>
<input name="löschen" class="deletebutton" id="deletebutton" value="Löschen" type="submit"> </form>
<br/>
<p id="innerleftp">'.$name.'</p>
</div>
<div class="innerrightdel">
<p>'.$formatteddate.'</p>
</div>
</div>
</article>';
EDIT: Well, the variable $formattedmessage is what the user enters. If the user enters HTML it actually converts it which should not be happening. I tried using the <plaintext> tag before and after the variable. It somehow changed everything after the variable into plain text and not only the user input.

Why won't my comment system work? [HTML]

I am a beginner in html, and I am beginning to develop my first website. I watched a video to learn to code a commentary system, here is the link:
https://www.youtube.com/watch?v=O4BkHj7Ws9U
My page does resemble what is shown in the video, but there is some errors, take a look at my webpage after watching the video, as you can see, there are errors:
Here is my code:
<HTML>
<form action="" method="post">
<label> Name: <br><input type="text" name="name"><br></label>
<label> Message: <br><textarea cols="35" rows="5" name="mes"></textarea></label><br>
<input type="submit" name="post" value="Post">
</form>
</HTML>
<?php
$name = $_POST["name"];
$text = $_POST["mes"];
$post = $_POST["post"];
if ($post){
#WRITE DOWN COMMENTS#
$write = fopen("com.txt", "a+");
fwrite($write, "<u><b> $name</b></u><br>$text<br></br>");
fclose($write);
#DISPLAY COMMENTS#
$read = fopen("com.txt", "r+t");
echo "All comments:";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
else{
#DISPLAY COMMENTS#
$read = fopen("com.txt", "r+t");
echo "All comments:<br>";
while(!feof($read)){
echo fread($read, 1024);
}
fclose($read);
}
?>
Thank you for your help, sorry, I'm sort of a newbie at this.
You should use something like MAMP to run php code.
Or upload everything to a webserver.
You have to save your file as a PHP file.

Accessing data from Wikidata JSON file

I'm trying to access the following properties from the Wikidata API: id, url, aliases, description and label but so far have been unsuccessful. I'm sure I'm making basic mistakes and so far only have the following code. Any suggestions as to the best way to access this data is much appreciated.
<html>
<body>
<form method="post">
Search: <input type="text" name="q" value="Google"/>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$errors = libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("https://www.wikidata.org/w/api.php?
action=wbsearchentities&search=Google&format=json&language=en");
libxml_clear_errors();
libxml_use_internal_errors($errors);
}
?>
</body>
</html>
Edit - I have managed to get a string ($jsonArr) containing particular data that I would like. However I would like to get the first instance of the particular elements from the string specifically id, url, alias, description and label i.e. specifically: variable1 - Q95, variable2 - //www.wikidata.org/wiki/Q95, variable3 - Google.Inc, varialbe4 - American multinational Internet and technology corporation, variable5 - Google/ Please see code below:
<HTML>
<body>
<form method="post">
Search: <input type="text" name="q" value="Google"/>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$errors = libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("https://www.wikidata.org/w/api.php?
action=wbsearchentities&search=$search&format=json&language=en");
libxml_clear_errors();
libxml_use_internal_errors($errors);
var_dump($doc);
echo "<p>";
$jsonArr = $doc->documentElement->nodeValue;
$jsonArr = (string)$jsonArr;
echo $jsonArr;
}
?>
</body>
</HTML>
You need to show the JSON you want to parse...
Basicly you can get values out of JSON in PHP like this...
If $doc is the JSON you want to parse
$jsonArr = json_decode($doc, true);
$myValue = $jsonArr["keyYouWant"];
Edit after understanding what you are doing there :-)
Hi, Im sorry I was completely confused of what you were doing there... So I have testet your code on my server and just get what you want...
Following the code you wanted... I took clear var names, so they are a little bit longer, but easier to understand...
$searchString = urlencode($_POST['q']); //Encode your Searchstring for url
$resultJSONString = file_get_contents("https://www.wikidata.org/w/api.php?action=wbsearchentities&search=".$searchString."&format=json&language=en"); //Get your Data from wiki
$resultArrayWithHeader = json_decode($resultJSONString, true); //Make an associative Array from respondet JSON
$resultArrayClean = $resultArrayWithHeader["search"]; //Get the search Array and ignore the header part
for ($i = 0; $i < count($resultArrayClean); $i++) { //Loop through the search results
echo("<b>Entry: ".$i."</b>");
echo("<br>");
echo($resultArrayClean[$i]["id"]); //Search results value of key 'id' at position n
echo("<br>");
echo($resultArrayClean[$i]["url"]); //Search results value of key 'url' at position n
echo("<br>");
echo($resultArrayClean[$i]["aliases"]); //Search results value of key 'aliases' at position n
echo("<br>");
echo("<br>");
echo("<br>");
}

Testing Symfony 2 form that contains html data

Given that a page http://test.intra has the following form code:
<form>
<textarea id="description" name="description"><p>Hello</p></textarea>
<input type="submit" name="submit_save" id='submit_btn' value="Save">
</form>
description field is submitted on browser with value <p>Hello</p>
But using the following code:
$crawler = $client->request('GET', 'http://test.intra');
/* $client is a instance of \Symfony\Bundle\FrameworkBundle\Client ]
* $crawler is a instance of \Symfony\Component\DomCrawler\Crawler
*/
$domForm = $crawler->filter('form');
$domForm = $domForm->selectButton('submit_btn');
$this->client->submit($form);
description field is submited with value &lt;p&gt; &lt;/p&gt;
Should client [\Symfony\Bundle\FrameworkBundle\Client] decode form data before submit is called? Am I missing something?
Thanks
I'm not totally sure what you are asking, but I noticed your code wasn't correct.
$crawler = $client->request('GET', 'http://test.intra');
$domForm = $crawler->filter('form')->form();
$crawler = $this->client->submit($domForm);
Now you'll be able to check your asserts on $crawler.
$this->assertTrue($this->client->getResponse()->isSuccessful());