简体   繁体   中英

Dealing with images using JS/PHP/MySQL

I would like to store images that users upload and retrieve them upon request to certain pages. More specifically:

  • How do I get users to upload photos?

  • How are they stored with respect to the databas?

  • How can I control image size/type and such (for when they upload)?

Sorry if these are pretty noob questions, just getting into things and unsure where to look. I'm totally cool with people just posting links to other resources!

Thanks.

How do I get users to upload photos?

At its simplest, <input type="file" /> (note that the form tag needs a special enctype="multipart/form-data" attribute for this).

How are they stored with respect to the databas?

You'd generally store them in the filesystem, and store their location in the database.

How can I control image size/type and such (for when they upload)?

If you want to do this before the user uploads to your server, you'll need to use a Flash uploader like SWFUpload - there isn't a way to do this with a basic HTML form.

If you want to do it after the user uploads, there are a number of approaches. The getimagesize() PHP function will tell you if a file is a valid image, and its size and proportions.

The PHP manual has a series of pages on file uploads: http://php.net/manual/en/features.file-upload.php

You need to use multi part MIME type to accept images. Just like type="password" you have something to accept image.

You can store images as BLOB in DB

But to bring back a BLOB from DB to show it in the screen as a photo, you cannot directly use a img src tag. You have to use some streaming technology and call a servlet perhaps in the target field which will read the blob line by line and stream the bytes to the screen.

Image resolution thing is a big topic in itself and you can get some programs on the internet to resize the image while maintaining something called ASPECT RATIO which is very important.

All these ar from my own experiences and you can take them as starting points. Forgive me if these are no way helping you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM