How to use the Flutter Custom Fields Options List to crop and manipulate images

Flutter, the popular WordPress plugin, is a very simple way to add a lot of flexible extra functionality to posts and pages. It’s a gold mine of usefulness that allows WordPress to compete with more robust CMS applications, and with some tweaking can provide some really powerful features.

As awesome as Flutter is, the documentation blows. Any new user will experience firsthand the displeasure of sorting through vague syntactic examples and soundless uninformative screencasts. There are plenty of good resources for learning how to use the plug-in, but Flutter has notable feature that no one seems to know about: image manipulation.

See that little “Custom Options List” link? This is the only indication that image manipulation is even possible. Clicking this link brings down a long list of confusing commands.

Create Custom Field options

By skimming quickly through this list you will see a boatload of useful-sounding photo manipulations, such as rounded corners, or sizing and cropping images.

Flutter's image manipulation documentation

These commands can be used to write strings that will apply edits to images on the fly, allowing users to upload photos without breaking a template’s visual style. I’ll demonstrate how to use Flutter to associate an image with each post, and print out a heavily styled image.

Setting up the content type

I assume you already know the basics of flutter, so I’ll go through this quickly. If you know all about this, skip down to the code.

1. Create a new Write Panel in Flutter

New Panel Options

2. Set up basic options

Notice that I unchecked “Custom Fields.” This prevents the user from messing up the Flutter data, but you might want those turned on for development. Create a new field. Make it an image field and pay special attention to the “Name” textbox because you’ll need it later. I set this field to “Required” as every blog post will need an image.

image Field Options

3. Remember this screen?

We’ll get back to it later, but expand the “Custom Options List” and copy + paste everything into a blank .txt file for reference. You can’t get back to this list unless you make a new field. Leave everything blank and hit finish.

Custom Options List

Okay, we’ll need a test post or two, so make a dummy post using your new content type. Don’t forget to upload an image! When you’re done, we’ll move on to code.

4. Building the WordPress Template

If you look at the test blog post, you’ll see that the image you added isn’t there. We need to add it to the template file. Take a look at your loop:

[php]

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div>

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

<div class="entry">

<?php the_content(); ?>

</div>

<p class="postmetadata">Posted in <?php the_category(‘, ‘); ?></p>

</div>

<?php endwhile; else: ?>

<p>Sorry, no posts matched your criteria.</p>

<?php endif; ?>[/php]

We need to add the Flutter image code. Use Flutter’s “get_image” command with the name of the image field.

I’ll put it right before the content:

[php highlight="3"]

<div class="entry">

<?php echo get_image(‘post-image’) ?>

<?php the_content(); ?>

</div>[/php]

Voila! There’s the image. But it’s pretty blocky in the layout. If every post must have an image, all the images will need a standard look and feel. For this blog, I’ll make every photo a 600×400 grayscale image with rounded corners. That’s where Flutter’s image tools come into play.

Test post image from Flutter

There it is: the unaltered uploaded image.

5. Manipulate your images on the fly with Flutter and PHPThumb

Look through the “Custom Options List” and determine which operations to use. I’ll be using these:

w    = max width of output thumbnail in pixels
h    = max height of output thumbnail in pixels
f    = output image format ("jpeg", "png", or "gif")
zc   = zoom-crop. Will auto-crop off the larger dimension
so that the image will fill the smaller dimension


fltr = filter system. Call as an array as follows:
- "gray" (Grayscale) [ex: &fltr[]=gray]
remove all color from image, make it grayscale

Here’s the full code we’ll end up with:

[php highlight="3"]

<div class="entry">

<img src="<?php echo pt(); ?>?src=<?php echo get(‘post-image’); ?>&f=png>&w=600>&h=400>&zc=C>&fltr[]=ric|12|12>&fltr[]=gray" />
<br />
<?php the_content(); ?>

</div>[/php]

The final result of Flutter's image manipulation

The final result of Flutter's image manipulation

6. Cool, huh? Let’s break this down.

To generate its image manipulations, Flutter uses something called PHPthumb. Step one is to rewrite the query to pass through PHPthumb. Instead of using  get_image() we’ll need to create the tags ourselves and use the pt() function for the path to PHPthumb:

[php highlight="3"]

<div class="entry">

<img src="<?php echo pt(); ?>" />
<br />
<?php the_content(); ?>

</div>[/php]

…PHPthumb takes a query string with several parameters, the first of which is always the image source. We need to print out the name of the image using a Flutter get() command. Make sure to use the correct field name (mine is ‘post-image’) for the get().

[php highlight="3"]

<div class="entry">

<img src="<?php echo pt(); ?>?src=<?php echo get(‘post-image’); ?>" />
<br />
<?php the_content(); ?>

</div>[/php]

Finally, you can add the image filters. Each command is added onto the query with an “&” We’ll start by converting the image to a PNG using the “f“ command:

[php highlight="3"]

<div class="entry">
<img src="<?php echo pt(); ?>?src=<?php echo get(‘post-image’); ?>&f=png" />
<br />
<?php the_content(); ?>

</div>[/php]

Image manipulation commands must be separated by a “>” symbol. We’ll add on the commands to set the target width and height, separating them with a “>”:

[php highlight="3"]<div class="entry">

<img src="<?php echo pt(); ?>&src=<?php echo get(‘post-image’); ?>&f=png>&w=600>&h=400" />
<br />
<?php the_content(); ?>

</div>
[/php]

Next up is telling the system to crop the image so that it doesn’t have to stretch out to fit the dimensions. We’ll use the zc command with “C” as the attribute to crop evenly towards the photo center:

[php highlight="3"]

<div class="entry">

<img src="<?php echo pt(); ?>?src=<?php echo get(‘post-image’); ?>&f=png>&w=600>&h=400>&zc=C />
<br />
<?php the_content(); ?>

</div>[/php]

Finally, it’s time to apply the fancy filters. I’m using the ric filter for rounded corners and the gray  filter for desaturation. ric  takes values for the corner radius, and I used 12 for each:

[php highlight="3"]

<div class="entry">

<img src="<?php echo pt(); ?>?src=<?php echo get(‘post-image’); ?>&f=png>&w=600>&h=400>&zc=C>&fltr[]=ric|12|12>&fltr[]=gray" />
<br />
<?php the_content(); ?>

</div>[/php]

Finish by closing the image tag. Phew!

Additional Notes

This system is really easy and powerful once you get used to it. Play around a little and read through the list of all possible image manipulations; There’s a lot of good stuff there.

When creating an image field, the “Custom” textbox allows you to enter an image filter string. You should avoid using this because there’s NO WAY TO EDIT it and the original unaltered image will no longer be available for display.

An assigned image for each post would make a great image gallery; You could even superimpose the post’s title onto the thumbnail as a teaser.

List of all available filter options

Has anyone else used this Flutter feature?? Let us know how it went and what you used it for.

10 Responses

  1. Jeremy Woertink

    Awesome post! The same day I needed it you wrote the post. I agree with the documentation though. I have looked into a ton of other CMS plugins because sometimes I feel flutter just can't handle it. It seems it *can*, you just have to know what to look for :)

  2. JR

    You need to remove the space before “?src” and the space before the first &, your example doesnt work, I found taking out those spaces made it work.

  3. LukeSideris

    You are right, thank you. I corrected the article.

  4. Josphine Raatz

    I tried grabbing the feed for the RSS for your blog but it is not showing up in Google Chrome. Does anyone have any suggestions?

  5. rmbanfield

    Hi Josphine, just checked and the feed is working. http://www.freshtilledsoil.com/feed/ Might be a chrome bug.

  6. Jonathan

    Absolutely AWESOME, love it love it love it!! I have used fluter for a while now and steered clear of image manipulation as it was so damn confusing – this helps so much and works like a charm – thank you so much. I have an issue with the 'add image from url in a flutter field in the admin panel – do you think you could help?

  7. freshtilledsoil

    Sure, Jonathan. Have you tried to source an image from a URL and it didn't load or was there another issue that you encountered?

  8. shannon

    Doesn't this re-render the image on every request? Is there a way to render the image only once and use the cached version subsequently? i thought there'd be a way to utilize the 'custom' field from the admin, rather than putting it into the template.

  9. LukeSideris

    Hi Shannon,

    PHPThumb will cache every image it processes, so it will only need to render the first time. As for the 'custom' field, I can't seem to get it to function. As far as I can tell it does nothing — maybe someone else out there has made use of it

  10. goto11

    Hi there,

    Thanks for the tutorial, I used this to really get my head around using phpThumb. I’m still having one big problem however. I just can’t figure out where to put any images that I want to use as filters, for example, watermarks, masks, etc.

    Even though I have managed to get this working at one point on a site, I have since managed to break it! I got it to work by simply dumping my watermark in every location I could think of! When I tried to delete them one by one to discover the correct location, I now cannot get it to work again!

    One example of an overlaid png uses the following code:

    &fltr[]=over|img/fade.png

    Now where on earth do I create the folder called ‘img’?! I’ve tried in my html root folder, the wordpress root folder, the wp-content/files_flutter folder, the plugins/fresh-page/thirdparty/phpthumb/ folder, all with no success.

    Firstly, if some kind soul can point me to at least the right folder location, I can start seeing what is up! Also, does anyone know if there are any strict requirements on the format of the .png files? At the moment, mine are transparent png-24 files, created in photoshop, but like I said I’m sure I had these working before!

    Thanks for any help anyone can offer!

Leave a Reply

By submitting a comment here you grant Fresh Tilled Soil a perpetual license to reproduce your words and name/web site in attribution. Inappropriate comments will be removed at admin's discretion.