Google it! / Yahoo it! / Bing it! / Ask it!
It took me a while to try and rephrase the title, but to no avail.
Using the image.php script from my last article, I will read database entries and replace all tags with clickable thumbnails. All you need to do is have a database entry with an
tag.
Read the database (I’m sure you know how to do this):
<?php $sql = "SELECT * FROM articole WHERE post_id = '$id'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); ?>
Extract the image and preg_replace
it:
<?php $content = $row['content']; $content = preg_replace("/<img[^']*?src=\"([^']*?)\"[^']*?>/", '<a href="$1"><img src="image.php/$1?width=150&height=150&quality=100&image=http://www.domain.com/images/$1" alt="" /></a>', $content); echo $content;?>
The script wraps any image found in the content with an anchor and the image.php caller script. I guess you recognize the parameters from the previous article. What does $1 do? It replaces the first parameter of preg_replace
with itself. Pretty easy, isn’t it?