Writing you own media library: where to start? - php

i'd like to add a media library to a custom-made CMS that is build on Zend Framework and Doctrine. The goal is to build something like Worpress's media library: you can upload media and then attach it to for example an article.
Do you have any suggestions how to start on this? How should the database be designed? Is there any code that i can (re-)use to build this? Is there any literature that I should read on this subject? Thanks!!

I don't know how WPs library is built, but it's Open source, you can take a look.
As for table structure, assuming you want a many-to-many link you'd want a cross reference table, something like:
record_id (int)
media_id (int)
title (text)
caption (text)
rank (int)
Then the media table would be something like:
id (int)
title (text)
caption (text)
filename (text)
type (image|multimedia|document)
Perhaps you'd also add a folder_id field to the media table, or perhaps you instead want tagging, in which case you have a third table with media_id and tag as fields (or you have tags as comma separated values in the media table).
This will let you link one media to multiple items and one item to multiple media. It'll let you set a title and caption for the media item and override it for a specific link. For instance you have a picture of a house with the caption of whose house it is, but in one link the caption is overwritten (in the cross reference table) with text saying how this house is an example of a certain architecture. SQL's coalesce function will come in handy for getting the correct title and caption.
If the content that links to media comes from multiple table, say you have a table called 'staff' and another called 'products' and they can both link to media, then the cross reference table also needs to have a table_name field. The type field is so that you can easily get only the multimedia attached to a record, or only the images; you may want to count how many documents are attached, how many images etc, basing this off the filename on each query means you have slower queries.
One thing this doesn't quite cater for is hosting the media off site. If you use say Amazon S3 for storing these images, then the 'filename' field would actually be a URL to the image. I'm just shooting this out there as something you need to take into consideration when designing the media library.
I can't think of any literature or code you can copy-paste though, it's not difficult to do, though I do appreciate this can be time consuming.
Best of luck.

write a file uploader and file browser
that's all

Related

Linking an entity to have several images and videos

I'll try to explain my problem as much as I can.
Let's say I have an entity called Profile. This entity has a few fields (name (string), description (text), birthdate (date))... etc. And I want to add to this entity images and videos, so these would be shown in the user's profile. If it was just one image and one video to upload I would've used a field image (string) and video (text) or something like that. But knowing that I want the user to upload several of each, it won't work.
The images will be uploaded by the user in the form. And for the videos, he needs to paste an embeded tag from any major website.
How to do this please? I thought about creating a junction table for both an image entity and videos entity. But I'm not sure at all if that's the right way to do it either in general or in Symfony specifically.
You should consider the following approach:
- create tables for photos and videos (create entities Photo and Video if you are using Doctrine);
- add column userId to the corresponding tables (add this link property User to the corresponding entities);
- insert/update the corresponding tables (via entities) when a user uploads/edits a new photo or video.
Remember, you have many (in general) photos/videos for one user but only one user (owner) for each photo/video.
If you are using Doctrine this short tutorial about associations mapping will be useful for you:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

Grouping a Restful Architecture

Assume I have a photo gallery, with a DB something like this (a rushy mockup in Word ^_^):
I've written a rest API for it. But I'm confused about how to map a certain scenario…
/api/galleries - Lists all of the categories available
/api/galleries/:categoryID – Lists all of the galleries for this category
/api/galleries/:categoryID/:galleryID – Returns all of the info for a gallery
/api/galleries/:categoryID/:galleryID/images – Returns all images for a gallery
Now, my question is, what if I want to list ALL galleries and ALL images on a single page? As it stands, I'd have to call /api/galleries/:categoryID/:galleryID/images/ for every single gallery.
What would you think to be best practice for doing this? Maybe remove the images resource completely and just merge the images into the results for the /api/galleries/:categoryID/:galleryID query? Or maybe create a separate resource called "show-all" or something? i.e. /api/galleries/:categoryID/all/show-all and return the info combined with the images?
I'm by no means an expert on the topic, but to me your api seems flawed.
What's wrong
For example this location is lying.
/api/galleries - Lists all of the categories available
I for one would expect that it returns a list of galleries.
/api/galleries/:categoryID
Here I would expect to pass the gallery id.
What I would advice
Having a more clean api.
/api/galleries // get all galleries
/api/galleries?expand=images // get all galleries with the images
/api/galleries/:id // get a specific gallery
/api/galleries/:id?expand=images // get a specific gallery with the images
/api/galleries/categories // get all galleries categories
/api/galleries/categories/:cat // get all galleries in a category
....
By using a parameter the api doesn't get cluttered with all kind of options you might have. Keeping it clean and simple.
Here's a link to a really nice video about REST api design http://www.stormpath.com/blog/designing-rest-json-apis
Also
I don't see the need for a separate info table. It would make the database simpler if you added that info to your gallery table.
Why not using like this.
/api/galleries - Lists all of the categories available
/api/galleries/:categoryID – Lists all of the galleries for this category
/api/galleries/:categoryID/info/:galleryID – Returns all of the info for a gallery
/api/galleries/:categoryID/images/:galleryID – Returns all images for a gallery
/api/galleries/:categoryID/images – Returns all images for a all galleries

handling images, videos, docs for custom cms

I am in the process of building a CMS using php, however I am struggling on how to handle the various different assets that a dynamic page could serve, typically an average page will contain pictures of various sizes(thumb, body, main, etc), inline images and various different assets(pdf, flv, mp4, doc, etc).
At the moment a user can upload assets and create a folder on the server, I just wanted some techniques and concepts on how to manage this in terms of deleting, editing and linking to my dynamic pages within the cms? I already have a content table which contains all the content(meta_stuff, title, friendly_url, content_text, etc).
On upload should a reference be stored somewhere in say a asset table?
Should all paths to assets(images, docs, videos,etc) be stored in one table? or separate for each asset?
Should multiple image sizes(small, medium, large, etc) be stored in different fields e.g(assetid, smallpath, mediumpath, largepath?
What technique to use to link assets to the dynamic page? should this be a joined table or single? how do I go about retrieving the different assets for a page several pdfs and several images?
How to handle deleting of assets as this could be referenced to another dynamic page?
and anything else you think would be beneficial?
Thanks for all your help
Here are some basic things to keep in mind when dealing with resources (videos, images, documents..) in a scenario similar to what you're describing.
You'd want is to know what's being uploaded, what kind of stuff are being uploaded and who's uploading. To do that, it's good to have the following tables:
Edit: Sorry about the mistake, asset_types.asset_type_id should be linked to assets.asset_type_id and NOT assesst.asset_id
(Note: This is just an outline, of course you'll have more fields)
This model makes it easy to do the following tasks
Upload: When something is uploaded, you store it somewhere (with the correct
.htaccess configurations) then store that path in the table.
Delete: When the user is trying to delete something, you check if he's the owner.
When you want to add a new type, you don't need to create a new table.
Hopefully this will put you in the right direction.
Update: Answering your comment, when a user wants to add an image to an article for example, you have two options here:
The user is presented with a button/select menu/whatever to choose the desired image, once clicked it will add <img src=LINK_TO_RESOURCE" /> to the body of the content (Make sure you protect yourself from XSS).
OR
You use your own simple markup, when the user clicks on an image this [[PREFIX_IMG:IMG_ID]] will be added to the body of the content, when the content is viewed you'll replace that "tag" with <img src="LINK_TO_RESOURCE" /> which you've acquired using IMG_ID.
2nd Update: Well you have a couple of options to handle resources that aren't directly embedded in the "articles" themselves.
You can have a field in the "articles" table you can call summary for example, and one more column to the assets table which you can call asset_sub_type and have different types like summary. Then when you want to view the summary you fetch the summary text/title from database and you add the resource to it.
(This is the technique I use) When fetching the summary from the database I see which image is the database that is related to that article and the resize it on the fly with PHP and append it to the summary.

PyroCMS - Pyrostreams multiple file upload on one entry

I've got the PyroStreams module for PyroCMS and made a stream called portfolio. Everything is working as expected, however it only allows to upload just one image for my file field. That is obvious, but I'd like to know if there's a way to upload multiple images for one stream entry without adding x number of extra file fields. Or do I need to code an extra field type myself to support this? Maybe someone's got it already?
Thanks
According to me you have to create a separate database table that will hold all the images those are related to particular portfolio and the portfolio id by id by which you can fetch the images for particular record and then use any multiple image upload library like http://www.plupload.com/example_queuewidget.php to upload multiple images in the portfolio image table. Hopefully this will help you in achieving what you want.
There's a multi-upload files field type available on the pyrostore.

How can I keep a set of data during several calls to the server in PHP?

I am developing a really simple PHP application to display pictures on the web.
My application is composed by a Database with two tables Image and Categories, some simple business logic and the UI.
In the UI, when the user loads the page for the first time, has a picture (randomly taken) the possibility to browse the images with left/right arrows and the possibility to choose a specific category (one picture belongs at the most to one category).
My issue is in the browsing mechanism. If I browse the image without selecting a category everything is fine. If I select a category the website correctly displays all the thumbnails of the images belonging to that category (easy query).
However if I click on one picture (it load in another page) and then navigate with the arrows the website browses all the pictures and not those beloning to the selected category. I would like instead to browse just pics in the selected category.
How can I implement this mechanism? I alwasy pass Category_Id and Image_Id (null for all pictures) to the server function but I always lose the state of the iterator in the variable containing the query elements. How can I solve this problem?
Solutions with code sample are appreciated, however it is enough to propose an implementation logic.
Thanks
Francesco
I did something similar a while back.
Assuming that you want your images displayed in image_id order you could do the following.
On the "next" link, you make a call to your PHP script with parameters that identify the category and the id and the direction you want to move.. eg
myserver.com/myapp/images.php?action=next&cat=holidaypics&image_id=111
Then you can do a query to find the next highest image_id with the same category by doing something like "select min(image_id) from images where category_id="holidaypics" and image_id > 111"
If you're going in reverse you'd change the action to "prev" and do a query like this...
"select max(image_id) from images where category_id="holidaypics" and image_id < 111"
Does that help?
Use sessions http://ru2.php.net/manual/en/book.session.php
or pass all variables your need every time you ask server.
Consider Ajax to load your container with images of a particular category.
It is as simple as saying HI

Categories