How to experiment with ImageMagick using Tinkerwell, a tool for PHP development
Tags: laravel, development, coding, php
Published: 2020-05-16 15:17:31

---
title: How to experiment with ImageMagick using Tinkerwell, a tool for PHP development
published: true
date: 2020-03-09 09:01:01 UTC
tags: laravel,development,coding,php
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/0*bIp565kjnB5j94QX)<figcaption>Photo by <a href="https://unsplash.com/@xpipzzyx?utm_source=medium&amp;utm_medium=referral">Zoritsa Valova</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

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_](https://tinkerwell.app/) 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_](https://www.php.net/manual/en/book.imagick.php). 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](https://ma.ttias.be/install-phps-imagick-extension-on-mac-with-brew/) 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:

{% gist https://gist.github.com/peterfox/a1893648d7c67b66f8dc28f918db5721 %}

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:

{% gist https://gist.github.com/peterfox/7c43bd4ec81521644d610c2c2a2a90f4 %}

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:

{% gist https://gist.github.com/peterfox/9fc34c5ee77c30544f84fce6521ac17b %}

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

![](https://cdn-images-1.medium.com/max/1024/1*Ic3LUGtkkBNjcry51NHCBA.png)

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](https://www.peterfox.me/) and feel free to follow me @[SlyFireFox](https://twitter.com/SlyFireFox) on twitter for more Laravel tips and tutorials.