Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#25 2011-04-09 20:30:45

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: Article thumbnail image from YouTube/Flickr code found in Body

Right. I was trying to avoid needing a third pattern, but I overlooked the fact that iframe is all one element. Probably best to add in a third pattern.

$img_pattern = '#^(.*?)<img\s[^>]*src=(\'|")(.+?)\2#s';
$iframe_pattern = '#^(.*?)<iframe[^>]+http://www.youtube.com/embed/([A-Za-z0-9\-_]+)#s';
$object_pattern = '#^(.*?)<object.+?>.+?http://www.youtube.com/v/([A-Za-z0-9\-_]+).+?</object>#s';

Determining which comes first is trickier. There’s probably a cleaner way to do this, but this should work:

if ( $first_img )
{
	if ( $first_iframe )
	{
		if ( $first_iframe < $first_img )
			$first_img = null;
		else
			$first_iframe = null;
	}
	if ( $first_object )
	{
		if ( $first_object < $first_img )
			$first_img = null;
		else
			$first_object = null;
	}
}
if ( $first_iframe && $first_object )
{
	if ( $first_iframe < $first_object )
		$first_object = null;
	else
		$first_iframe = null;
}

Then something like:

if ( $first_img ) echo ...
if ($first_iframe) echo ...
if ($first_object) echo ...

Code is topiary

Offline

#26 2012-07-30 02:35:31

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

Hello! I am revisiting this code and was wondering how I can make the results into a variable? Something like: <txp:variable name=“bodyimg” /> I want to be to test if there are any embedded images in the article body, then output different code in my article form if there aren’t.

Scratch the above. I have bigger issues now because I’m using hak_tinymce to insert images into the body of articles. If the appearance of an image is edited when inserting an image into an article, the style tag shows up between <img and src=”, like this: <img style=“float:left;” src=”. The regex code for finding images doesn’t work (see posts in this thread above for sample code of regex pattern). I’m starting to wonder if I need to use something other than regex to find the first image or video? Or, is there some regex that will find the src regardless of the attribute order? I’ve searched the web for regex patterns, but can’t find any that work here. I know it isn’t the ideal situation for regex, but I’m not sure how else to proceed. Anyone have advice? Thanks.

Last edited by photonomad (2012-07-30 04:49:24)

Offline

#27 2012-07-30 14:32:42

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

I found a solution. :) Inspired by this post on Stackoverflow, I gave phpQuery a try. The code below works great for finding the first image src in an article body and storing it as a variable. (I am postponing the youtube test for now).

Call at top of page: <txp:php>require 'phpQuery-onefile.php';</txp:php>

<txp:php>
global $variable;
$texthtml = $thisarticle['body']; 
$pq = phpQuery::newDocumentHTML($texthtml);
$img = $pq->find('img:first');
$src = $img->attr('src');
$variable['bodyimg'] = $src;
</txp:php>

<txp:if_variable name="bodyimg" value="">nope<txp:else /><txp:variable name="bodyimg" /></txp:if_variable>

My next challenge is to introduce the logic of finding the first youtube video id or first img src, whichever comes first in the article body.

Offline

#28 2012-07-31 21:01:59

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: Article thumbnail image from YouTube/Flickr code found in Body

You could give a try to etc_query:

<txp:variable name= "bodyimg" value='<txp:etc_query data="{?body}" query="//img[1]@src?" />' />

Offline

#29 2012-08-01 17:16:41

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

Thanks, etc! I’m trying out your plugin now and there is a problem somewhere (using the txp:variable code you included in the post above). I’m not sure how to fix it:

Tag error: <txp:etc_query data=”{?body}” query=”//img1@src?” /> -> Warning: DOMXPath::evaluate() [domxpath.evaluate]: Invalid expression on line 194
textpattern/lib/txplib_misc.php(653) : eval()’d code:194 DOMXPath->evaluate()
textpattern/publish.php:1188 etc_query()
textpattern/publish.php:1100 processTags()
textpattern/lib/txplib_misc.php:1053 parse()
textpattern/publish.php:1188 splat()
textpattern/publish.php:1100 processTags()
textpattern/publish.php:847 parse()
textpattern/publish.php:964 doArticles()
textpattern/publish.php:954 parseArticles()
textpattern/publish.php:1188 article_custom()

Offline

#30 2012-08-01 19:03:19

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: Article thumbnail image from YouTube/Flickr code found in Body

Yes, sorry, should be <txp:etc_query data="{?body}" query="//img[1]/@src">{?}</txp:etc_query>.

Edit: or <txp:etc_query data="{?body}" query="string(//img[1]/@src)" />

Last edited by etc (2012-08-01 19:09:03)

Offline

#31 2012-08-01 19:15:15

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

Thanks! No longer throwing erros and it is finding an image. However the context is an article list and instead of finding the first image from each article body, it is only finds the first image from the first article body, then it inserts that one same image for every one of the subsequent articles as well. It’s like it isn’t observing the fact that it is inside and article_custom form.

Here is the code I’m using:

<txp:article_custom section="sectionnamehere" limit="5">
<txp:etc_query data="{?body}" query="//img[1]/@src"><txp:variable name="bodyimg" value="{?}" /></txp:etc_query>
<txp:if_variable name="bodyimg" value="">
<txp:permlink><img src="http://www.gingermanley.com/thumbs/timthumb.php?src=<txp:variable name="bodyimg" />&w=160&q=100" /></txp:permlink>
<txp:else />
some other content
</txp:if_variable>
</txp:article_custom>

Offline

#32 2012-08-01 20:36:29

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: Article thumbnail image from YouTube/Flickr code found in Body

No, it looks for images in thisarticle['body'], which is context aware. Are you sure <txp:else /> is not misplaced? Shouldn’t it be (I have modified the quotes too)

<txp:article_custom section="sectionnamehere" limit="5">
<txp:variable name="bodyimg" value='<txp:etc_query data="{?body}" query="string(//img[1]/@src)" />' />
<txp:if_variable name="bodyimg" value="">
some other content
<txp:else />
<txp:permlink><img src='http://www.gingermanley.com/thumbs/timthumb.php?src=<txp:variable name="bodyimg" />&w=160&q=100' /></txp:permlink>
</txp:if_variable>
</txp:article_custom>

Additionally, if you use it in some article, this article should be excluded from the list, for example by assigning it a different section. Otherwise, <img src="http://www.gingermanley.com/thumbs/timthumb.php?src=<txp:variable name="bodyimg" />&w=160&q=100" /> will be matched too.

Edit: I also have changed the variable definition.

Last edited by etc (2012-08-01 20:38:44)

Offline

#33 2012-08-01 21:22:07

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

No, it looks for images in thisarticle['body'], which is context aware. Are you sure <txp:else /> is not misplaced? Shouldn’t it be (I have modified the quotes too)

Oops! Sorry… I mistyped and accidentally switched the <txp:else /> logic around while simplifying my code for this discussion.

Additionally, if you use it in some article, this article should be excluded from the list, for example by assigning it a different section. Otherwise, <img src="http://www.gingermanley.com/thumbs/timthumb.php?src=<txp:variable name="bodyimg" />&w=160&q=100" /> will be matched too.

The image tag with the bodyimg variable is only used in the custom_article form in the page template and is not in any of the article bodies.

I’m still having trouble with the same thumb showing up for every article in the list. In reality, the image that you see repeated in this example is only in the body of the first post and there are zero images in any of the next four posts. On the example/test page (section name is test) I have the following code in the page template:

<txp:if_article_list>
<txp:article_custom section="assisted-loving" limit="5">
<txp:etc_query data="{?body}" query="//img[1]/@src"><txp:variable name="bodyimg" value="{?}" /></txp:etc_query>
<div class="entry clearfix">
<h2><txp:permlink><txp:title /></txp:permlink></h2>
<txp:if_variable name="bodyimg" value="">
some other content
<txp:else />
<txp:permlink><img src='http://www.gingermanley.com/thumbs/timthumb.php?src=<txp:variable name="bodyimg" />&w=160&q=100' /></txp:permlink>
</txp:if_variable>
</div>
</txp:article_custom>
</txp:if_article_list>

Given what is displaying on the example/test page, it seems like the tag is somehow storing the image from the first post.

This page and the home page show the same articles listed using the phpquery code that I posted earlier in this thread.

I’d love to use your plugin instead of bringing in the phpquery file. I just don’t know why it is repeating in my test.

Offline

#34 2012-08-01 21:38:01

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: Article thumbnail image from YouTube/Flickr code found in Body

I think it’s because of

<txp:etc_query data="{?body}" query="//img[1]/@src"><txp:variable name="bodyimg" value="{?}" /></txp:etc_query>

If an article has no images, then query brings nothing, and etc_query does not process the content (it passes to the else part). That’s why I have changed it to

<txp:variable name="bodyimg" value='<txp:etc_query data="{?body}" query="string(//img[1]/@src)" />' />

Could you try this?

Offline

#35 2012-08-01 21:42:46

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

Now it works perfectly! I completely missed that. Thank you for helping me figure it out and thank you creating this great plugin!

Offline

#36 2012-08-01 21:48:56

etc
Developer
Registered: 2010-11-11
Posts: 5,195
Website GitHub

Re: Article thumbnail image from YouTube/Flickr code found in Body

Glad to know you like it, thank you!

Edit: and your photos are great!

Last edited by etc (2012-08-01 21:50:27)

Offline

Board footer

Powered by FluxBB