use some online websites to show a random image in my code/websites, but it is very slow and sometime doesn't provide an image. Since image is usually a bigger file (than text) and it might be a load on those online servers. Therefore I decided to develop a small module where I simply upload images with any Content Types and then I can get a random image from them of desired dimensions (wxh).
In this article I would like to create a simple Drupal ( MVC ) module. The big question here is how to resize an image to user required size. Well we can do so by creating an Image style in Drupal and can choose it for any rendering image. That works but we can create a limited number of image sizes/styles this way.
I wanted to have a plethora of image sizes available to users for example ranging from 16 pixels to 4800 pixel and this would result in 22 million different styles which is not feasible using Drupal GUI Image styles.
At the same time I wanted to use the Drupal builtin modules for Image Styles and Image Effects. Hence I ended up with creating/configure the image styles on the fly (at run time). This way we don't need to save any of the image style in Drupal DB.
The user simply asks for a dimension, our module will create an object (OOP), resize it and then provide that image to the user through the API.
Create File Structure
- Create info yml file
- Create Directory
mkdir -p src/Controller
- Add a controller SiteController.php inside the above directory. It will hold all the code for our simple module.
- Create file routing yaml.
Here is the online site https://rimages.softhem.net/image/1800/900
To download the source code from local server
git clone ssh://ali@g10:2222/home/ali/repos/modules/rimages