How to get the intro image of a joomla article - php

I am trying to customize the the category list of my website. So I have a menu option that shows the list of all article is a certain category.
In this list I would like to show the into image of the article too. But unfortunately I cannot find the path of this intro image of each article to put it in the '' tag.
This code takes the first image in the article. Which is not what I want.
<?php
$text = $article->introtext;
if(preg_match('/<\s*img[^>]+>/Ui', $text, $matches)){
$image=$matches[0];
$text = str_replace($image,'',$text);
}
if ($matches) {
$image = str_replace('img src="','img width="150px" src="/demo/persianlondon/',$image);
$image = str_replace("caption","", $image);
echo '' . $image . '';
}
else {echo '<img width="150px" src="http://goo.gl/k47rNN" class="defaultArticleImage" />';}
?>
So I need to get the intro image url and set it for $image.
Any idea to do it?

If you are talkin about blog view of a category, this code will write out the intro image filename with path, if you put it in file:
jroot/component/com_content/views/category/tmpl/blog.php or in the template overrided blog.php file.
$article_items = $this->get('lead_items');
foreach ($article_items as $item) {
$imageObj = json_decode($item->images);
echo $imageObj->image_intro;
}
other variables of $imageObj:
stdClass Object
(
[image_intro] =>
[float_intro] =>
[image_intro_alt] =>
[image_intro_caption] =>
[image_fulltext] =>
[float_fulltext] =>
[image_fulltext_alt] =>
[image_fulltext_caption] =>
)

Related

displaying html from php

this has to do with wordpress and php. i have a function that takes an array of names from json. for each name i loop through, it creates a div with the name and an image inside. this div is then attached to a post and displayed on the front end. the problem is the image doesn't display correctly due to a 404 error. when i looked at the image source, the path to the image looked like this:
<img src="\"http://localhost/card-store/wp-content/themes/card-store-theme/images/baseball/team2.jpg\"">
clearly the path is broken so the 404 makes sense. seems like php is trying to escape some quotes, so i tried removing this with the str_replace, and as a shot in the dark i also tried html_entity_decode. also tried an absolute path to my images but that did not work either. when i refresh the page, the images appear fine, so i think its something to do with it not compiling right away? if that is true, how can i get it to display correctly without refreshing the page?
function test_function() {
if ( isset($_POST) ) {
$nameData = $_POST['nameData'];
//Strip any double escapes then use json_decode to create an array.
$nameDecode = json_decode(str_replace('\\', '', $_POST['nameData']));
//loop through names array and create a container for each
$html_string = "";
foreach ($nameDecode as $keyIndex => $name) {
$html_string .= '<div class="team-container team-container--inline col col--md-2 col--lg-2 col--xl-2"><img src="'. get_template_directory_uri() .'/images/baseball/team' . $keyIndex . '.jpg"> <p>'.$name.' <p /></div>';
}
echo ( $html_string);
//$html_final = str_replace('\\', '', $html_string);
$html_final = html_entity_decode($html_string);
// update teams post
if($html_string != "") {
$my_post = array(
'ID' => 9,
'post_content' => $html_final,
);
// Update the post into the database
wp_update_post( $my_post );
} else {
echo 'html string is empty!';
}
}
die();
}
If you want to remove the slashes try this.
$str = "Is your name O\'reilly?";
// Outputs: Is your name O'reilly?
echo stripslashes($str);
$nameData = $_POST['nameData'];
$stringObject = json_decode(stripslashes($nameData));
For json encode
json_encode($html_string, JSON_UNESCAPED_SLASHES);

how to pass two values in one variable through associative array in PHP

I have a UberGallery.php from the UberGallery which is I am using.
For each image popup there is image name(e.g 4488826 6f061c99ec b d) displayed at the bottom of image which is taking from UberGallery.php
// Loop through array and add additional info
foreach ($dirArray as $key => $image) {
// Get files relative path
$relativePath = $this->_rImgDir . '/' . $key;
$galleryArray['images'][htmlentities(pathinfo($image['real_path'], PATHINFO_BASENAME))] = array(
'file_title' => str_replace('_', ' ', pathinfo($image['real_path'], PATHINFO_FILENAME)),
'file_path' => htmlentities($relativePath),
'thumb_path' => $this->_createThumbnail($image['real_path'])
);
}
file_title from above code is affecting that title to be changed. Images are displayed like below in defaultGallery.php
<?php if (!empty($images) && $stats['total_images'] > 0): ?>
<ul id="galleryList" class="clearfix">
<?php foreach ($images as $image): ?>
<li><img src="<?php echo $image['thumb_path']; ?>" alt="<?php echo $image['file_title']; ?>"/></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No images found.</p>
<?php endif; ?>
Now I want to show the image name(4488826 6f061c99ec b d) and email address(aaa#gmail.com) both at bottom of the image for each image popup.
if I changed in above file_title's str_replace function to get email address from csv file then nothing gets appeared.
gallery.csv is having two field like 4488826 6f061c99ec b d aaa#gmail.com. These two values I want to pass to file_title so that they gets appear in image popup.
$file_handle = fopen("gallery.csv", "r");
while (!feof($file_handle)) {
$lines_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
// Loop through array and add additional info
foreach ($dirArray as $key => $image) {
// Get files relative path
$relativePath = $this->_rImgDir . '/' . $key;
$galleryArray['images'][htmlentities(pathinfo($image['real_path'], PATHINFO_BASENAME))] = array(
'file_title' => str_replace('_', $lines_of_text[1], pathinfo($image['real_path'], PATHINFO_FILENAME)),
'file_path' => htmlentities($relativePath),
'thumb_path' => $this->_createThumbnail($image['real_path'])
);
}
How should I pass name and email to above file_title to get required result?
Or is there any other way to do this?
Please help me to find out the solution.
try this one like
$galleryArray['images'][htmlentities(pathinfo($image['real_path'], PATHINFO_BASENAME))] = array(
'file_title' => ' ',
'file_path' => htmlentities($relativePath),
'thumb_path' => $this->_createThumbnail($image['real_path'])
);

Wordpress NextGen Gallery - Only show if gallery has images

I have created 4 empty galleries(using Nextgen) and I have one list of static image links. The empty galleries have been created so that if someone wants to add images in them later, that gallery will show up in place of the static image links.
The static image links should up if no images are in the Nextgen gallery, but if there are images in the NextGen gallery - use those in place of the static image links.
This is the code I am using for the Nextgen gallery that I'm calling in the page template:
<?php global $nggdb;
$gallery = $nggdb->get_gallery(1, 'sortorder', 'ASC', true, 0, 0);
foreach($gallery as $image) {
echo '<img src="';
echo $image->imageURL;
echo '" />';
}
?>
I know I need to check to see if the specified gallery has images, and then if not, do an else statement, but I'm not exactly sure how to do this.
Any help is greatly appreciated. Thank you.
You could try something like this:
$gallery = $nggdb->get_gallery(1, 'sortorder', 'ASC', true, 0, 0);
if ( !empty($gallery) && is_array($gallery) && count(gallery) > 0 ) {
foreach($gallery as $image) { ... }
} else {
//show static image links
}

Adding array to a model file in Codeigniter

I have the following array in my view file (which works):
<?php
$images = array(img("slideshow/demo1.png", "one"),img("slideshow/demo2.png", "two"),img("slideshow/demo4.png", "three"));
$captions = array("first image", "second image", "third image");
foreach (array_combine($images, $captions) as $image => $caption ) {
echo "<li>", $image, "<div class=\"orbit-caption\">",$caption,"</div></li>";
}
?>
However I wish to use the same foreach in a slideshow file across multiple pages with different images displaying so i thought the simplest way would be to create a model page for each page that required different data and having the images and captions in an array as above. I have tried searching how to do this but all the solutions I have found relate to being in a database which is unfortunately not an option.
Also I would like to code it so that if there is not image caption the caption code does not run ie:
"<div class=\"orbit-caption\">",$caption,"</div>
The options I have tried either I get a blank semi transparent box ie it puts down:
<div class="orbit-caption"></div>
Or I getting nothing if I have a caption or not.
I understand a small amount of php but am more of a designer and am fairly new to MVC so will not be offended if anyone has a solution in an idiots guide :) just trying to learn. If anyone has a better solution then please share
Create a helper in application/helpers folder and name it slideshow_helper.php, Now, in slideshow_helper.php write :
<?php
function get_image_array(){
$images = array(img("slideshow/demo1.png", "one"),img("slideshow/demo2.png", "two"),img("slideshow/demo4.png", "three"));
$captions = array("first image", "second image", "third image");
return array_combine($images, $captions);
}
?>
In your controller function from where you load the view for slideshow page, write:
<?php
$data = array();
$this->load->helper('slideshow');
$data['slideshow'] = get_image_array(); #will return the image array from the helper
$this->load->view('view', $data); #load the image array in the view
?>
In your View:
<?php
foreach ($image as $imageSrc => $caption ) {
if($caption != ""){
echo "<li>", $imageSrc, "<div class=\"orbit-caption\">",$caption,"</div></li>";
}
}
?>

Get last section from echo and store in variable PHP

I've a function that will echo the URL of the image from the content of Wordpress.
I got the function work now no problem
// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') { //it can be thumbnail or full
if ($postid<1)
$postid = get_the_ID();
$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
if ($thumb != null or $thumb != '') {
echo $thumb;
}
elseif ($images = get_children(array( //If you upload an image function gets first image
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => '5',
'post_mime_type' => 'image', )))
foreach($images as $image) {
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
?>
<?php echo $thumbnail[0]; ?>
<?php }
else { //If you don't upload or declare as thumb custom field func. gets custom (default) image
echo get_bloginfo ( 'template_directory' ); //same as wp-content/themes/your-theme/
echo '/images/image-pending.gif'; // Put this image into your themes images folder and set the path here
}
}
The only problem now is that the <?php echo $thumbnail[0]; ?> if there are more than one image it will echo all of them something like this
<img src=" http://applesiam.com/wp-content/uploads/2555-05-02_19h14_34-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h14_11-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_43-150x123.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_20-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_17-150x150.png ">
As you can see it just separated by some spaces.
Now I just want to have the last image if there is more than one image in the $thumbnail
I'm not really expert with PHP as my semester for PHP course will start next week.
Thanks in advance for any suggestion going to be.
Try:
$imgSrc = end((explode(' ', trim($imgSrc)));
Where $imgSrc is the value you put into <img src="!!!==>>here<<==!!!">.
quickly typed, w/o any warranty. Should leave single URLs intact, and if multiple ones separated by space(s) will take the last.
Try this instead.
echo trim($thumbnail[sizeof($thumbnail)-1]);

Categories