Skip to content
Advertisement

WordPress – Blur Image on Upload

So I’m following the example given here (which I modified to only blur, no watermark), to make a blurred image in WordPress on upload. The problem is, that if the uploaded file is the exact same size, or smaller, than the set size, then WordPress will not generate an image, and hence no blurred one will be made.

I tried using a isst($meta['sizes']['background-image-blurred']['file']) to determine if one was made, and if not then copy() the source file, but then no WordPress “metadata” would be generated for the image (for non-WordPress people, the metadata is different than what you think), so it would give height/width undefined problems when displaying using wp_get_attachment_image.

So I’m convinced using wp_get_attachment_image hook as shown below is probably the wrong way to do this. It probably needs to happen earlier in the image upload process.

Any ideas on how to best get this working?

JavaScript

Advertisement

Answer

Unfortunately wp hasn’t got a filter to force a size so what you can do is hook in after and resize your image if not created and pop it into the metadata.

I haven’t got imagick so you will have to try these functions yourself, but you have the correct process above, you just need to update the filename and type in the array below. PS don’t output anything within the filter!

JavaScript

Updates

  1. This is designed to only catch where your existing filter has failed to create your blurred custom size otherwise it does nothing. You should still include your original filters. You may have an issue in the original code: You are deleting the original file in your filters and this will cause issues as there is a postmeta field called ‘_wp_attached_file’ that will need updating. I have included a note below on this.

  2. The filter catches the metadata before saving so any changes are also going to be saved once you return the $metadata. If you look at the source code: https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-admin/includes/image.php#L72 here you can see exactly how it works. I’ve also confirmed using wp4.3

  3. I have attempted to insert the imagick functions you need below. I havent tested as i dont actually have it installed anywhere. (imagemagick is actually a wonderful opensource program but very server intensive). Try this function in place of the one above:

    JavaScript

update to prevent the image stretching out smaller images replace the imagick code with this.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement