
Change the aspect ratio of an image using CSS
24/09/2021In this post I'll show you how to change the aspect ratio of an image or element using CSS, and explain why this is important.
Quite often you will need to create or edit files in a PHP application. The problem is that you will probably need to create the directory structure at some stage. Therefore, it is useful to know how to recursively make directories easily.
To do this we are first going to check whether the current directory structure exists. If it doesn’t, we are going to recursively create all the folders using mkdir(). You might have guessed, but the purpose of this function is creating directory paths. You can directly create a directory or recusively create the directory structure by passing a boolean flag.
We’ll start with a function, and pass it a path variable. This will serve as our base:
function makeDirPath($path) {
}
We can check whether a directory exists using either file_exists() or is_dir(). The thing is, is_dir() is actually telling us whether the given path/file is a directory, and therefore it returns false if the directory doesn’t exist. file_exists() checks whether the file or directory exists, and as we are checking whether a directory exists I strongly suggest we use it.
function makeDirPath($path) {
return file_exists($path);
}
Note: We are checking whether the file exists first to avoid errors later.
Our function is currently pretty useless. It returns true or false depending on whether a directory exists or not. The next step is to use mkdir() when file_exists() returns false.
To do this we are going to use an or (||) operator:
function makeDirPath($path) {
return file_exists($path) || mkdir($path, 0777, true);
}
We are passing mkdir the path, the file permissions (0777 is default, and it is ignored in windows), and setting the recursive flag to ‘true’. If the file doesn’t exist, the entire directory structure will be recursively created.
Drop the function into your project and call it whenever you need a new directory.
In this post I'll show you how to change the aspect ratio of an image or element using CSS, and explain why this is important.
Here is how you can fix node incompatibility error, using "Node Sass does not yet support your current environment" as an example.
Need to make a directory in PHP? No Problem! mkdir has you covered... Need to recursively make directories in PHP? mkdir has you covered, too!
Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.
Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.
Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!
How is this article written on 05/03/2021 when today in only 03/16/2021 ?????
because : DD/MM/YYYY
Nope, he’s come from the future 😎 haha
He comes from* Sorry, my english is not pretty good 🙁