Page 1 of 1

Problem Calling PHP from HTML - Image Rotator

Posted: Mon Feb 27, 2006 10:53 am
by kkashi
Hi,

I appreciate all help on the following issue:

I have a download php code that is used for image rotation ( http://www.alistapart.com/articles/betterrotator) which uses a configuration file to indicate which images to use. I can call the page directly ( http://usa-translation.com/rotator3.php ) which will also show me the links when pointer placed on the image file.

However, it does not work when executing from html ( http://usa-translation.com/foo.html ).

All feedback is greatly appreciated. I am a developer, but not really a PHP developer.



Regards,
Kevin

Posted: Mon Feb 27, 2006 11:08 am
by feyd
If you look at the page source for your rotator, you'll see that it outputs HTML, not an image.

Posted: Mon Feb 27, 2006 11:16 am
by kkashi
Not sure if I understand. Is this work for hire? If so, I thought this is a community for helping each other, not soliciting work.

Thanks,
Kevin

Posted: Mon Feb 27, 2006 11:23 am
by John Cartwright
kkashi wrote:Not sure if I understand. Is this work for hire? If so, I thought this is a community for helping each other, not soliciting work.
We are here to help you free of charge :wink:

to get the image displayed directly within an <img> tag call your rotator3.php script has to send the right content type headers.. hint header() in combination as readfile()

Feyd explained exactly why your script was not working, because your script is returning html code, instead of the image file.. so your script is basically trying to read it as

<img src="<img src="image.jpg">>

Posted: Mon Feb 27, 2006 11:24 am
by feyd
I think you have mistaken my signature for post text.

The rotator outputs

Code: Select all

<a href="something"><img src="somefile.gif" /></a>
not an image in itself.

Posted: Mon Feb 27, 2006 11:33 am
by kkashi
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Sorry feyd,

You are right, I was looking at your signature.

Is there any way to call the php from html. The documentaion was saying I should just be able to use the img tag.

Code: Select all

<?php

/*

  Author: Dan Benjamin - http://hivelogic.com/

  Copyright (c) 2004 Automatic, Ltd. All Rights Reserved.

  THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY OR CONDITION OF 
  ANY KIND, EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY 
  IMPLIED WARRANTIES OF ACCURACY, MERCHANTABILITY, FITNESS FOR A PARTICULAR 
  PURPOSE OR NONINFRINGEMENT.  

  IN NO EVENT SHALL DAN BENJAMIN, A LIST APART, OR AUTOMATIC, LTD. BE LIABLE
  FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES OR
  LOST PROFITS ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE (HOWEVER
  ARISING, INCLUDING NEGLIGENCE), EVEN IF DAN BENJAMIN, A LIST APART, OR
  AUTOMATIC, LTD. IS AWARE OF THE POSSIBILITY OF SUCH DAMAGES.

*/



  # file containg your image descriptions

  $IMG_CONFIG_FILE = 'images.ini';
	showImage($IMG_CONFIG_FILE);


  # You shouldn't need to change anything below this point

  function showImage( $ini=null ) {
    global $IMG_CONFIG_FILE;
    # if no custom ini file has been specified, use the default
    $ini_file = $ini ? $ini : $IMG_CONFIG_FILE;
    # read the config file into an array or die trying
    $images = @parse_ini_file($ini_file,true);
    if (! $images) {
      die('Unable to read ini file.');
    }
    # pick a random image from the parsed config file
    $img = array_rand($images);
    # get the selected image's css id if one exists

    $id = $images[$img]['id'] ?
      sprintf( ' id="%s" ', $images[$img]['id'] ) : '';
    # get the selected image's css class if one exists
    $class = $images[$img]['class'] ?
      sprintf( ' class="%s" ', $images[$img]['class'] ) :
      '';
    # get selected image's dimensions
    $size = @getimagesize( $images[$img]['src'] );
    # if an url was specified, output the opening A HREF tag
    if ( $images[$img]['url'] ) {
      printf(
        '<a href="%s" title="%s">',
        $images[$img]['url'],
        $images[$img]['title']
      );
    }
    # output the IMG tag
    printf(
      '<img src="%s" alt="%s" %s %s%s/>',
      $images[$img]['src'],
      $images[$img]['alt'],
      $size[3],
      $id,
      $class
    );
    # if an url was specified, output the closing A HREF tag
    if ( $images[$img]['url'] ) {
      echo('</a>');
    }
  }


?>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Feb 27, 2006 11:40 am
by feyd
comment out the logic after "# if an url was specified, output the opening A HREF tag"

replacing it with

Code: Select all

header('Content-type: ' . image_type_to_mime_type($size[2]));
header('Content-length: ' . filesize($images[$img]['src']));
readfile($images[$img]['src']);
or similar.

Posted: Mon Feb 27, 2006 12:51 pm
by kkashi
Thank you for your inputs.

I did exactly what you mentioned. However, it seems like the code is never getting called ( http://www.usa-translation.com/foo.html ). It only outputs the exact text.

Can anyone refer me to a working image rotator which allows you to specify LINKS?

Thanks :x

try this:

Posted: Sat Jun 17, 2006 7:04 pm
by newB
if your link address contains question marks try putting quotation marks on either side. I use it for a site I've been playing around with. As you can see it works great even with a large number of random images being loaded every time the page is refreshed.

feyd | link removed. One and only warning about this kinda rule break.

Sorry

Posted: Sun Jun 18, 2006 9:16 am
by newB
I did a search for site rules and came up blank, and thought I was being courteous in specifying site content