Created
June 23, 2014 07:28
-
-
Save apphp/76d4fc3eaa21d982921f to your computer and use it in GitHub Desktop.
Change Image Properties 'On-The-Fly'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. source: http://www.apphp.com/index.php?snippet=php-change-image-properties-on-fly | |
| <?php | |
| $postText = '..includes html + image tags'; | |
| // find first image and redo it | |
| preg_match_all('/<img[^>]+>/i', $postText, $images); | |
| $postThumbnail = isset($images[0][0]) ? $images[0][0] : ''; | |
| $postThumbnail = preg_replace('/(width|height|style)="*"/', '', $postThumbnail); | |
| $postThumbnail = preg_replace('/<img/', '<img class="blog-post-thumbnail"', $postThumbnail); | |
| echo $postThumbnail; | |
| echo $postText; | |
| ?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment