Problem in access image file from outside of script folder - php

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; ?>">

Related

Image not showing when using PHP to link to SRC

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>

<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 can I get the image url in a Wordpress theme? [duplicate]

This question already has an answer here:
How can I get the image url in WordPress?
(1 answer)
Closed 2 years ago.
I am developing a theme for wordpress. And I have many images in 'images' folder. But when I take the page in browser it is not comming.
My code is
index.php
<ul>
<li><img src="images/mindset.jpg" width="145" height="32" /></li>
Is there any function for getting the image path in wordpress ?
src="<?php echo base_url()?>your_theme_dir/image_dir/img.ext"
As well
src="<?php bloginfo('template_url'); ?>/image_dir/img.ext"
get_template_directory_uri();
This function will help you retrieve theme directory URI, and can be used with your images, your example below:
<img src="<?php echo get_template_directory_uri(); ?>/images/mindset.jpg" />
I had to use Stylesheet directory to work for me.
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/image.png">
You need to use
<?php bloginfo('template_directory'); ?>
It returns the directory of the current WordPress theme.
Now for example, if your theme has some image named example.jpg inside some folder name subfolder folder in the images folder.
themes
|-> your theme
|-> images
|->subfolder
|-> examples.jpg
You access it like this
<img class="article-image" src="<?php bloginfo('template_directory'); ?> /images/subfolder/example.jpg" border="0" alt="">
src="<?php bloginfo('template_url'); ?>/image_dir/img.ext" worked for me for wordpress 3.6 i used it to get header logo using as
<img src="<?php bloginfo('template_url'); ?>/images/logo.jpg">
If you are developing a child theme you can use:
<img src="<?php echo get_template_directory_uri(); ?>-child/images/example.png" />
get_template_directory_uri() will return url to your currently active theme (parent theme), then you add -child/, then add path to your image (the example above assumes your image is at <child-theme-directory>/images/example.png)
I strongly recommend the following:
<img src="<?php echo get_stylesheet_directory_uri(); ?>/img-folder/your_image.jpg">
It works for almost any file you want to add to your wordpress project, be it image or CSS.
If your img folder is inside your theme folder, just follow the example below:
<img src="<?php echo get_theme_file_uri(); ?>/img/yourimagename.jpg" class="story-img" alt="your alt text">
You asked of Function but there is an easier way too. When you will upload the image, copy it's URL which is at the top right part of the window (View Screenshot) and paste it in the src='[link you copied]'. Hope this will help if someone is looking for similar problem.

Categories