Modus Operandi - Upload and Resize Multiple images using Zend Framework and HTML5 - zend-framework

The good news:
I don't care if it uses ajax or not.
I don't care if the user must install a specific browser to make it work.
I don't care if there isn't any specifc progress bar.
The bad news:
I don't want to use flash.
The user must upload a file from any width or height - however no bigger then 8MB.
The file must be stored on a specific folder (or database column).
A thumbnail must be generated on a specific folder (or database column).
Those images must be associated with a specific record.
This is a "modus operandi" question, I realise that there is to much code involved here.
So:
We first create our form element to support multiple upload, like this:
$element = new Zend_Form_Element_File('multifile');
$element->setAttrib('multiple', true);
$element->setIsArray(true);
We then, need to add some validations and allowed extensions;
Then we need to process the upload;
Once the upload is done, we need to resize those multiple files according to our needs.
We need to store those resized files somewhere else.
Once all this is done, we are ready to display the files associated with a specific database record?
Is this the way to go? Should I have more steps? Am I missing something. I've never done this before, but I'm taking it like a challenge.

First, you create a form. Not much complication here:
$this->addElement('File','Filedata');
$this->Filedata
->setLabel('Select images')
->setDestination('somepath') //define & create it somewhere earlier ;)
->addValidator('Size', false, 1024000)
->addValidator('Extension', false, 'jpg,png,gif')
->setMultiFile(5)
->addValidator('Count', false, array('min'=>0,'max' => 5))
;
In the controller, you receive the images. They will have temporary random names, which you can keep later if you wish (I usually do).
$fileinfo = $form->Filedata->getFileInfo();
$path = 'somepath'; //make sure it exists
for($i=0;$i<5;$i++) {
if($fileinfo['Filedata_'.$i.'_']['tmp_name']) {
$img = new Imagick($fileinfo['Filedata_'.$i.'_']['tmp_name']);
// image processing goes here
file_put_contents('somepath',$img);
}
}
And that's it ;)

Related

TYPO3 - Extbase - Detect missing files for a given FileReference

I've tried three different ways to detect if a FileReference's original file is still existing (i.e. file has been deleted outside TYPO3 using SFTP or similar):
if($fileReference instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference) {
$isMissing = $fileReference->getOriginalResource()->getStorage()->getFile($fileReference->getOriginalResource()->getIdentifier())->isMissing();
$isMissing = $fileReference->getOriginalResource()->getOriginalFile()->isMissing();
$isMissing = $fileReference->getOriginalResource()->isMissing();
}
Only the first one give me the right isMissing() value.
The property isMissing is an database value, which is set if the storage detect an missing file. On getFile the storage check if the file is missing and set "isMissing" for the file. If you dont persist this to the database, the setting is get loose with the next call.
You can also call $isMissing = $fileReference->getOriginalResource()->getStorage()->hasFile($fileReference->getOriginalResource()->getIdentifier());
You can run the file indexer scheduler (TYPO3\CMS\Scheduler\Task\FileStorageIndexingTask) if you want to check frequently for deleted files. This should be required if you let change files externaly (like ftp).

The best way to save game data in Unity that's secure & platfrom independent

I am looking for a way to save the users progress for my game, I am looking for something that can't be tampered with/modified. I would like to have to be able to be platform independent. And I would like it to be stored within the game files to the user can transfer their data to different computers (with a flash drive or something). (Correct me if I'm wrong in any area) But I believe because I need to to be secure and platform independent that removes player prefs from the equation. I know there is a way to save data by encrypting it with Binary, but I don't know how that works and if the user could transfer the data from computer to computer. Is there a better way of saving data? Is encrypting is through Binary the way to go? If so how would I do it? Thank you :) If you have any questions feel free to ask.
Edit: I understand nothing is completely secure, I'm looking for something that can stop the average user from going into a file, changing a float, and having tons and tons of money in game.
The previous answer mentiones two good methods for storing data (although there are still some quirks regarding writing files on different platforms), I'd like to add on to the subject of security, as mentioned in a comment here.
First of all, nothing is fully secure, there is always someone brighter out there that will find a flaw somewhere in your code, maybe unless you want full on crypto, which is not trivial with key management etc.
I understand from the question that he wants to prevent users from moving files between machines, or allow them to move the files between machines but seal them so that users cannot easily change data stored in them.
In either case, a trivial solution would work: generate a hashcode from your dataset, mangle with it a little bit (salt it or do whatever to jump to another hashcode). so you could have something like
{
"name"="john",
"score"="1234",
"protection"="043DA33C"
}
if the 'protection' field is a hashcode of "john1234", it will not match "john9999", hence if the user doesn't know how you salt your protection, you will be able to tell that the save has been tampered with
The first way to save data in unity is use PlayerPrefs, using for example:
PlayerPrefs.SetString("key","value");
PlayerPrefs.SetFloat("key",0.0f);
PlayerPrefs.SetInt("key",0);
PlayerPrefs.Save();
and for get, you only need
PlayerPrefs.GetString("key","default");
The second way and the way that permit you stored in a independent file is use serialization, my prefer way is a use the json file for it.
1) make a class that will store the data (not necessarily it need extends of monobehaviour:
[System.Serializable]
public class DataStorer {
public data1:String = "default value";
public data2:Int = 4;
public data3:bool = true;
....
}
and store it in another class with
DataStorer dataStorer = new DataStorer();
.... // some change in his data
string json = JsonUtility.ToJson(this, true);//true for you can read the file
path = Path.Combine(Application.persistantDataPath, "saved files", "data.json");
File.WriteAllText(path, json);
and for read the data
string json= File.ReadAllText(path);
DataStorer dataStorer = new DataStorer();
JsonUtility.FromJsonOverwrite(json, dataStorer);
and now you dateStorer is loaded with the data in your json file.
I found a link of data encryption tool, which is very helpful according to your need, as you want to secure data on device (nothing 100% secure), there are 3 mode to secure data, APP, Device and Encryption key, you can choose as per your need.
See this link, it may help you.
https://forum.unity.com/threads/data-encryption-tool-on-assets-store.738299/

DropzoneJS which one file is?

So, I use DropZoneJS for uploading images for individual products. But i make in database 6 columns for images
"image_1" "image_1thumb" "image_2" "image_2thumb" "image_3" "image_3thumb"
I make script for uploading images on the host and also to convert image to thumb...
So what i need. How i can detect first, second and third image?
I wanna first image name to write in "image_1" and in "image_1thumb", second in "image_2" and in "image_2thumb" and third one in "image_3" and in "image_3thumb"
The dropzone docs mentions a list of events... one being
complete
which returns the current file
So for the Dropzone post the processqueue event
this.on("complete", function (file) {
// this complete will be fired 3 times...in your case
});
you can manipulate the filename here or you can store then in order and later updated your database

Get path of uploaded image in Moodle

I have added custom column to store company logo. I have used file api of moodle like :
$mform->addElement('filepicker', 'certificatelogo', 'Company Logo', null,
array('maxbytes' => $maxbytes, 'accepted_types' => '*'));
$mform->setDefault('certificatelogo', '0');
$mform->addHelpButton('certificatelogo', 'certificatelogo', 'certificate');
Once the form is submitted itemid will be stored in custom column. Say "648557354"
Now I need to get image to print logo on certificate. How can I get image path from itemid? Do I need to store any other information to retrieve image?
The itemid returned is the temporary id of the draft area where the file is stored whilst the form is being displayed. You need to copy the file into its 'real' location, when the form is submitted, otherwise the file will be automatically deleted after a few days (and it will only be accessible to the user who originally uploaded it).
I'd always recommend using the filemanager element, if you are planning on keeping the file around (filepicker elements are for files you want to process and discard, such as when uploading a CSV file data to parse and add to the database).
Details of how to use it are here:
https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#filemanager
But the basic steps are:
Copy any existing files from the 'real' area to the draft area (file_prepare_standard_filemanager).
Display the form.
On submission, copy files from the draft area to the 'real' area (file_postupdate_standard_filemanager).
When you want to display the file to the user, get a list of files stored in the file area (defined by the component, filearea, context and, optionally, itemid, you used in file_prepare_standard_filemanager and file_postupdate_standard_filemanager). You can do this with: $fs = get_file_storage(); $fs->get_area_files().
For those files (maybe only 1 file, in your case), generate the URL with moodle_url::make_pluginfile_url.
Make sure your plugin has a PLUGINNAME_pluginfile() function in lib.php, to examine incoming file requests, do security checks on them, then serve the file.
There is a reasonable example of all of this at: https://github.com/AndyNormore/filemanager

How do I use IPTC/EXIF metadata to categorise photos?

Many photo viewing and editing applications allow you to examine and change EXIF and IPTC data in JPEG and other image files. For example, I can see things like shutter speed, aperture and orientation in the picture files that come off my Canon A430. There are many, many name/value pairs in all this metadata. But...
What do I do if I want to store some data that doesn't have a build-in field name. Let's say I'm photographing an athletics competition and I want to tag every photo with the competitor's bib number. Can I create a "bib_number" field and assign it a values of "0001", "5478", "8124" etc, and then search for all photos with bib_number="5478"?
I've spent a few hours searching and the best I can come up with is to put this custom information in the "keywords" field but this isn't quite what I'm after. With this socution I'd have to craft a query like "keywords contains bib_number_5478" whereas what I want it "bib_number is 5478".
So do the EXIF and/or IPTC standards allow addtional user-defined field names?
Thanks
Kev
It can be used for that, but it really shouldn't: it's meant to be user-editable and so isn't a safe place to put critical metadata. Using an XMP sidecar is better for this kind of thing: in XMP, any field added that a given app does not understand is, according to the standard, supposed to be ignored by that app and not destroyed.
I don't know if there are applications to do this but by the standards described for JPEG files there is a field called Comments where you can assign values that could act like tags.
C# code:
using System.Windows.Media.Imaging;
using System.IO;
...
FileStream fs = new FileStream(#"<img_path>", FileMode.Open, FileAccess.ReadWrite);
BitmapMetadata bmd = (BitmapMetadata)BitmapFrame.Create(fs).Metadata;
bmd.Comment = "Some Comment Here";
also if you are looking for an application that already has this functionality built into it, then might i recommend Irfan View (open pic, go to Image menu, click on Comments button).
Hope this helps.