Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-04-16 01:16:32

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Need help with PHP to get the first image url from the Body_html

I am really shaky with PHP, but I want to do the following in one of my article forms:

<txp:if_article_list>
<txp:if_article_image>
will use the article image thumb if there is an article image in the Article Image field for the article – no big deal… got this covered
<txp:else />
<txp:php>
I want to test IF image code even exists in the article’s Body_html:
if <img src=“the_url_here” /> exists in Body_html then, give me the url for the first occurrence (I already have some php code to resize the image into a thumb once I have the url — I just don’t know how to get the url out of Body_html)
</txp:php>
</txp:if_article_image>
</txp:if_article_list>

I’ve been looking around the PHP manual and I think preg_match() might be the way to go to get the img url out of the Body_html…. but I don’t really know what I’m doing.

Any suggestions / code examples would be super greatly appreciated!

heck, maybe this would make a cool plugin once it is figured out! :)

Offline

#2 2010-04-16 01:54:49

rsilletti
Moderator
From: Spokane WA
Registered: 2004-04-28
Posts: 707

Re: Need help with PHP to get the first image url from the Body_html

Given that your image tag follows the format < img src=“whatever” / > the following code shells out the first image tags url for me. I do regex with a book in my lap so take this as is with a grain of salt.

$u = preg_match('@<img src=([^<]+)/>@',$thisarticle['body'] , $matches);
 if($u) 
  {
	$theurl = $matches[1];
	dmp($theurl);
  }

Offline

#3 2010-04-16 02:42:29

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: Need help with PHP to get the first image url from the Body_html

Thanks so much rsilletti!

The only issue I run into with your suggestion is that it outputs more than just the url… do you happen to know how to cut out the other stuff?

example of what it returns:

“http://www.website.com/image.png” class=“right” alt=”“

Offline

#4 2010-04-16 03:26:39

rsilletti
Moderator
From: Spokane WA
Registered: 2004-04-28
Posts: 707

Re: Need help with PHP to get the first image url from the Body_html

This way you can pick your pieces from the array returned by explode.

$u = preg_match('@<img src=([^<]+)/>@',$thisarticle['body'] , $matches);
  if($u) 
  {
	$theurl = $matches[1];
	$var = explode(" ",$theurl);
	dmp($var[0]);
  }

Last edited by rsilletti (2010-04-16 03:28:00)

Offline

#5 2010-04-16 04:33:35

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: Need help with PHP to get the first image url from the Body_html

That works great!

now I just have to make it output without the <pre> tags and quotes.

Update: figured it out

<txp:php>
$u = preg_match('@<img src=([^<]+)/>@',$thisarticle['body'] , $matches);
  if($u) 
  {
	$theurl = $matches[1];
	$var = explode(" ",$theurl);
	echo trim($var[0], "\"");
  }</txp:php>

thanks again rsilletti!

In case anyone is curious, I’ll try to post my full code with the image resize when I get it done.

Last edited by photonomad (2010-04-16 04:51:57)

Offline

#6 2010-04-16 14:56:02

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: Need help with PHP to get the first image url from the Body_html

Ok, here it is! This is the code I am using to find an article image (wherever it might be) to make into a thumbnail in the article_list part of my article form.

Note: I am using this script by Wes Edling to resize images on the fly… it is called at the beginning of my page template!

<txp:if_article_image>

// test to see if the article image is a number vs. a url – if it is a number, then I use the id to form the url to resize it (I’m doing this just to be sure the thumb will be the correct, consistent size (sometimes the thumb size/crop settings are accidentally changed in the images tab and I want to avoid any confusion).

<txp:smd_if field="article_image" operator="isnum">
<txp:permlink><img src="<txp:php>
$thisimg = "images/";
$thisimg .= htmlspecialchars($GLOBALS['thisarticle']['article_image']);
$thisimg .= ".jpg";
echo resize("$thisimg",array("w"=>120));
</txp:php>" class="left" /></txp:permlink>

// if the image isn’t a number, then use the following code to downsize the url image

<txp:else />
<txp:permlink><img src="<txp:php>
$thisimg .= htmlspecialchars($GLOBALS['thisarticle']['article_image']);
echo resize("$thisimg",array("w"=>120));
</txp:php>" class="left" /></txp:permlink>
</txp:smd_if>

// if there isn’t an article_image, then look to see if there are any image urls in the body and downsize the first image found into a thumbnail

<txp:else />
<txp:php>
$u = preg_match('@<img src=([^<]+)/>@',$thisarticle['body'] , $matches);
  if($u) 
  {
	$theurl = $matches[1];
	$var = explode(" ",$theurl);
	$thisimg = trim($var[0], "\"");
	$sized = resize("$thisimg",array("w"=>120));
	echo "<img src='".$sized."' class='left' />";
}
</txp:php>

</txp:if_article_image>

Thanks to rsilletti for the regex/explode code and to Bloke for smd_if!

Offline

Board footer

Powered by FluxBB