Jump to content

Original Image Size, Thumbnail Image, Anchor Tag, Referencing?


Recommended Posts

The script for creating a new file name for the image:

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
        $file_extension = end($ext); //store extensions in the variable

        $new_image_name = md5(uniqid()) . "." . $ext[count($ext) - 1];
		$target_path = $target_path . $new_image_name;//set the target path with a new name of image

The script creates a new file like:

f6c9b8d9db05366c3504210cded9ddb2.jpg

and moves the file to the "uploads" folder. And then the script also creates a thumbnail with the same file name and moves the file to the "thumbs" folder.

 

The issue I am having is that the same ID code could happen again for a different image in the database, thus I would be calling a different original sized image than the thumbnail image.

 

My question is:

How to avoid this issue of the same ID code has happened again for a different file.

What is the proper way to reference the anchor tag of the thumbnail image to its actual original sized image?

 

With the script I have the thumbnail image would be coming from the "thumbs" folder and the anchor tag would get referenced to the "uploads" folder to get the original sized image.

Edited by glassfish

As for ensuring no duplicates, how about just using a file system function to check if it already exists?

Thanks for the answer.

 

I am wondering, if there are a lot of images and the website has a "higher" traffic would that still work out? I am actually more wondering if it would "work out" or if problems would occur?

Edited by glassfish

Are you opening up your own image museum?  What are you worried about?  What is "working out" supposed to mean?

 

Basically what I see in your posts is :

 

1 - accept an image that you allowed someone to upload thru your form.

2 - rename that image and store it someplace

 

To be safe you have asked how to ensure name integrity.  You have been offered the quite simple solution of checking if that filename exists in your images folder before saving the new file there.

 

Case closed?

I did not think you would see this as "name integrity".

 

Actually, I thought, perhaps there are alternative ways. With the way you suggested I was not sure if it still "works out" when the traffic is high.

Edited by glassfish

You want to be sure that the name you create doesn't wipe out another file.  I think 'name integrity' is an apt term.  Dont' you?

 

Your concern is about the possibility of two instances of your script creating the same random name at the same time.  Even tho I don't quite follow your convoluted logic (and wasted logic) I dont' think that's going to happen.

How to avoid this issue of the same ID code has happened again for a different file.

 

Append something like timestamp to the files name when saving it.

If you want to store it as md5 after so be it, as long as you save what the image actually is in your database.

$_SERVER['REQUEST_TIME'] will work

 

What is the proper way to reference the anchor tag of the thumbnail image to its actual original sized image?

 

It's supposed to display the sizes of the image are viewing.

If you want to add additional info to original size...add it to alt or title, under the image on page, possibly even a js script like mouseover event.

First thing that I see is that you could improve the variation in the uniqid, by using the optional parameter to increase entropy.  

 

Change uniqid() to 

 

 

uniqid('', true)

 

The more variation of input the better the distribution of hashes.  There is always a chance albeit infinitesimally small that you will have a collision (2 different inputs hash to the same hash).  The chances of a collision on random data requires something around 2^64 hashes.  You will never approach that number of files in your system, short of your system being at google/facebook-like scale.  You could decrease this probability even more by using a larger hash -- sha1 being the most likely alternative.

 

With that said, and as everyone has already pointed out -- if this is really important to you, then you can simply take the random name generated, and do a  simple filesystem check to see if a file of the same name exists, and keep iterating until one does not.  Practically speaking, the chances of it happening in your system are close to nil.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.