How to add a text+image+text+image+ ... in mysql? - php

I'm using php for scripting. My typical problem is how do I add a question in mysql which has a lot of alternate text and images. Imagine something like a description from Organic Chemistry which has those benzene structures.
What's the most efficient way to do this? I understand that if I had a single image I can add an url to that image in the database.

It may be poor but i do it this way in my projects. You can create another table in your database.Let me explain this:
Text...Image...Text
You can handle this by this steps:
Create a table that will be post_meta
Insert every image into this table and every image should have a post id
In the other table(actual post table) store your post something like this:
Text...{MetaImage}....Text
When printing your post if you replace your tags with the image paths in the table post_meta, you could handle this.
By this way:
1. You will not store long and dirty image src's in the post table.
2. Your all media contents will be in a table.(image,music,video etc..)

Related

Good ways to implement multi sizes for thumbnails?

Thanks for jumping by!
I'm building a PHP script to manage employees, and I would like to get your help in deciding which good way do you use to implement multiple size for profile picture thumbnail.
The sizes may change (would like to keep it dynamic), thus, I can't set a specific column name for each thumbnail size.
I assume there are few options:
I can create a database table for each profile picture, there I will save the thumbnail path and "bind" to the user id with a foreign key, then I will be able to get all profile pictures and sort them based on my needs.
I can generate a thumbmail and save its path in the "users" table in a special column called "thumb_path" or something similar - in the same way I can store instead json of the profile picture paths (while the key represent the thumbnail size).
But both seems to be a bit wierd for me, and I can't clearly thing about another way to implement in efficiently.
Will be happy to get your kind help!
Marco.
I suggest to create one table for the thumbnails, with a reference to the user_id, the thumb path and the info about the size. You can have multiple rows for a user.
Hope this will help you.

Why save uploaded image path in database?

I have posts system where users can upload images inside a post, I intend to build the images paths url and not store the uploaded images paths in database.
I will simply use unique Post ID to build the url path for post images like:
$path = "images/{$postID}/";
Then when rendering the post I will use
$images = glob($path);
When post is deleted I will simply:
unlink($path);
Why all implementations for php image uploads that I read about contains table to store images paths? Why not this method are there any disadvantages that I am not aware about?
I think that this one can go with a little discussion. Although the image location is recommended to be stored in the database, but it is not always the best way. If you according to your logic find some algorithm to uniquely name images based on blog id or something that would save you database latency cost.
For the same i would like to give example of a sample use case. You are either way fetching blog text from database. Now you can add another field to store the number of images that you have associated with the same. Now take the unique id of blog suppose uq1. If that blog has 6 images , you can name the images as
$path = $uq1.$imageindex
remember that you will also have to store the image in same path. You can always get fancy and add simple XOR encrypt. Although the use of XOR is mainly when you are also associating sensitive data to the same. This will save you some database latency.
Hope this helps
its a good idea doing it in this way.
unless you don't need duplicates of a lot of images
for example if you have image apple.jpg with size 500K,
and you have 2000 posts that should have apple image in them.
so you will save 2000 x 500k of apple image.
but if you have it in the database and you just select images from
something as gallery so you will have only 1 image for apple.jpg
but it linked for 2000 posts using the database table.
but if you dont care! about duplicates and data size, or you are sure that there is no any post have any image like the other! so its ok.
summary: saving all post images in folder with its id is the easy way
but it will cost you space ( duplicates and so on )
the other way, saving the image link in database and just link it with every post need it. its better.

Laravel 4 - Storing unlimited filenames + image sizes

I was wondering if someone could help me out.
Im in the middle of writing migrations for a project, im a bit of a newb.
What i want to be able to do is store a gallery on each row of a table.
I have the following fields.
id
imagecount
displayimage
timestamps
The question i have is .... How would i be able to store varying amounts of imagenames + the sizes of those images in a field.
As im not going to be able to know how many images are in each gallery i have to be able to store varying amounts on the fly.
What would be the best way of doing this?
Any help would be greatly appreciated.
Cheers
If you want to store images in single table then you have to use JSON. In JSON you can have varying amount of images as separated indexs.
Although you have to spend more coding effort to add or remove images from gallery
Other solution would be to use a 2-table based approach. Where you need table structure like this
Gallery <id,added , other stuff ..>
Images <id,gallery_id,name,size, other stuff ..>
Its really straightforward. Store images in Images table.Link them using Primary-Foreign-Key relation with Gallery table.

Best way to use MySQL for image galleries? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the best way to store a series of images in MySQL?
This is the first website that I built for a friend a few months ago:
http://www.jpsinghphotography.co.uk
I want to rebuild it as my first PHP/MySQL project, but I had a couple of questions regarding how to structure the database.
1) Is it best to put the actual image files in the database using BLOB data types; or store the file names and use these to look up the images from another folder on the server?
2) I'm new to SQL and I'd initially imagined a seperate table for each gallery I want to create. In what ways is one table sorted by category better?
Thanks for any and all advice
Welcome to stackoverflow.
You can put image data in MySQL, but it's usually better to put them as files. That make them easier to cache and thumbnail, otherwise you have to do them yourself.
One table sorted by category would allow you more flexibility in category names and make it easier to move images between categories as well as get information from multiple categories such as image count of each category.
I would stick to providing a reference ie. the file name in the MySQL table. Putting the images in the table as BLOBs would mean you're now maintaining two copies of your pics. If you're primary application is to store history, this makes sense, but otherwise, a simple reference to the original is probably better.
A secondary issue may be storage space: you'd obviously be dealing with some very large tables if you BLOB the pictures. That may or may not be a problem.
As others posted, creating multiple tables for each gallery is a definite no-no and against the whole philosophy of using a relational database. Simple add a column to the pictures table that holds the gallery id and make it part of the unique key. You may also want a separate gallery table too, to be more 'normalized' about it.

Image Gallery tie in with database?

I'm developing an image gallery for one of my customers, they want a very minimal, simple and easy user experience. The image gallery must contain a title and description of each image, we have created a app to upload the images and set titles and descriptions this is stored in a mySQL database. We're developing this site with php.
Before I go off and develop this, does anyone know of any opensource galleries that tie into a mySQL database with similar functionality? Or maybe this approach is naive? Maybe I should approach this from a different direction and not use a mySQL backend..
Any advice is very welcome.
Josh
This one looks decent: http://gallery.menalto.com/. It supports MySQL and a bunch of other DBs.
For something this simple, you could get by without a database (however I doubt you would be able to have a title and description for the images without using a flat-file database; not recommended). To do it without a database, try this: http://spgm.sourceforge.net/.
However, if you already have written the uploader which works with MySQL, I would go with the database option and tweak it if necessary to work with the first link I posted.

Categories