What is the best way to upload & access images in Yii2 advanced? - yii2

I'm new to Yii2 & I have the following question: what is the best way to upload & access images in Yii2 using advanced template?
Let's say I have common\models\User model with avatar attribute and I want to upload & access this image from both environments: backend & frontend. So what is the best way to do this?
Important note: accessing this image in views I may not know from which environment the image was uploaded (by admin or custom user).

Create a separate folder for static resources
You could serve all your static files in a cookie-less subdomain static.mysite.com that would point to a /static folder in the root of your project. Create aliases for that in your common/config/main file: #static and #staticPath for example, use that through the code.
When you are saving an image, either from the backend or frontend, you use the alias, for example
Image->saveAs(Yii::getAlias('#staticPath/img/users/') . $user->avatar)
When you are displaying the images, use the alias.
Html::img(
Yii::getAlias('#static/img/users/') . $user->avatar
)
You could go even further and create aliases that point directly to the image or img/users folder, or store all the images together and do away with the img/users folder.

Related

store image locally and refrence them in react app

I'm building a desktop app with React and MySQL, I only store the image name in MySQL and the actual images are in the images folder in /src/images, When uploading new images to the images folder, the react app reload, So to avoid that from happening
I either need to store the images in MySQL DB
access the images locally not in /src but outside like D:/images/*.
but react has some importing restrictions.
How can I resolve this issue, please share your knowledge.
You need to import and use images directly in the component or serve your images as publically available static assets.
You can do this in plenty of ways:
Put all images in React's /public/images directory making them static assets of your page
Serve images as static assets from your backend (here how to do it in express.js)
Serve images as static assets from 3rd party storage service (e.g.: Azure Blob Storage)
Store images in database and provide them to frontend as data blobs - while it is possible, it's not recommended.

How do I concatenate 2 strings in Django

I need to show an image of the database, but I need to insert a slash before {{...}} because only then does the file access the static folder. What should I do?
You can do this like recommended in the two comments but actually django coveres this exact usecase. What you are searching is an administration of your static files, the docs are here: https://docs.djangoproject.com/en/1.11/howto/static-files/ .
If you want to have your files organised with the orm, checkout https://docs.djangoproject.com/en/1.11/topics/files/ especially the second code snippet. A File object gives you a name, path and url which should cover all your needs including absolute and relativ paths for your files.

Not allowed to load local resource: file:///D://foldername//filename.jpg

Actually I have to upload 20000 images into mysql database if I store all images to Mysql database as Blob type the performance is reduced and It will slow down if we upload other images if required.To avoid this problem I am storing all images into folder and their path's into mysql database table.and retriving these images with pagination .
now data is showing in table but images are not displaying.
String sb1 = new String("file://");
String sb2=new String(user.getPlaceImage());
out.println("<td><img src="+sb1.concat(sb2)+" width='70' height='50' /></td>");
If there is any alternative way to deal with this problem?
Here is a basic alternative technique that I use to serve images to users:
Create a folder outside your website root so users won't have direct access to the files using HTTP. Let's say you already have a database table that contains URI to all the images. Create a interface file, for instance: getimage.jsp?id=42 that serves user the requested image file. Also, if you are storing relative URLs in the database you need to be extra careful because you might be trying to read the file from a wrong path in relation to your JSP file that's trying to access the image.

How to handle uploading html content to an AppEngine application?

I would like to allow my users to upload HTML content to my AppEngine web app. However if I am using the Blobstore to upload all the files (HTML files, css files, images etc.) this causes a problem as all the links to other files (pages, resources) will not work.
I see two possibilities, but both of them are not very pretty and I would like to avoid using them:
Go over all the links in the html files and change them to the relevant blob key.
Save a mapping between a file and a blob key, catch all the redirections and serve the blobs (could cause problems with same name files).
How can I solve this elegantly without having to go over and change my user's files?
Because app engine is running your content on multiple servers, you are not able to write to the filesystem. What you could do is ask them to upload a zip file containing their html, css, js, images,... The zipfile module from python is available in appengine, so you can unzip these files, and store them individually. This way, you know the directory structure of the zip. This allows you to create a mapping of relative paths to the content in the blobstore. I don't have enough experience with zipfile to write a full example here, I hope someone more experienced can edit my answer, or create a new one with an example.
Saving a mapping is the best option here. You'll need to identify a group of files in some way, since multiple users may upload a file with the same name, then associate unique pathnames with each file in that group. You can use key names to make it a simple datastore get to find the blob associated with a given path. No redirects are required - just use the standard Blobstore serving approach of setting the blobstore header to have App Engine serve the blob to the user.
Another option is to upload a zip, as Frederik suggests. There's no need to unpack and store the files individually, though - you can serve them directly out of the zip in blobstore, as this demo app does.

CodeIgniter: Storing an image in the database?

I would like to store images in my mySQL database using the CodeIgniter PHP framework.
How do I write and read the image?
Thank you.
You will have to use the file uploading class of CI to upload the files and use the models to save them to the database, here are related resources:
File Uploading Class
Models
It's considered bad practice to store the actual image files directly in a database. Instead you can just store a path to the file in the database and store the file in a directory on the server.
AstaHost Forums
Best approach for storing uploaded image
CodeIgniter - Uploading an Image through a form, store the location of the image in database