spatie

All my posts about spatie.

Easily convert images with Glide

Glide is an easy to use image manipulation library by Jonathan Reinink. It was bumped to version 1.0.0 a few days ago. Glide can created optimized images on the fly by levering url's such as /img/users/1.jpg?w=300&h=400&fit=crop. Take a look at the example in the Glide documentation to know more.

I think Glide provides a very nice API to create image manipulations. Unfortunately it isn't very easy to use the API to generate an image using code. So I created a little package for that called laravel-glide. All new major versions of Spatie packages will require PHP 7, laravel-glide is no exception to this.

Here's an example of how to create a greyscale version image with a maximum width of 50 pixels.

GlideImage::create($pathToImage)
    ->modify(['filt'=>'greyscale', 'w'=> 50])
    ->save($pathToWhereToSaveTheManipulatedImage);

Take a look at Glide's image API to see which parameters you can pass to the modify-method.

Read more

A PHP 7 / Laravel package to create slugs original

by Freek Van der Herten – 2 minute read

Spatie, the company where I work, recently released a Laravel package called laravel-sluggable. It automatically creates unique slugs when saving a model. To install the package you just need to put the provided Spatie\Sluggable\HasSlug-trait on all models that have slugs. The trait contains an…

Read more

Join 9,500+ smart developers

Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Building a crawler in PHP original

by Freek Van der Herten – 4 minute read

When Spatie unleashes a new site on the web we want to make sure that all, both internal and external, links on it work. To facilitate that process we released a tool to check the statuscode of every link on a given website. It can easily be installed via composer: composer global require…

Read more

On open sourcing Blender original

by Freek Van der Herten – 3 minute read

At Spatie we use a homegrown Laravel template called Blender. It's being used on nearly all our projects. When starting with a greenfield project we take a copy of Blender and make project specific changes in that copy. We built it because we want maximum flexibility and don't want to be hampered by…

Read more

URL signing in Laravel

The project I'm currently working on will have to send out mails to all its users on a regular basis. It's not a newsletter: the contents of each mail will be very specific to each user. The mail also should contain a link to unsubscribe the user from simular future mails.

The link could look like this: https://myapp.com/unsubscribe. Clicking on the link would direct the user to a login page. After the user has logged in the unsubscribe can be automatically performed. In my mind requiring the user to login first in order to unsubscribe from something isn't very user friendly.

This can be improved by adding the id of the user to the link. Here's what that could look like: https://myapp.com/user/1/unsubscribe. With this link you the app can unsubscribe the user with id 1 in one go. That'll work, but it's not very secure. Unsubscribe links for all other users can be easily guessed. Such links can be made more secure by adding a signature and an expiry date on them.

My colleague Sebastian coded up a Laravel package to create signed url's with a limited lifetime. Here's example where the url gets signed and made valid for only one day:


echo UrlSigner::sign('https://myapp.com/user/1/unsubscribe', 1);

This outputs an url that looks like: https://myapp.com/user/1/unsubscribe?expires=1123690544&signature=93e02326d75

The validate-method can be used the determine if a signed url is (still) valid:


$isValidUrl = UrlSigner::validate($theSignedUrlInTheExampleAbove);

The signature is calculated using the original url itself, the expiration date and a secret string that's specific to your project. When a malicious user tries to change any part of the url the signature won't match up.

I'm assuming that the most common use case of signing url's is to protect routes. The package supplies a middleware that protects routes from invalid signed url's. In the following example only requests with a valid signed url will hit the controller:


Route::get('unsubscribe', ['middleware' => 'signedurl', 'uses => 'UserController@unsubscribe']);

If you're interested in using the package, take a look at it on GitHub: https://github.com/spatie/laravel-url-signer

There's also a framework agnostic version: https://github.com/spatie/url-signer

E-mails can be intercepted and are never 100% secure. Bearing that fact in mind you should never use this kind of link for any destructive action.

EDIT: Some fellow developers pointed out that I could also obfuscate the id in the url. Here are two good libraries to do that:

When using obfuscation of the id this url `https://myapp.com/user/1/unsub `would become something like: `https://myapp.com/user/kwxgqu5w/unsub`

And sure enough, the unsubscribe links of other users cannot be easily guessed. A small downside however is that the url becomes less readable. The big disadvantage is that the url will remain valid forever. As these links through an unsafe medium I think it's a good idea to give them a limited lifetime. Signing an url will do that.

Read more

Our open source software original

by Freek Van der Herten – 1 minute read

The past few months my colleagues and I invested quite some time on creating open source software. Because there now are a lot of packages under the Spatie-vendor name, we decided to put a nice overview on our website. Obviously these packages benefit the community, but there are a lot of advantages…

Read more

Common string functions

When working on projects I found myself, over time, needing the same string functions over and over again. Things like how to shorten a string but still let it end on entire word, replace the last occurrence of string in a string, ...

Instead of keeping these functions in a helper file locally in the project, I made a package out of it. It was a good opportunity to spice it up with bit of OO to make them chainable. An example:

[code] // outputs "MIDDLE" echo string('StartMiddleEnd')->between('Start', 'End')->toUpper();



In addition to it's own methods, the package provides <a href="https://github.com/spatie/string#integration-with-underscorephp">an integration</a> with <a href="https://github.com/Anahkiasen/underscore-php">Maxime Fabre's underscore package</a>.

You are very welcome to submit pull requests to add functions that you feel are missing. Be sure to include some unit tests to ensure everything working as intended.

Granted,  it isn't the most sexy package in the world, but it sure is handy.

<a href="https://github.com/spatie/string">https://github.com/spatie/string</a>

Read more

An SSL certificate chain resolver original

by Freek Van der Herten – 1 minute read

When installing an SSL certificate on your server you should install all intermediate certificates as well. If you fail to do so, some browsers will reported "untrusted" warnings for your site like this one: Searching and downloading those intermediate certificates can be a hassle. It…

Read more

A Laravel webshop original

by Freek Van der Herten – 3 minute read

Earlier today Spatie, the company where I work, launched a webshop polkadots.be. When the project started one of the first choices that we had to make was if we were going to use an existing solution or build our own custom solution. For this project we took an extensive look at some existing…

Read more