Magento Forum

   
How to implement random header graphic? 
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

I want to create multiple versions of “header_top_container_bg.jpg”, and have them change throughout the day - either per session, or every 15 minutes or so.

The PHP is no problem; I can do this in my sleep. But what is the best way to go about it in the Magento environment? I guess my main question is - what about caching?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

The header block html is not cached, as it contains dynamic information (number of items in cart), so caching will not be a problem in your example.

Currently header background is set from boxes.css file, so it can stay as a default choice.
edit app/design/frontend/default/default/template/page/html/header.phtml:

<div class="header-top-container" 
  
<?php if ($_url $this->getBackgroundImageUrl()): ?> 
    style
="background-image:url(<?php echo $_url ?>)" 
  
<?php endif ?> 
>

and define this method in app/code/local/Mage/Page/Block/Html/Header.php or better, extended class:

<?php
//...
public function getBackgroundImageUrl()
{
  
// replace with your random image name generator
  
$image 'header_top_container_bg.jpg';
  return 
Mage::getDesign()->getSkinUrl('images/'.$image);
}

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

This is perfect - I can’t wait to try it out. Thanks for the help, Moshe!

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

Moshe (or anyone),
To get your example to work, I copied the entire Header.php from the /core folder structure, to the /local folder structure, and added the new method. First, tried extending my changes from the core class, but that didn’t work and just proved that I didn’t know what I was doing.

Was it correct to copy over the entire Header.php? ...or is there a better way that would allow me to only add my new method without duplicating the core file?

Apologies if its an easy question. I’m just concerned about preserving functionality through Magento upgrades.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

Nevermind, I think I figured it out. When I tried to extend the single method, my class name and folder structure had to be unique - and they had to match. I tried again, and it seems to have worked. I’m slowly but surely getting a hang of this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

Nevermind nevermind. I jumped the gun on my last post. It wasn’t using my custom code at all, it was just using the default background specified in the CSS. I just thought that since it didn’t produce an error, that it was working. Back to the drawing board. smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

I don’t think I’m far from figuring it out - I think I need to modify config.xml.

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

1) I created the folder structure, “app/code/local/Mycompany/Page/Block/Html/”.

2) I created a new Header.php in the Html folder with the following contents

<?php

class Mycompany_Page_Block_Html_Header extends Mage_Page_Block_Html_Header
{   
    
    
public function getBackgroundImageUrl()
    
{
        
// replace with your random image name generator
        
$image 'header_bg_alternate.jpg';
        return 
Mage::getDesign()->getSkinUrl('images/'.$image);
    
}

}

3) Now what? I think I have to declare it in config.xml, but I’m not sure where the config.xml should be located, and I’m not quite sure what I should put in the config.xml. There may be other steps that I just don’t know.

I’ve been referring to this “Hello World” thread, and this wiki entry to try to understand what I’m doing. Are there any other resources I should look into?

Additional thoughts: I’m also not sure about the “MyCompany” - not sure if its necessary, but I think I have to declare it as a module.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

To declare your module, create app/etc/modules/MyCompany_Page.xml:

<?xml version="1.0"?>
<config>
  <
modules>
    <
MyCompany_Page>
      <
active>true</active>
      <
codePool>local</codePool>
    </
MyCompany_Page>
  </
modules>
</
config>

To configure your module, create app/code/local/MyCompany/Page/etc/config.xml:

<?xml version="1.0"?>
<config>
  <global>
    <
blocks>
      <
page>
        <
rewrite>
          <
html_header>MyCompany_Page_Block_Html_Header</html_header>
        </
rewrite>
      </
page>
    </
blocks>
  </global>
</
config>

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

Thanks, Moshe. I was skirting around the solution, and your last post sealed the deal. Owe you a beer.

I’ll definitely be getting php|Architect’s Programming with Magento book when it becomes available. Hopefully that’ll cut down on the trial and error - but I greatly appreciate your help. wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
cshontz
Sr. Member
 
Avatar
Total Posts:  91
Joined:  2007-11-07
 

opendir() doesn’t seem to work inside Magento’s file structure - I’m assuming due to a security restriction. Makes sense. Not a huge problem, because it works just fine when I manually specify image files, but I was hoping to be able to just drop files into a folder and have them automatically included in the random selection.

Does anyone know of a way to circumvent this access restriction without lifting it?

$handle opendir('images/'); 
$file_list = array();
        
while (
$file readdir($handle)) {            
    
if (substr($file,0,10) == "header_bg_"array_push($file_list,$file);
}
        
closedir
($handle);
        
$image $file_list[array_rand($file_list)];

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

upload your images into magento/media/header/ and try:

$file_list glob(Mage::getBaseDir('media').DS.'header'.DS.'*');
$image Mage::getBaseUrl('media').'header/'.array_rand($file_list);

 
Magento Community Magento Community
Magento Community
Magento Community
 
JeffreyKarbowski
Jr. Member
 
Total Posts:  23
Joined:  2008-11-18
 

Could someone please tell me why this is not returning the filename, and instead it is returning integers? Could you please give a way to get filenames too?
http://www.mysite.com/media/upload/image/headerImages/(0 and 1)

<?php
class MySite_Custom_Block_Test extends Mage_Core_Block_Abstract
{
 
public function getRandomImage()
  
{
$file_list 
glob(Mage::getBaseDir('media').DS.'upload'.DS.'image'.DS.'headerImages'.DS.'*');
$image Mage::getBaseUrl('media').'upload/image/headerImages/'.array_rand($file_list); 

    return 
$image;
}
 
protected function _toHtml()
  
{
$html 
$this->getRandomImage();
    
Header('Content-type: image/jpg');

     return 
$html;
  
}
}
?>

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top