PHP / JavaScript to dynamically change image paths - php

This is my first question, hence please ignore mistakes, if any.
I have an issue where I am serving three alternate layouts to my visitors based on the device they are surfing from.
Mobile, Tablet, and Desktop.
My website is in PHP and I am using just 280 bytes of JavaScript to determine the browser width of my visitor. There is no other JavaScript or any libraries like jQuery, MooTools. I want to keep my site very very light weight thus resulting in faster page load speeds.
I have a PHP variable called $layout and the the value to it is assigned by the JavaScript dynamically based on the browser width. The three values assigned to it are mobile or tablet or desktop
Now I have links in my xhtml which are like this:
<img src="cdn/images/desktop/image1.jpg" width="500" height="200" alt="image1">
<img src="cdn/images/desktop/image2.jpg" width="500" height="200" alt="image2">
<img src="cdn/images/desktop/image3.jpg" width="500" height="200" alt="image3">
By default images are loading from the cdn/images/desktop folder.
What I am looking for is if the value is $layout is tablet then the images should load from cdn/images/tablet folder and similarly if the valur of $layout is mobile then images should load from cdn/images/mobile folder.
Name of the images remain the same. They are three different resolutions in three different folders.
Kindly suggest a PHP solution if possible to do this in PHP.
Otherwise please suggest a plain JavaScript solution (No libraries like jQuery, MooTools ot any such)
Thanks
UPDATE
Actually I am using Joomla as a CMS so in my posts I cannot use PHP code within posts hence I want that after the page is rendered or during the rendering these paths must change.

$(function(){
//mobile or tablet or desktop
var client='<?php echo $layout; ?>'
if(client=='desktop'){
//do nothing
}else if(client=='tablet'){
$('#image-list img').each(function(){
$('this').attr('src',$(this).attr('src').replace('desktop','tablet'));
});
}else{
//mobile
$('#image-list img').each(function(){
$('this').attr('src',$(this).attr('src').replace('desktop','mobile'));
});
}
});
<div id="image-list">
<img src="cdn/images/desktop/image1.jpg" width="500" height="200" alt="image1">
<img src="cdn/images/desktop/image2.jpg" width="500" height="200" alt="image2">
<img src="cdn/images/desktop/image3.jpg" width="500" height="200" alt="image3">
</div>
use jQuery
sorry i didn't know it don't require lib.
var client='<?php echo $layout; ?>'
var list=document.getElementById('image-list');
var img=list.getElementsByTagName('img');
for(var i=0;i<img.length;i++){
//img[i].src=img[i].src.replace('desktop',client);
img[i].setAttribute('src',img[i].getAttribute('src-data').replace('desktop',client));
}
put it in the end of file.
<img src-data="cdn/images/desktop/image1.jpg" width="500" height="200" alt="image1">

Try this:
<img src="cdn/images/<?php echo $layout; ?>image3.jpg" width="500" height="200" alt="image3">

why don't you use simple javascript to change the src of the image ?
it's maybe not the most nicely looked script but you can do something like this:
xhtml file :
<img id="image1" src="cdn/images/desktop/image1.jpg" width="500" height="200" alt="image1">
<img id="image2" src="cdn/images/desktop/image2.jpg" width="500" height="200" alt="image2">
<img id="image3" src="cdn/images/desktop/image3.jpg" width="500" height="200" alt="image3">
after the javascript find out which layout is it just do:
document.getElementById("image1").src = "cdn/images/" + layout + "/image1.jpg";
document.getElementById("image2").src = "cdn/images/" + layout + "/image2.jpg";
document.getElementById("image3").src = "cdn/images/" + layout + "/image3.jpg";
is that what you mean ?

Related

Setting up a text page using a php string of code

It will be placed within a template that returns dynamic channels
Php linking thats dynamic > http://domain.com/text.php?content=yourtext
doing so returns the text. "yourtext"
what i mean by this is "yourtext" will be added into embed codes thus being dynamic.
i am basically reconstructing a streaming sites layout.
EXAMPLE:
<iframe src="http://domain.tv/embed/video/<?php echo $yourtext ?>" frameborder="0" framespacing="0" width="100%" height="100%" /></iframe>
sorry for my poor english i will adjust it if it doesn't meet your english.
If you are trying to get yourtext from http://domain.com/text.php?content=yourtext
<iframe src="http://domain.tv/embed/video/<?= htmlspecialchars($_GET['content']) ?>" frameborder="0" framespacing="0" width="100%" height="100%" /></iframe>

How to show files in iframe by Jquery or JavaScript and prevent to show in another page loading?

Here is my problem, I had describe in code project. When I am clicking in an icon it try to open in new page for showing image or pdf or video. But, I don't like this. I like to show that files in my iframe in same page. How can I do this ?
My code project link is:
Please don't open in new page. Just stay in same page.
I could suggest using a plugin as it makes things easier.
http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/
Alternatively what you want is to change the iframe src.
Using jQuery, you could for example, do something like this
<iframe id="myframe" style="display:none" width="630" height="420"></iframe>
<div class="fileIcon">
<a class="click" onClick="" rel="gallery" href="#">
<img src="src" title="" width='100' height='100' alt="" />
</a>
</div>
And JS
$('a').click(function(e){
e.preventDefault()
var s = $(this).find('img').attr('src');
$('#myframe').attr('src', s).show();
});
FIDDLE
This will get the image src and more it to the iframe
Give your iframe a name="myframe" then in your target you would use target="myframe"

displaying a php file in an iframe using codeigniter

Good day guys, I'm trying to learn codeigniter and I'm having a trouble displaying a php file in an iframe. Here's my code. It returns "404 Page not found"
here's my view:
<iframe class="tabContent" name="myIframe" src="<?php echo site_url('pages/view_about');? >" marginheight="8" marginwidth="8"></iframe>
then here's my controller:
function view_about(){
$this->load->view('pages/about');
}
here's how my folders are arranged
application->views->pages->about.php
application->contollers->pages.php
I would just like to add, the code I pasted above for my view is located in
application->views->templates->footer.php
the iframe is actually a part of my footer thanks
php ending tag has space between
try this..
<iframe class="tabContent" name="myIframe" src="<?php echo site_url('pages/view_about'); ?>" marginheight="8" marginwidth="8"></iframe>
Hi can you please try below code hope it will work for you.
Page 1
<iframe id="targetFrame" src="" width="100%" height="100%" scrolling="NO" frameborder="0" ></iframe>

Evaluating PHP inside javascript for a wordpress blog for a pinterest button

This is a very interesting problem that I am running into. I am appending a div with a pinterest button using javaScript but I am using PHP inside the Javascript to determine what the source of the image and the URL of the page to send to pinterest.
I am doing it this way instead of using plain HTML provided by pinterest because I have found that you can use your own branded pinterst button .jpg instead of the iFrame that pinterest uses and forces you to use their branding. I have used this javaScript successfully on other parts of my sites without using PHP. I am very new to PHP and JavaScript so I am not sure if I am missing anything but I have spent countless hours tinkering with this problem and need a fresh set of eyes. Thanks for all your help!
function add_social_media () {
?>
<p align="right">
<a href="http://twitter.com/home?status=<?php print(urlencode(get_permalink())); ?>+<?php print(urlencode(the_title())); ?>">
<img src="/sites/aerialist.localhost/files/images/twitter.jpg" alt="Share On Twitter" /></a>
<a href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Ffamousartistprints.com%2F&media=http%3A%2F%2Ffamousartistprints.com%2F" ><img border="0" src="/sites/aerialist.localhost/files/images/twitter.jpg"/></a>
<script>
jQuery(document).ready(function() {
jQuery('#pinItDiv').append( '<a class="pin-it-button" href="http://pinterest.com/pin/create/button/?url='+
<?php the_permalink(); ?>+
'&media='+
//set your image path here
<?php if(function_exists('the_post_thumbnail')) echo wp_get_attachment_url(get_post_thumbnail_id()); ?>+
'&description='+<?php echo get_the_title(); ?>'">'+
'<img class="pinItSuite" title="Pin It" src="/sites/aerialist.localhost/files/images/pinSuite.jpg" alt="" border="0" /></a>');
});
</script>
<?php
}
add_action('thesis_hook_before_headline', 'add_social_media');
As I understand it, it's best practice to keep php and javascript separate as much as possible. Try putting your javascript in its own js file and enqueuing it with wordpress's scripts via wp_enqueue_scripts. Then use wp_localize_script to pass it any php variables you need (such as the_permalink() or get_the_title() ).
Check out these sites for more info:
http://wp.tutsplus.com/articles/how-to-include-javascript-and-css-in-your-wordpress-themes-and-plugins/
http://tosbourn.com/2011/08/wordpress/accessing-php-variables-from-within-javascript-under-wordpress/

What is wrong with my PHP code?

I have been seriously been looking at this forever! I'm going out of my mind And can't figure out why my images are not displaying in my custom made footer.
I have firefox with firebug and it is simply saying that the url is failing to load. so I copy and pasted a url to an image that is currently working and is being shown via the background property in CSS just fine. (thats the top one that says dakota jones). copy and pasting the exact img src proves it still not to work.
my folder is images. My functions.php is right outside. the hierarchy is correct. what the heck?? The testing text in the p tags work just fine. uhuhuhu
Somebody help me! I'm using wp and genesis theme.
add_action('genesis_before_footer', 'include_sponsors');
function include_sponsors() { ?>
<div class="sponsors">
<p>This is testing text</p>
<img src="images/dakotajonesheader3.jpg" alt="Smiley face" height="42" width="42" />
<img src="images/tfobw.png" />
<img src="images/basskingbw.png" />
<img src="images/bighawgbw.png" />
<img src="images/kbw.png" />
<img src="images/mccoybw.png" />
<img src="images/nfcbwpng" />
<img src="images/rayjusbw.png" />
<img src="images/rrbw.png" />
</div>
<?php }
Use WordPress' inbuilt function:
<?php
bloginfo('template_directory'); ?>/images/dakotajonesheader3.jpg
?>
.. which will reference /wp-content/themes/your-theme/images/dakotajonesheader3.jpg
Wordpress dynamically rewrites URLs, so the URL you use to access a page is not the same as the path to the scripts that are running on the server. So you might request your page with example.com/Home. But your images are not stored in example.com/Home/images, which is where you're telling your browser to look. They're stored in example.com/wordpress/wp-content/themes/your_theme/images. So you have to give the absolute path to the images to the browser in your <img> tags.
Carson is correct, and you can use absolute paths to your images. Alternatively, if you want to avoid using absolute paths, you can call your images using bloginfo('template_directory');
For example:
<img src="<?php bloginfo('template_directory'); ?>/images/dakotajonesheader3.jpg" alt="Smiley face" height="42" width="42" />

Categories