Rename image to random - image

I'm working on an image uploader and if a user uploads a image I want to change the image name to something unique.
Imgur also does this for example "11Z6nlI.jpg"
So how can I generate a unique combination? I was thinking of using a timestamp since but that will also causes problems when more users upload at the same second.
I hope you guys can help me, I'm working in PHP

Although sometimes clunky, GUIDs are a good way to generate unique strings. See here for a way to generate GUIDs within PHP.

Related

Best barcodes and how/what to generate barcodes from, and how to store it the best way?

I am writing an webapp for a used thrift store. We are planning to add all items to a database, and I will be using Laravel and MySQL for this.
We also want to get a barcode scanner in order to scan and add items.
But I've never worked with barcodes before and info regarded this for a website like mine is hard to find info on, so is the barcode-scanners so I'm asking help from someone with experience.
I'm not sure which barcodes we should use, but I'm thinking a 3D one, like QR would do good. I've found several qr-generators, but I'm unsure on how I should create them properly from items.
Say the store is named Brukten. And it has items, with number IDs. Should I then generate barcodes out of "Brukten_53" or something, or just the ID, or how is a smart way? Because I can use an option like this instead of storing the generated barcode, right, and its better to generate a QR each time instead of storing the data about it?
At first we will use a web interface to add items, and later on we want to get a barcode-scanner to help scan things, so I'm guessing it would be nice with a scanner that could take pictures and add to database. So we want the DB to be ready for this, and properly made to work with such systems. So I'm wondering also if anyone can recommend me a device for this. I've seen several but unsure what to choose. 2d, 3d, something with camera, or is there other devices we could use?
What we want a device for is:
We want to store in the db: Old things, new things
We want to retrieve: all things
We want to alter info: edit things, sell, check things
Something easier than going around with a laptop, phone or a tablet and entering stuff.
Building the site is no problem for me, PHP, MySQL, Laravel framework and all is old stuff, but barcodes is a new world for me.
You only have to make sure that every single distinct physical item has a unique ID.
A simple way to accomplish this is using category ids as prefixes.
Suppose your "used men clothes category" is ID 25, and when you enter a pair on jeans of that category into the system, you app assign them ID 12345.
You can reference that item as 002500012345. You only need to calculate the 13rd digit using this function (http://edmondscommerce.github.io/php/barcode/ean13-barcode-check-digit-with-php.html) and you have a full-fledged EAN13 barcode you can print using this font (http://www.fontpalace.com/font-details/EAN-13/) and read with virtually any code scanner.
The other approach is generating a unique url for each product:
http://www.example.com/25/12345.html
And embed the url into a QR code.
You can generate the image for the QR code using this library:
http://phpqrcode.sourceforge.net/

How to name images so that other image names can't be guessed easily?

Users will be able to upload images and the name will be changed so it doesn't have the same name as another file. Using a simple convention like calling them 1.jpg, 2.jpg, 3.jpg and so on will mean other users can simple type in 4.jpg and see someone else's image.
Is there a way or a convention for naming images different while still ensuring guessing an image name is hard?
You could just write a PHP script to generate some pseudo random image name each time, like 21412adfs.jpg.
Better yet, take the name of the file being uploaded, and append a 6 digit random number to it or something, say, 19353--toy--car.png, you could even replace the 6 digit number with a number representing the date of the image uploaded.
Naming conventions can be in any form you want really, whatever works best for your setup and archive purposes. Including the date in the image name can be good, as you could easily sort images into different upload folders depending on their dates, etc.
Your best bet is to use a hashing function to generate a random string that you can use. For example in PHP you could use the following.
$filename = md5('SOME RANDOM STRING'.rand(0,200000).time());
MD5 can sometimes generate the same random string which would cause a filename collision, but the likelyhood of this happening is quite small, and if so ... all you have to do is to run the name generation again - a collision twice in a row is extremely unlikely.
Make sure you change 'SOME RANDOM STRING' to something that only you know and use on your site. It's what's known as a "site salt", it means that outsiders will have a much harder job guessing the names of your generated filenames, because they wont be able to predict and reverse engineer everything that you've put in to the mix to generate it.
Hope that helps?

Organizing Images and their thumnails

What's the best-practice method of storing a user's uploaded pictures and it's corresponding thumbnails. I noticed Flickr uses filename distinctions like: http://farm5.static.flickr.com/1234/789456123a_s.jpg where _s.jpg describes the size of the image (_s.jpg = small, _m.jpg = medium...). However, does storing images like the following make sense?
/images/123456.jpg
/images/small/123456.jpg
/images/medium/123456.jpg
...therefore, it's easy to access different sizes by simply pre-pending the folder-size name
Whatever works for you - pick a scheme and stick to it. As long as you're consistent, and document what you do, you should be fine.

Any way to rename image filenames?

Is there any way to make the "image" module store files under a new name on upload? Basically, someone could upload a file that says something like "macs are cool" or something equally absurd. Now, I don't want people to see that. I'd much rather have all the files renamed on upload to something like: "111494949478383.jpg". How would I go about accomplishing this?
The FileField Paths module should do what you want. It works with both core Upload, as well as the FileField and Imagefield modules. You may also want to check out Imagefield Tokens, which allows the use of node tokens in things like the default alt text for images.
You can use the module "filefield Paths", and put in the filename [raw] that way the uploaded picture will have the node's timestamp creation time.
Also you can use [raw]_[filename].[file-extension] or whatever combination you can use, take a look at "file name replacement patterns"
with hook_nodeapi you could move the files and rename the images when the node is saved.
Or you could add something to the image upload callback with hook form alter to rename the image once it has been uploaded
yep, form_alter is the way to go for this
there are file modules that basically use tokens to do this
edit also imagefield supports tokens http://drupal.org/node/152640
immagefield could be better in long run if cck going to Drupal 7
hope that helps

Image Upload Service - Image locations and Image Identifiers

I would like to create a image uploading service (yes, i am aware of imageshack, photobucket, flickr...etc) :)
I have seen only imageshack show the directory names ("img294", "1646") of where the image is located, in the same way - i would like to do this.
http://img294.imageshack.us/img294/1646/**jquerykd5**.jpg
1) Are there any security issues I should be aware if i take this implementation?
2) How do these sites come up with short unique identifiers ("kd5")?
Thanks all for any advice and help.
Well for starters, unless you would like the directory to be public, put dummy index.html files in there or just restrict access to public users for those directories.
As for the unique identifiers there are many ways of going about this... some of my favourite chunks of information to use:
UNIX time (if running a unix based server)
chunks of the md5 of the file
pseudo random numbers
piece of the original filename
With these and many other pieces of information at your fingertips it should be easy to prevent duplicate image names conflicting on your server as well, you can gather as many as you like and concatenate them into a string for the filename. The md5 can be placed in a database as well to aid in a method of duplicate image detection, which could save you disk space as well.
I can promise you they all use URL rewriting. This will help with security issues, too.

Resources