scriptygoddess

02 Jun, 2002

Easy pop up picture script

Posted by: Jennifer In: Scripts

This script was sent in by Pete Holiday.

What this script does is make it easier for you to have pop up images on your site. Instead of writing the code for a new pop up window each time, this little snippet of PHP will basically write that code for you.

(I should probably note that there are a quite few different ways to do this, but this is an interesting use of PHP)

Create a file called "showpic.php" with this code in it and place it in your images directory:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head><title>Display Image</title></head>
<body style="background-color : #000000; color : #FFFF90; font-family : Verdana, Geneva, Arial, Helvetica, sans-serif; font-size : 12px; margin : 0px 0px 0px 0px;">
<img src="<?=$img?>">
</body>
</html>

Then, put this function at the top of your pages, or even better, if there's an included file you're already using, put it in that so you have access to it.

<?
function popup_image($filename) {
// put the path to your images folder below.
$prefix = '/images/';
//put the path to the showpic.php file below
$showpic = '/images/showpic.php';

@$img_info = getimagesize($prefix . $filename);
// the window.open needs a name for the window, and it can't have certain characters. This parsing is for that.
// If you use a static text string for the name, it will try to open every image into the same window (without resizing)
$f2 = str_replace('.', ", $filename);
$f2 = str_replace(' ', ", $f2);
if ($img_info != null) {
$h = $img_info[1]; $w = $img_info[0];
return "<a href=\"#\" onclick=\"javascript:window.open('$showpic?img=$filename', 'ShowImg_$f2', 'directories=0, height=$h, location=0, resizable=0, scrollbars=0, toolbar=0, width=$w'); return false;\">";
}
}
?>

Then when you want to do a pop up image, put this on your page:

<? echo(popup_image('puppies.jpg') . 'My dog' . '</a>'); ?>

where 'puppies.jpg' is the name of your picture, and 'My dog' is the text you want to have linked, that will open the popup window.

See the demo for this here

You can also download the script here.

12 Responses to "Easy pop up picture script"

1 | Lynda

June 5th, 2002 at 1:54 pm

Avatar

Heh…. This is the same script (minus a few features) more or less that I posted a while ago. Amy had taken it and made it a bit more server friendly.

2 | pat

July 4th, 2002 at 11:42 am

Avatar

you rule. thanks very much for this sweet piece of code.

3 | Tina

August 14th, 2002 at 8:28 am

Avatar

I know it's possible to create applet buttons in the MT entry forms/bookmarklets, so I was just wondering how I'd go about making one for this? Just because I know I'll never be able to remember the code to put in every time I want to post a pic, and I'm too lazy to look it up all the time. 😉

4 | CreativePush

August 25th, 2002 at 5:54 pm

Avatar

Can you do that using thumbnail images instead of text??

TIA
Creative/

5 | Derek

June 13th, 2003 at 12:51 pm

Avatar

Thanks for the great code. I implemented it last night on a small site I'm developing for myself and it seems to work fine in Camino and Safari on Mac OS X.

Tested the same code now this morning in Mozilla on Windows 2000 and it works similarly well. But in MSIE 5.5 (Win2k) it craps out. Is this a browser-specific bug and is there a work-around?

6 | Derek

June 21st, 2003 at 4:07 pm

Avatar

Follow-up as I (inevitably) figured out what was wrong with my implementation of this (comment previous): MSIE 5.5 for Windows apparently requires each identifier to be unique, unlike other browsers.

Using the same 'ShowImg_$f2' bit above in the Javascript will not work in MSIE 5.5. For example, if you have a page full of DVD cover thumbnails and one references the same image to be popped up (e.g. two or more titles with the same cover image file name), it won't work. The work around, at least for compatibility in this browser, is to have unique file names for everything (requiring duplicate image file with different names). Nasty workaround, but it works.

Any other easier solutions, now that the problem seems to be pinpointed?

7 | Stephen

May 1st, 2004 at 6:33 am

Avatar

Hi,

I successfully installed this script and it worked fine.

However, I had a recent server upgrade to PHP Version 4.3.6.

Since then the window is pop-up but the tag is just replaced with a blank now instead of the image url.

Any ideas?

Thanks a lot,
Steve

8 | Steve

May 2nd, 2004 at 5:23 am

Avatar

Please view my problem here:

This is just using this script with no changes whatsoever. Here it simply doesn't pass the link to the pop-up window.

Maybe the syntex has changed for PHP Version 4.3.6?

Php info here:

Please contact me here: steves_site@hotmail.com

Thanks for any suggestions,

Steve

10 | Steve

May 5th, 2004 at 11:48 am

Avatar

Hi all,

I've been looking into it a bit more.

I can run this script fine on php 4.3.2 because I have access to a server with that version, I was able to check.

My server has php 4.3.6.

If I follow all instructions to the letter the text link is placed in the page, but without a link.

If I place the showpic.php and image files in the same directory as my html page, the link becomes active and pops up the page. However, <?=$img?> is just blanked out rather than replaced with the image name in the pop-up window. This did work on my old php version of 4.0.6 as well. I think that this script is incompatable with the latest version.

Could someone please look at me php info: http://www.stevessite.scsuk.net/info.php and this script installation here: http://www.stevessite.scsuk.net/popup/

Any help would be appreciated. If you don't have any ideas, please tell me and I will try to find another script.
Right now my images don't pop-up.

Thanks,

Steve

11 | witchy

May 20th, 2004 at 1:37 pm

Avatar

just wanted to say, that I was able to get this to work on the hubby's site, first try! Thanks! :)

12 | Steve

May 24th, 2004 at 12:16 pm

Avatar

Hi,

I found the problem.
If anyone can't get the image to display, check that register_globals are set to "on" in the php configuration.

By the way people are harvesting e-mail address from this site and sending them viruses.

Steve

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements