Image not showing when using PHP to link to SRC - php

Trying to use php to link to an image in a different folder than the file I am currently in but the image isn't showing.
Here's the path file:
<?php
if (!defined("ROOT_PATH")) define ("ROOT_PATH", realpath(dirname(__FILE__)));
if (!defined("BASE_URL")) define ("BASE_URL", "http://localhost/stnresp");
?>
Here's the code I am using at the moment:
<div class="logo-image">
<img class="header-logo" src="<?php echo ROOT_PATH . '/assets/images/stn-logo-cropped.png'; ?>" alt="">
</div>
I can't understand why the root path isn't taking the link to the root directory and then through to the image? What am I missing?
Thanks

realpath defines the physical path of the file, instead the src attribute of the image needs url path.
In your case you have to use BASE_URL instead of ROOT_PATH:
<div class="logo-image">
<img class="header-logo" src="<?php echo BASE_URL . '/assets/images/stn-logo-cropped.png'; ?>" alt="">
</div>

Related

Problem in access image file from outside of script folder

Problem: Hard to synchronise image between 2 folder
My project has 2 image folder:
Home/user/public_html/image
Home/user/m.fruit.com/image
public_html serves the main page for desktop version
m.fruit.com serves the mobile page
Both version serves the same images.
When I have new fruit image, I need to put it into both folder. This becomes double work...
my way of accessing image for desktop version is: (index.php in public_html folder)
<img id="image" src="image/<?php echo $image; ?>.jpg">
my way of accessing image for mobile version is: (index.php in m.fruit.com folder)
<img id="image" src="image/<?php echo $image; ?>.jpg">
For mobile version, I did try to access image in public_html so that I don't need 2 image folder. But it seems like not possible:
<img id="image" src="public_html/image/<?php echo $image; ?>.jpg">
Is there any way to use only 1 image folder to serves these 2 version of website instead of 2 in this case?
Other better solution also welcomed.
First, you can set base URL: like
define('BASE_URL', 'http://example.com/');
then set image src:
<img src="<?php echo BASE_URL ?>image/imagename.jpg ">
or create a helper function:
PHP CODE
function asset(path){
return BASE_URL . path;
}
HTML CODE:
<img src="<?php echo asset('image/imagename.jpg'); ?> ">
This will work on both sides.
Try to change the image url scheme with this
<?php echo "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>
define base_url();
define('base_url', 'http://Home/user');
and then call this base url where you want show image.
<img id="image" src="image/<?php echo base_url('/public_html/').$image; ?>">

<img> tag is not working in php Codeigniter

<img> tag is not working in PHP Codeigniter
<img src="<?php echo $p_info['p_img']; ?>" alt="Product Image is not uploaded" height="700px" width="500px" />
Output of $p_info['p_img'] is giving correct path where image is saved.
and i uploaded the image now i want to show that image in edit process
<img src="<?php echo $p_info['p_img']; ?>"
alt="Product Image is not uploaded" height="700px" width="500px" />
<?php echo $p_info['p_img']; ?> should be a path which access images from the project directory not from a local path.
C:/xampp/htdocs/squadfreetry/assests/img/products_images/Acne Free (1).jpg this is wrong & will not show image.
You have to save image path starting from /assests/... and to access it by calling base_url() at first & followed by saved path in DB.
Code should be like:
<img src="<?=base_url().$p_info['p_img'];?>"
alt="Product Image is not uploaded" height="700px" width="500px" />
Where $p_info['p_img'] is a path starting from /assests/..
Sometimes if you view source using certain browsers (firefox, chrome), it omits the /> tag. Try view source using a notepad or something, it should display proper /> tag.
If you want a shorter tag, use this:
<?php echo img(array('src'=>'image/picture.jpg', 'alt'=> 'alt information')); ?>
Here is detailed information:
https://codeigniter.com/user_guide/helpers/html_helper.html#img
More examples:
<img src="<?php echo base_url('assets/uploads/' . $gambar)?>" alt=""/>
and
<img src='<?php echo base_url("assets/uploads/$gambar")?>' alt=""/>
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Please try the below way:
I am assuming that your project folder is Cart -> images/your desired image
when you do file uploading then you have to save the image in folder -> images
and the image name in database.
so to access it replace your image code like this:
<img src="<?php echo base_url(); ?>images/<?php echo $p_info['p_img']; ?>" alt="Product Image is not uploaded" height="700px" width="500px" />
On CodeIgniter 4 you can use the img function, from html helper.
Adds the helper on controller -> helper('html');
Generates the img tag on the view -> echo img('public/img.png').

how to insert custom images and html in wordpress

I'm creating a new WordPress Theme and I want a media folder to contain some images that are not considered to be posts.
e.g. themes/theme-name/media/sampleIMG.jpg
adding
<img src="media/sampleIMG.jpg">
in my theme folders index.php leads to an 404 error code output because apparently my local apache server is requesting that file from a different directory:
/html/media/sampleIMG.jpg
What am I doing wrong?
<img src="<?php echo get_stylesheet_directory_uri();?>/assets/images/logo.svg" alt="" class="svg logo">
Using get_template_directory_uri() to link a static image with its correct path in html :
<img src="<?php echo get_template_directory_uri(); ?>/media/sampleIMG.jpg" alt="theme logo" />
Note: get_template_directory_uri() return absolute path to the
directory of the current theme.

How to Autoload & Display Images in View (Codeigniter)

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.

How to correctly use get_template_directory_uri() WordPress function to load an image that is in a subfolder of my theme?

I am pretty new in WordPress and I have the following doubt about how to insert in my homepage an immage that is in a subfolder into my theme directory.
So I have the following situation: Into my custom theme directory I have the following folder that contains a jpg immage: /assets/img/flexslider/flex-1.jpg
Now in my header.php file I have something like this:
<li>
<img src="assets/img/flexslider/flex-1.jpg">
<div class="flex-caption">
<p class="flex-caption-text">
<span>Lorem ipsum</span><br>
<span>sit dolor</span><br>
<span>adipiscing elitur</span>
</p>
</div>
</li>
Obviously, when I load the page, the immage flex-1.jpg is not loaded because there is not the right path (infact using FireBug I obtain that it try to load the assets/img/flexslider/flex-1.jpg immage) so I think that I could use the absolute path but this is pretty orrible !!!
So I am thinking to use the get_template_directory_uri() function provided from WP to do this and I have try to change the previous code in this way:
<li>
<img src=<?php get_template_directory_uri().'/assets/img/flexslider/flex-1.jpg' ?>>
<div class="flex-caption">
<p class="flex-caption-text">
<span>Lorem ipsum</span><br>
<span>sit dolor</span><br>
<span>adipiscing elitur</span>
</p>
</div>
But don't work and using FireBug I can see that load nothing, infact in my brower source code I have:
<img src="">
Why don't work? What am I missing?
Tnx
Andrea
I hope it will work:
<img src="<?php echo get_template_directory_uri(); ?>/assets/img/flexslider/flex-1.jpg" />
If your assets folder inside theme.
please try :
<img src="<?php print(get_template_directory_uri()); ?>/assets/img/flexslider/flex-1.jpg" />
just check for slash, if double before "assests", remove static one.
You can also use:
<img src="<?php bloginfo('template_url'); ?>/images/yourimage.jpg">

Categories