DEV Community

Peter Fox
Peter Fox

Posted on • Originally published at Medium on

How to experiment with ImageMagick using Tinkerwell, a tool for PHP development

Photo by Zoritsa Valova on Unsplash

I’ll make this a quick blog style post compared to my usual tutorials. If you’re a Laravel (or even just PHP) user and haven’t heard of Tinkerwell then you should really give it a look. It’s an excellent tool for testing out snippets of PHP code. You can even open projects you’re working on and use classes etc. from said project. Often I use it to test some array or string manipulation that I’m struggling with.

Today though I needed to work with ImageMagick. If you’re not aware, ImageMagick is a PHP extension used for manipulating images. Using the extension is something I honestly haven’t done in quite some time. I generally hate playing with it because you’re trying to make the code do something visual which requires a lot of “tweak, run, view results” then rinse and repeat, often working in three different applications to do so. This then made me think, why not try and do it in Tinkerwell then?

Setting Up

First off, make sure you have Imagemagick installed as a dependencies. You can well by running php -m | grep magic in a terminal, if it brings back nothing you need to install the extension for PHP. If you’re using a Mac and have Brew this guide was the one I used and worked fine. In most cases it’s just getting Pecl to build/install it but having the correct dependencies can be difficult.

If you have problems with the code in the tutorial saying it can’t find class Imagick like I did at first, it’s because Tinkerwell will use the default PHP binary (for MacOS) at usr/bin/php but if you’re using Brew like me then you need to /usr/local/bin/php or you can find which php binary your command line uses using which php.

HTTP Mode in Tinkerwell

The example I’m quickly going to provide is just a fairly simple image manipulation of compositing one image on top of the other. Basically I have some images which are rectangular but I’d like them to be made into a perfect square.

To make the preview work we need to put Tinkerwell in to HTTP mode. We could use the CLI mode but then we’d have no way to actually preview the results. Firstly you need to change to HTTP mode by selecting the option under the Tinker Mode menu. This divides the UI into 3 views, one for a blade view, one for generating a response and one to show rendered output.

The code I managed to put together to perform my transformation from simple rectangle to perfect square looked like so:

To quickly summarise it, the code reads the original, finds the biggest of the height/width, makes a new image with the same dimension for the max width/height and then we composite the two together with the original in the center.

But we still need a way to render it. Ideally without saving it to the disk. To do this we’re going to just create a very basic view that takes base64 strings which we’ll use as Data URLs in the HTML. That way, we’re not saving files, we can play with the code as much as we like and see the results instantly.

The view I created looks like so:

I’ve set it up so I can see the original and the changed output as I change things. Now we just need to add the bottom line for our code to make it generate the view and return the output to be rendered. That final line looks like so:

The final result of all this will look like the following:

Please note if you want to play around with my example, the data URLs in the blade view are set to be PNGs and therefore if you try with JPEGs etc you need to update that and potentially in other places as well.

Conclusion

All in all, if you haven’t got Tinkerwell I really do suggest it. It’s very cheap for how extensive it is. There’s a lot of features there and it really makes sense compared to say writing a PHP file on disk just to echo out the results or trying to use an online PHP tool which while useful, won’t be able to tap into extensions like Imagemagick.

I’m Peter Fox, a software developer in the UK who works with Laravel among other things. If you want to know more about me you can at https://www.peterfox.me and feel free to follow me @SlyFireFox on twitter for more Laravel tips and tutorials.

Top comments (0)