Moodle File API uploading an image - php

I am currently working on Moodle and i am trying to upload an image and display it.
I followed all the steps explained here : https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Simple_use
and it is working.
For example if i upload a file with some text in it and i access the URL given by the make_pluginfile_url function it will display the text that is inside the file.
The problem is when i upload an image it doesn't display the image but some text like this :
(+�ء��\U�k���*�j~�Uܽ�U���W\Uت�k��zb��S�
I suppose it's because the File API treats the image as a text file and not as an .jpg.
Could someone tell me how i could make it display the image ?

Display it with html_writer class. Example:
html_writer::empty_tag('img', array('src'=> $url));
Where $url is valid url to your file. (Prepared with moodle_url::make_pluginfile_url() or similar)
empty_tag docs

Related

How to upload,get and update an image or file in laravel ? What is the correct way?

What is the correct way to upload,update and get an image or file in laravel ? I had gone through laravel docs and other references but its confusing.Can anyone show a complete simple example for
any disk configuration in filesystems.php
a simple method to upload and update image in controller
and what will be the image full url?
input type file name is image you can store directly into images folder
return $path = $request->image->store('images');

Change image size to Save on PHP server

I have a form in my Android app that send information to php server with an image pick button. I want to resize image before saving on server with php codes :
<?php
move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'.$_FILES['file']
['name']);
$orgfile='uploads/'.$_FILES['file']['name'];
list($width,$height)=getimagesize($orgfile);
$newfile=imagecreatefromjpeg($orgfile);
$thumb='uploads/a/'.$_FILES['file']['name'];
$truecolor=imagecreatetruecolor(600,400);
imagecopyresampled($truecolor,$newfile,0,0,0,0,600,400,$width,$height);
imagejpeg($truecolor,$thumb,100);
unlink($orgfile);
?>
This code just resize jpeg images and another formats (png or gif and even jpg) saved a black image.
It is necessary to mention that name of image file changed to a random number like "32165465423" and I don't know the image format to use "imagecreatefrompng" or "imagecreatefromgif" in my php file.
I want a code like "imagecreatefromall" or another ...
Thanks guys(sorry for bad English)
You will have to detect the type of image, based on that you can run the function. See the one cool php library for reference
https://github.com/eventviva/php-image-resize/blob/master/lib/ImageResize.php#L77

Programmatically (PHP) save image with no extension

I am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.

Embed ppt file in webpage

I currently have a problem to open a .ppt/.pptx file inside a webpage. I currently have all "uploaded" file in a folder and am able to open .html/.txt files in that folder, but not .ppt/.pptx. Whenever I try, a new window pops up and Windows Uploader starts to run.
<?php
$target_dir = "C:\Apache\htdocs\upload\\";
$target_file = $_FILES['file']['name'];
?>
<iframe src = "upload/"<?php echo $target_file; ?>" name = "iframe_s" id = "download" style = "display:none"></iframe>
Click here to view files
In the above code, I try to list all files in the folder, and the goal is a user can click on one of the file, then the file will open inside the webpage. The main problem I have right now is to have the program each individually attach different .ppt url to different files.
Thank you in advance!
For your question "How to upload to Google Docs".
My first Answer:Why don't you search properly???
If you still don't find it. Then, (taken from here)
You'd use the Google Documents API to upload them using a simple HTTP POST with the data. Here's an explanation how to upload and convert documents: http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingDocs
The link will provide you will examples and all you need to do what you were asking for.
To be more precise this is what you are looking for if you want to upload pdf's: http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#ResumableUploadPUT
Python Google Documents API guide
PHP Google Documents API guide

Checking if the uploaded file is really a proper file or not (php)

Do the following steps to Understand the problem clearly :
Open notepad.
Type something and save it as " .png " (or any other image format).
Try to upload it as a image file with extension validation.
Now try to display it.
Expected : To show error while uploading
Actual : The file gets easily uploaded without any problem and error occurs only while accessing it.
mime_content_type() is deprecated function.
instead you can use
<?php
print_r(getimagesize("listing.png"));
?>
if this shows error then the file is invalid png file. If file is valid it returns an array of information.
Edit: This works if you are working with images only.

Categories