I am trying to scrape a few images from a website by using the PHP Simplehtmldom library. Normally the image url is found in the 'src' but in this case the image url is found in the 'data-url'. I am having trouble to access the data-url value and I was hoping some could help me out.
Let's say I have the following code:
<section id="imagesContainer">
<div class="img">
<img data-src="https://example.com/image1.jpg" alt="imageAlt1">
</div>
<div class="img">
<img data-src="https://example.com/image2.jpg" alt="imageAlt2">
</div>
<div class="img">
<img data-src="https://example.com/image3.jpg" alt="imageAlt3">
</div>
</section>
I attempted to extract the image urls from the data-src with the following code but it doesn't return the image url:
foreach($html->find('#imagesContainer') as $imagesContainer) {
foreach($imagesContainer->find('img') as $image) {
echo $image->data-src;
}
}
How can I extract the image url from the data-src? Is it possible with the simplehtmldom or do I need a regex?
many thanks for your suggestion. This code, using XPath, works to extract the value of the data-url:
$image->getAttribute('data-src')
Related
I have below code but don't want to encode the image as Base64 just link the image and get it with same function.
<img src="data:image/png;base64,'.base64_encode(file_get_contents(get_field('email_logo', 'option')['url'])).'" alt="The Domain" />
How to call the function inside img src?
You've not shown us how this bit of code connects to the PHP around it. Assuming that there is a closed PHP block before hand, rather than this appearing in a print or heredoc...
?>
...
<img src="data:image/png;base64,
<?php
print base64_encode(file_get_contents(
get_field('email_logo', 'option')['url']
));
?>'" alt="The Domain" />
If you want only the link and it's correct as it is:
<img src="'.get_field('email_logo', 'option')['url'].'"/>
I have Below code in my header.html
<div class="col-md-3">
<div class="logo">
</div>
</div>
But it's not showing my png image in header, Is the way adding image path in div is wrong?
<img src="http://example.com/themes/default/images/logo.png">
That is how you use an image. You are placing a link to the image.
Also, you are not seeing anything because your <a></a> tags are empty. If you write something between them, you can click on it, to see the image you linked.
Add Image inside the img tag not in a tag
<img src="http://example.com/themes/default/images/logo.png">
You should do is this way. img tag is used to display image.
Good practice is to link the logo to the home page of the website.
<div class="col-md-3"><div class="logo"><img src="http://example.com/themes/default/images/logo.png" alt="Site Name or Logo"/></div></div>
put link or path in img tag source(src) not anchor tag.
<div class="col-md-3">
<div class="logo">
<img src="http://example.com/themes/default/images/logo.png" alt="">
</div>
</div>
I have the following html code that tried to place in my WordPress page.
html:
<div class="hovereffect">
<img src="<?php echo get_template_directory_uri() ?>phone.jpg" >
<div class="overlay">
<h2>Hover effect 9</h2>
<a class="info" href="#">link here</a>
</div>
</div>
At the moment everything is in the site except the image that does not show.
How can I use this code WordPress in a way that it can display the image?
I think you forget to tell which place it should get the images from. And you are also forgetting a semicolon after the get_template_directory_uri();.
This is an example, but here i'm telling which folder to get the image from:
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/your_image.jpg">
you can do that but it is not a good practice to paste this code as it is in WordPress editor,
upload this image in media and get link of that image
Edit page, select text mode from top right corner of your editor and paste code these i.e
<div class="hovereffect">
<img src="http://example.com/wp-content/uploads/2016/07/img.png" >
<div class="overlay">
<h2>Hover effect 9</h2>
<a class="info" href="#">link here</a>
</div>
</div>
Here is good practice create a template for that page and write there your code.
Image replace with feature image
Heading with page title.
Detail with page content
link with page permalink.
Not enough reputation to leave a comment so I will leave this as an answer instead.
Assuming phone.jpg is at the root of your theme, you're forgetting the / (slash) before phone.jpg.
It should be
<img src="<?php echo get_template_directory_uri(); ?>/phone.jpg" >
PHP won't get parsed inside a page. Just upload the image to the WordPress media library and link to it directly.
I'm using Visual Composer in Wordpress. I have created own custom element.
Now I'm trying to figure out how to get url from link.
If I print {$a['link']} I get url and title:
url:http%3A%2F%2Fwww.url.com|title:url_title|
This should be pretty easy(?) but just could not figure it out.
return "<div class='icon_text_box'>
<a href='{$a['link']}'>
<div class='icon_image'><img src='$image_url' alt='icon' / ></div>
<div class='icon_text'>{$a['text_heading']}</div>
</a>
</div>";
Here is whole element
I am a newbie to codeigniter framework. I have created a PHP slider that load images automatically, it works fine in PHP only. Now I used codeigniter, the PHP code does not work well, just can't read the images in a folder.
PHP code:
<div class="slider-wrapper">
<div id="slider" class="phpSlider">
<?php $dir_handle='./slider/images/';
foreach(array_diff(scandir($dir_handle), array( '.', '..')) as $file) {
echo '<img src="./slider/images/'.$file. '" />';
}
?>
</div>
</div>
the code works fine without codeigniter. It reads all images in a specific folder "images" under "slider" folder. And the code when run on browser looks like below:
<div class="slider-wrapper">
<div id="slider" class="phpSlider">
<img src="slider/images/banner1.jpg">
<img src="slider/images/banner2.jpg">
<img src="slider/images/banner3.jpg">
<img src="slider/images/banner4.jpg">
<img src="slider/images/banner5.jpg">
</div>
</div>
It is looking like a path issue. Please use base_url() with img src. where base_url() is your project directory url where your index.php file exist.
echo '<img src="'.base_url().'path/to/images/'.$file. '" />';
Changing the folder path to absolute URL solved the problem.