Change Image Properties 'On-The-Fly'

Sometimes you may need to change image properties 'on-the-fly'. The best example is when you want to display blog post short description, retrieve the first image and try to turn it into the thumbnail: change some properties and add a special class name.

[View All Snippets]
Show Plain Text »
  1. <?php
  2. $postText = '..includes html + image tags';
  3.  
  4. // find first image and redo it
  5. preg_match_all('/<img[^>]+>/i', $postText, $images);
  6. $postThumbnail = isset($images[0][0]) ? $images[0][0] : '';
  7. $postThumbnail = preg_replace('/(width|height|style)="*"/', '', $postThumbnail);
  8. $postThumbnail = preg_replace('/<img/', '<img class="blog-post-thumbnail"', $postThumbnail);
  9.  
  10. echo $postThumbnail;
  11. echo $postText;
  12. ?>
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.