I have some images in drupal/sites/default/files/images/.
How can I get the absolute path for an image like abc.jpg placed in this directory?
If Drupal is on its own domain (i.e., http://example.com), the absolute path would be /sites/default/files/images/abc.jpg.
If Drupal is in a subfolder (e.g. http://example.com/drupal), the absolute path would be /drupal/sites/default/files/images/abc.jpg.
As I explained in my answer on the same question you deleted, if you are trying to programmatically generate the path (that is, you do not know where the file directory is), you need to use file_directory_path():
$image = file_directory_path() . "/images/abc.jpg";
So if the files are in:
sites/default/files: $image = "sites/default/files/images/abc.jpg".
sites/example.com/files: $image = "sites/example.com/files/images/abc.jpg".
Assuming your previous question is still the use-case, you should be using l() and theme_image() to generate the links and images, respectively, within your module. This will ensure the paths generated are correct given Drupal's environment.
Using the expected output you provided before, a solution would be:
// Images are in drupalroot/sites/default/files/images
$path = file_directory_path() . '/images/';
$detail_file = 'detail_1.jpg';
$image_file = '1.jpg';
$img = theme('image', $path . $image_file);
$options = array(
'attributes' => array(
'title' => t('Sample title for image 1 lorem ipsum'),
),
'html' => TRUE,
);
$output = l($img, $path . $detail_file, $options);
So, assuming your site is at http://example.com/, the output would be:
<a href="/sites/default/files/images/default_1.jpg" title="Sample title for image 1 lorem ipsum">
<img src="/sites/default/files/images/1.jpg" height="<image height>" width="<image width>" />
</a>
But if your site was in a subfolder (e.g. http://example.com/drupal), the output would be:
<a href="/drupal/sites/default/files/images/default_1.jpg" title="Sample title for image 1 lorem ipsum">
<img src="/drupal/sites/default/files/images/1.jpg" height="<image height>" width="<image width>" />
</a>
For Drupal 7
$relativePath = "blabla/abc.jpg";
$uri = file_build_uri($relativePath);
//show public://blabla/abc.jpg
echo $uri;
$url = file_create_url($uri);
//show http://www.yoursite.com/sites/default/files/blabla/abc.jpg
echo $url;
Just use
$url_image = url('sites/default/files/'.file_uri_target($uri), array('absolute'=>true));
In that case you will have a path like "http://www.mywebsite.com/sites/default/files/images/image.jpeg"
$uri is an internal path, like "public://images/image.jpeg"
This is Drupal 7, enjoy it, love it
file_directory_path has been removed in drupal 7. If you use it, you'll get an error 'call to undefined function.' Try to use drupal_real_path or . You can find about the deprecated function here. http://api.drupal.org/api/drupal/includes!file.inc/function/file_directory_path/6
public:// will give you the absolute path of the public folder
Example: to access
drupal/sites/default/files/images/
just put
public://images/
I tried mark's solution and found out that my directory is
"/sites/default/files/images/a.jpg"
But when i write this into my nodes as img src, it didn't show related images.
Besides when i wrote
"../sites/default/files/images/a.jpg"
It worked :)
Because in first case, Drupal tried to find the file in
"content/sites/default/files/images/a.jpg"
Related
I'm beginner in drupal. I would like to fetch images from specific folder in drupal. Is there any way to fetch drupal images directly from any specific in custom theme.
You should use only images from your theme. And you can get path to your theme like this:
$theme = drupal_get_path('theme',$GLOBALS['theme']);
Put that i.e. at top of template file that uses theme images. And then inside your theme templates you can have something like:
<img src="/<?php echo $theme; ?>/images/my_image.png">
If you stored you theme images inside images dir...
If an array of image paths on your local filesystem is what you want - use this code, it should do what you need. gets all the png jpg gif paths within the sites/default/files/vegas directory.
//We will use the stream wrapper to access your files directory
if ($wrapper = file_stream_wrapper_get_instance_by_uri('public://')) {
//Now we can easily get an actual path to that folder
$directory = $wrapper->realpath();
//Don't forget to append your "vegas" subfolder here
$directory .= DIRECTORY_SEPARATOR . 'vegas' . DIRECTORY_SEPARATOR;
$images = glob($directory . "*.{jpg,png,gif}", GLOB_BRACE);
foreach($images as $imagePath)
{
//Do your thing!
echo $imagePath;
}
}
Is there a way to put the html code from a twig template into a html file.
I mean, in this case
$html = $this->render('SiteBundle:Generated:home_news.html.twig', array('news' => $news))->getContent();
Is there a way where I can do this?
$html->output($html_file);
And it gerenates a html file with the template.
I'm doing this because I have news from a wordpress blog and I want to show the latest news in the homepage. So I would want to put the news of my site in a static file to avoid to generate it with each home request.
If you check documentation more carefully, you'd find this chapter, which says:
$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
To complete the #VitalityZurian answer:
First, generate your markup :
$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
Then, write in file :
$file = file_put_contents($filename, $content);
You can use Symfony2 help methods to deal with locations in your bundles:
$bundleResourcesDir = $this->get('kernel')->locateResource('#AcmeAnotherBundle/Resources/views');
file_put_contents($bundleResourcesDir.'/index.html.twig`
And retrieve it:
$view = $this->get('kernel')->locateResource('#AcmeAnotherBundle/Resources/views/index.html.twig');
I would like to get the image path of a field. I'm in a node and I need the Url of image in order to put it as a background-image in inline css.
I can't find the solution. Can you help me?
To get just the path of an image from it's URI:
file_create_url($node->field_image['und'][0]['uri']);
More information on file_create_url() can be found here: http://api.drupal.org/api/drupal/includes%21file.inc/function/file_create_url/7
To get the path of an image created using an image style from it's URI use:
image_style_url($style, $node->field_image['und'][0]['uri']);
More information on image_style_url() can be found here: http://api.drupal.org/api/drupal/modules!image!image.module/function/image_style_url/7
I am afraid, none of the solutions above are correct. They don't follow Drupal standards.
// field_video_image is the name of the image field
// using field_get_items() you can get the field values (respecting multilingual setup)
$field_video_image = field_get_items('node', $node, 'field_video_image');
// after you have the values, you can get the image URL (you can use foreach here)
$image_url = file_create_url($field_video_image[0]['uri']);
More details here:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
I sometime use this:
$node = node_load($nid);
$img_url = $node->field_userimage['und'][0]['uri'];
<img src="<?php print image_style_url($style, $img_url) ?>" />
You also can use the Image URL Formatter module. It allows to change the field format in order to get only the URL:
Bonus: it works with Views as well:
to get image style url:
$field = field_get_items('node', $node, 'field_image');
$image_url = image_style_url('landingpage_slider', $field[0]['uri']);
In the directory containing a custom module, I have a directory containing images.
How do I get the URL of those images?
drupal_get_path('module', $module_name);
The easiest way, like referred above, is to use the relative path to site root:
'/' . drupal_get_path('module', $module_name) . '/img1.jpg'
Using it without the trailing slash in the beginning would break it on multiple level aliases, e.g. http://www.your_site.dev/category/2012/11/02/
// path
drupal_get_path('module', $module_name) . '/images';
file_create_url( drupal_get_path('module', $module_name) ) . '/images';
To get proper URL of your image you need to append base_path() at the beginning of your image path. The correct URL would be:
$url = base_path() . drupal_get_path("module", "MY_MODULE") . "/image.png";
A more complete example that works in Drupal 7 and 8. All other answers just show how to return the path to the module but not how to actually make a URL out of it.
<?php
$module_path = drupal_get_path('module', 'mymodule');
$image_path = "$module_path/images/img1.jpg";
$image_url = file_create_url($image_path);
I'm looking for a plugin (or better yet, not a plugin) for wordpress that lets me generate standard content elements, or includes for posts and pages.
For example, my_content_1 could be:
buy it now for $23!!
Which could then be included in posts and pages using some kind of syntax (or whatever) like:
Welcome to my site, blah blah blah.. check out this product - %my_content_1%
Not looking for anything fancy, anything that does this sort of thing would be awesome.
The point of this being much like a regular php include I could have the same information updated in one place and applied over many pages/posts.
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/
However, other suggestions would be good as I'm not too confident in the quality of the code for that plugin.
Not sure about a plugin, but how about simply creating something yourself? If you created a PHP page and set up variables such as
$content->title = "This is a title"
$content->smallText = "Insert some short paragraph here"
And then just include it in your header? You could store it in your theme directory and then call it like so
<?php $themeFolder = get_bloginfo("template_url"); ?>
<?php include($themeFolder."/content.php") ?>
Would that be suitable?
How about creating a few files and link them in using shortcode?
ie: open your themes/functions.php file add this..
<?php
function wp_my_shortcodes($atts)
{
extract(shortcode_atts(array(
'type' => '', //author, rss, adverts
), $atts));
switch($type) {
case 'author' : $display = wp_display_author_info(); break;
case 'rssview' : $display = wp_display_rss_info(); break;
case 'adverts' : $display = wp_display_adverts(); break;
default : $display = wp_display_author_info(); break;
}
return $display ;
}
add_shortcode('mycontent', wp_my_shortcodes);
function wp_display_author_info()
{
include(TEMPLATEPATH.'/my_author_info.php');
}
function wp_display_rss_info()
{
include(TEMPLATEPATH.'/my_rss_info.php');
}
function wp_display_adverts()
{
include(TEMPLATEPATH.'/my_adverts.php');
}
?>
using shortcodes inside your posts you can then bring in which ever piece of content that you want.. in the example above I've created 3 pages in the template root folder called
my_author_info.php, my_rss_info.php, my_adverts.php all of which speak for themself..
my_author_info.php
this page could use the the_author_meta() to populate a div box with included author info,
my_rss_info.php
include your subscription box to let users subscribe to your blog
my_adverts.php
include 4x 125x125 adverts?
so in the post i could use
[mycontent type='author']
[mycontent type='rssview']
[mycontent type='adverts']
if no argument is added to the shortcode then the default view is shown, in this case..
[mycontent]
would return the authorview as default...
this would then include that file in the content...
just remember to create the included files :)
I found something that is pretty much what I'm looking for:
http://wordpress.org/extend/plugins/reusables/