How to Show First Image In Post as Thumbnail In yii

i want to Using First Image In Post as Thumbnail In yii. but i don’t…

do you helf me…

thanks for your helf.

scan the post contents for the tag img and index them.

Use that Image as thumb for the post and store its information in a db field

i don’t understand .Can you give me a good example?

helf me…

You will need to be a lot more specific in what you actually want to do, but here is a quick way to get the url of the first image in a piece of text.




$post = 'some text with two pictures: <img src="http://www.example.com/image1.jpg" /> <img src="http://www.example.com/image2.png" />';


if (preg_match('#<img src="([\w./-_]+)"#i', $post, $matches))

    $url = $matches[1];



In this example, the variable $post contains some HTML that you want to scan. In your application, you might fill the variable by rendering a view to a variable, for example:


$post = $this->render('myView', array(), TRUE);

The if-statement uses a regular expression to find the first image tag. From that tag, it assigns the src url to an array called $matches. $matches[1] contains the url. Note that this regular expression only works if the image tag starts exactly like this:


<img src="url_to_image"

If you have any other attributes between img and src, or spaces around the equals sign, it won’t work. You need a more sophisticated regular expression in that case. Unfortunately my regular expression knowledge is a bit rusty, so someone else will have to help you with that.

If you have the url to the image, you can do anything you want with it. You mentioned making it a thumbnail, but the actual implementation of that depends a great deal on what exactly you want to achieve. Therefor, it makes little sense to go into that unless you can describe what you want to do in more detail.

thanks jodev

but i don’t understand …i’m newbie.

Can you give me a demo? :unsure:

I can give it a try, but could you first tell me what in particular you need help with? Do you need me to clarify what I wrote above more, or do you need more general help?

Demo:

1 - Create a new action in your controller

2 - Have some custom entries as variables such as


$posts[1]='This is demo 1';$posts[2]='This is demo 2';

3 - Use the foreach loop as





foreach($posts as $post){

$url='';

if (preg_match('#<img src="([\w./-_]+)"#i', $post, $matches)){

    $url = $matches[1];

}


$post_render = $this->render('myView', array('post'=>$post,'timg'=>$url), TRUE);


}




then in myView have the code as




if(!empty($timg)){

echo '<img src="'.$timg.'">';

}



:-[ :-[ :-[ i can’t do it … why?


$posts= CHtml::encode($model->hanhtrinhTour);

//$posts= $model->hanhtrinhTour;

echo $posts."</br>";

$matches= array();

//foreach($posts as $post){

$url='';

if (preg_match_all('#<img src="([\w./-_]+)"#i', $posts, $matches)){

    $url = $matches[1];

}


if(!empty($url)){

echo '<img src="'.$url.'">.</br>';

echo "</br>";

}

It’s a good try and you are very close. Let me first post your code with corrections so that it should work, then I’ll tell you why yours failed.




$posts= $model->hanhtrinhTour

echo $posts . "</br>";


$matches = array();

$url = '';


if (preg_match('#<img src="([\w./-_]+)"#i', $posts, $matches))

    $url = $matches[1];


if (!empty($url))

{

    echo '<img src="' . $url . '">.</br>';

    echo "</br>";

}



There are two things that I fixed up. First, I removed the CHtml::encode() bit. When you encode the text, any html will no longer work but will be printed as-is. So instead of the html source being…


<img src="http://www.example.com/img1.png />

… the source code with encoding will read:


&lt;img src=&quot;http://www.example.com/img1.png&quot; /&gt;

Result: you lose all formatting in the text, no pictures are shown and the regular expression used to find the first picture url. Secondly, I changed preg_match_all() to preg_match). We are only interested in the first url, so pret_match() is all we need. See http://php.net/manual/en/function.preg-match.php and http://php.net/manual/en/function.preg-match-all.php for more details.

One last important note. I removed encode(), but that leaves a possible vulnerability. You need a way to filter dangerous text (scripting code) from valid html. I haven’t used it myself yet, but you might try looking into CHtmlPurifier.

Hope that helps. :)

thanks All :D :lol:

I enjoyed the reading of jodev and PeRoChAk answers. +1 to all for such a friendly and detailed help to newcomers ;)

;D yah i also enjoyed the show…

but it will help me when i need it, Thanks to jodev ;D