Easy pop up picture script
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.
June 5th, 2002 at 1:54 pm
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.
July 4th, 2002 at 11:42 am
you rule. thanks very much for this sweet piece of code.
August 14th, 2002 at 8:28 am
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.
August 25th, 2002 at 5:54 pm
Can you do that using thumbnail images instead of text??
TIA
Creative/
June 13th, 2003 at 12:51 pm
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?
June 21st, 2003 at 4:07 pm
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?
May 1st, 2004 at 6:33 am
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
May 2nd, 2004 at 5:23 am
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
May 2nd, 2004 at 5:26 am
Please view my problem here: http://www.stevessite.scsuk.net/test/
Php info here: http://www.stevessite.scsuk.net/info.php
May 5th, 2004 at 11:48 am
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
May 20th, 2004 at 1:37 pm
just wanted to say, that I was able to get this to work on the hubby’s site, first try! Thanks!
May 24th, 2004 at 12:16 pm
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