I am creating a website for funny images. Those images are stored in a file in the server. I have a database that contain all the information for all of those images (name in the file, type, views, likes, dislikes, reports, and so on).
I want to use the name of that image as my url www.example.com/name_of_the_image and when I click on the image it will take me to the next (image page) so each image will have its own unique url.
I already tried to pass the id of the image from the database to the url and keep browsing the images but it seemed unprofessional way to do what I want to achieve.
Now what is the best and efficient way to do this ? Any ideas ?
use ’slugs’.
have an extra field with your database image data called slug.
everytime you upload an image store it using the slug(usually a slug is the title in lowercase with underscores instead of spaces).
Then use .htaccess to rewrite your urls to something pretty that uses the slug to retrieve the info.
www.host.com/images/cat_in_a_pickle_jar
points to
www.host.com/displayimage.php?slug=cat_in_a_pickle_jar
Related
I need to link individually uploaded images (via an image field) to different external URLs (e.g. Link one image in one post to a website outside of my own). I've found modules that allow me to link image fields to nodes within my drupal site, but nothing that allows me to link uploaded images individually to different urls. That said, I figured I could create a text field, input a URL there, and set my Image Field in views to be linked to the contents of that field. E.g. Destination: [field_link_image_to]
However that doesn't seem to be do-able either. Any suggestions as to how I can go about doing this?
Use Link field module (https://www.drupal.org/project/link). After enabling add this field to the same content which contains the image you want to link to specific URLs.
Create a view then include both image field and link field url (you will get the exact URL which you will enter into the link field) then add custom text and create your html accordingly.
Let me know if it works or you want something else.
Thanks.
On my website I let users upload images.
I save the images under some hashed (based on name + timestamp) name.
I store both the original name as the hashed name in the database.
I would like to display the image on my page using the original name.
CASE
user uploads test.jpg
image is saved as /img/6eabd22d35b4132cc58e4dff75e466cc1e444d93053ca80087b12ff620bd3451 on the server
both original and hashed names are stored in database
display the image on a page using the original name (<img src="/img/test.jpg" alt="" title="">) which actually is /img/6eabd22d35b4132cc58e4dff75e466cc1e444d93053ca80087b12ff620bd3451
Is this possible?
Yeah, that's definitely possible. It's actually quite easy, you have to:
Rewrite all requests that conform to that pattern to a (PHP) script using the Apache module mod_rewrite;
In that script fetch the real name from the database and display the image. Don't forget to also send the correct header, otherwise it won't work.
Edit
I would suggest making a relational table which holds your hash to filename. When you do the query you can return an array of both results, one used for the image source, and the other used for the name.
OK. What I wanted to do is simply not possible.
I am making an application in which every user has to sign in first and then he can access his home page. Now on the home page, I have given an option of uploading an image. Now if the user is uploading one image I am storing the full path of the uploaded image into a database and from there I can display the image easily by an img tag...
But what should I do when the user want to upload many images? Then how should I store their full paths in a database for the same user. Give me an idea just like Orkut or Facebook. Should I make a different table with named images and should I store images in different rows with the same username. What should I do?
I don't know the logic. What should I do? How can I upload many images and how can I store their path and what will be the wisest method and how do I display many images on one page (I can display one)?
You can make a folder, named after user name and id and put all their images there.
To display many images on one page, just add more <img> tags to the page.
This seems to be more of a design question than a PHP question. I would create a separate table to store all paths, this is more normalized.
You still need to handle the UI, but if you are doing a sort of gallery then that is fairly simple with some jQuery sideshows or something like that.
Yes, you want to use a separate table to store the image paths. You'll most likely want a record ID, the User ID, and the path to the image. You could also add a field to contain the sorting order for the images.
Having the sorting order field will allow you to page through the photos if there is more than one page of photos.
Your thinking is correct where you suggest creating a separate table with rows containing the image path and the username. The concept that you are dealing with is called cardinality. I'd recommend that you take a few minutes to read about this concept, since it is so important to database design.
In this case, you're talking about a one-to-many relationship between the user and the images.
I am trying to upload an image to a directory on a server. i'm using the tutorial found at http://www.reconn.us/content/view/30/51/.
First, is that a good method for uploading images using PHP?
Second, I'm also going to store the info in a MySQL database. What is a good way to deal with images that have the same name that the user uploads? For example, if a user uploads a file 'test.png' 2x in a row, what should happen to the second filename? From the script above, both will get a unique filename, but how would I as the user access that image again? I couldn't just query because the only name I know was the duplicate name I gave it, and I definitely don't know the unique name the server gave it using the upload time...
Third, what is a good max file size for images?
You can report the unique URL back to the user after the upload so that the user will know where to find the image. So, the first test.png could be http://www.example.com/images/fjdklagjsdl.jpg and the second could be http://www.example.com/images/jklfsdlkj.jpg
You can also provide some kind of interface for users to view images they've uploaded. If you display a thumbnail of the uploaded image next to the image's unique filename, it will be easy for the user to identify which image is which.
This is the method I use:
Users upload images
Server saves the image with a unique (GUID or something) filename and stores - both - the unique generated filename and the original uploaded filename in a database
Images are linked to using either the original_filename, unique_filename or primary_key for the images table.
The images are taken from the server, and served using the original filename stored in the database. This way you avoid chances of conflicting filenames and you preserve the image's original filename. In addition, this allows you to build a search on the original_filename column for the user to use.
With this method, unique filenames never have to be exposed to the user, instead they're used to locate the image associated with a specific id or original_filename in the 'images` table.
Of course, if you don't care about giving the original filename to the image when it's displayed, you can just generate a unique filename whenever you want to store it.
I have a form that searches a mysql db for whatever the user is after, and returns the results on the same page in a div (using ajax then php to search mysql)...
The results are ads, as cars for example, and I want each ad to have a unique image associated with it so that it displays the images next to the ads...
Storing images in BLOBS arent really a good solution I have heard...
How would I do this the best way?
Thanx
A fast way will be to store images in a folder giving unique filenames in folder or separate folders if you want to put images of different categories in separate places. After searching the available ads, read associated unique file-names and server to client.
Suppose you save images related to car ads in folder /images/cars/. User searches for Audi S6 and the result returns 5 ads.
Now we will proceed based on the naming of the image files. If you give file related to each ad unique name and put that name into record for that ad then simply get that name and create image URL as follows:
/images/cars/ + result_row('image_name')
If you are naming images based on id of ad record then use this scheme:
/images/cars/ + result_row('id')
If you mean that at first you were sending image bytes in response, then you don't need to do that. simply send the path constructed and use it in src property of img tag.
PS:- My PHP skill are not very good now!
Typically, you would want to store image records in the database as details about the image and then a file path to the actual image. Then, store the images in the regular file system.
As a side best-practice, you typically will want to store the path relative to some common root, then you can append the file root so the store of images can be moved around.
ie> store ads/cars/1005.jpg and then append a root of 'C:/myApp/images/'
Each unique ad will have a unique identifier in the database. You could create image subdirectories for a given set of images and save the images based on their unique identifier.
i.e. advertisement 506 could be stored in /img/506.jpg
Ultimately you would query the database for the advertisement and then you would assume you are loading the image with filename identifier + '.jpg'