Resolution Post
This issue is resolved. All images now appear both ways, no matter whether I have my open_image() function or just the case statement. I opted to keep that in there because the reasoning behind that is so that no matter what image is sent through the script, even if it has an incorrect extension, it should still display although I have not tested this. I will test to see if it works.
Requirements
1. PHP with GD Enabled - Ask your hosting company or use <?php phpinfo(); ?> in a php file and verify it yourself.
2. JavaScript enabled browser
Here is the code to output multiple images between JavaScript and GD PHP. This script does nothing more at this time than detect the images and screen width of the user and output the images. I do not have anything in here yet for resizing the images according to users width. I will work on that next. You can add any other GD programming code you want in there.
URL
Spectacular Computer Repair
HTML and JavaScript Code
The JavaScript code must go on your webpage, preferably in between the <head></head> tags. I tried it as a .js file and it wasn't working. I don't know why. The code has been cleaned up a little to make it a little more user-friendly to read.
<html>
<head>
<title></title>
<head>
<script language="javascript" type="text/javascript">
function resize() {
// Variables to be sent to PHP. The PHP variables to be sent are in "" (ie width=, imgpath=)
var width = "width=" + screen.width; // Set variable width to users screen width
var path = "imgpath=images/&"; // Set the path to the image
var logo = "imgname=scrfix-logo.png&"; //Name of my image
var shopping = "imgname=shopping-cart.jpg&"; //Name of my image
var forums = "imgname=forums.jpg&"; //Name of my image
var signin = "imgname=sign-in.jpg&"; //Name of my image
var contact = "imgname=contactus.jpg&"; //Name of my image
var printer = "imgname=printer.jpg&"; //Name of my image
// End of Variables to be sent to PHP. The below variables are only for this script.
var script = "shrink.php?"; // The name of the php script
var scrpth = script + path; //Combine both the script and the path to make it less to work with below.
document.write('<a href="/"><img class="headerimages" src="' + scrpth + logo + width + '" alt="Spectacular Computer Repair" title="Spectacular Computer Repair" /></a>');
document.write('<a href="/catalog/"><img class="headerimages" src="' + scrpth + shopping + width + '" alt="Computer Repair Shopping" title="Computer Repair Shopping" /></a>');
document.write('<a href="/computer-repair-forums/"><img class="headerimages" src="' + scrpth + forums + width + '" alt="Computer Repair Forums" title="Computer Repair Forums" /></a>');
document.write('<a href="/coordinator.php"><img class="headerimages" src="' + scrpth + signin + width + '" alt="User Sign In" title="User Sign In" /></a>');
document.write('<a href="/contact.php"><img class="headerimages" src="' + scrpth + contact + width + '" alt="Contact Spectacular Computer Repair" title="Contact Spectacular Computer Repair" /></a>');
document.write('<a href="#" onclick="window.print();return false;"><img class="headerimages" src="' + scrpth + printer + width + '" alt="Print This Page" title="Print This Page" /></a>');
return 0;
}
</script>
</head>
<body>
<script language="javascript" type="text/javascript">
resize();
</script>
</body>
</html>
Save this file as shrink.php and keep it in the same directory as the above file.
PHP Code
<?php
$imgp = $_GET["imgpath"]; // The image pathway returned from JavaScript. (ie. images/)
$width = $_GET["width"]; // The users screen width returned from JavaScript? (ie 800)
$imgn = $_GET["imgname"]; // The image name returned from JavaScript. (ie scrfix-logo.png)
$ext = substr($imgn, strrpos($imgn, '.') + 1); // Find the extension from the image name (ie jpg)
$image = $imgp . $imgn; // Combine the pathway and the image name together (ie images/scrfix-logo.png)
header("Content-Type: image/" . $ext); // What type of image are we working with?
//Test to see what type of file is loaded. Even if an incorrect extension is loaded, load the image.
function open_image($file) {
# JPEG:
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }
# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }
# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }
# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false) { return $im; }
# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false) { return $im; }
# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false) { return $im; }
# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false) { return $im; }
# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false) { return $im; }
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false) { return $im; }
return false;
}
// Test what extension we are working with and display the image.
switch($ext) {
case "png":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagepng($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "jpg":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagejpeg($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "jpeg":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagejpeg($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "gif":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagegif($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "gd":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagegd($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "gd2":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagegd2($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "wbmp":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagewbmp($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "xbm":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagepng($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "xpm":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagexpm($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
default:
echo "Unable to find any images.";
}
?>
Thanks for everyone's insight and for help finding those stupid mistakes.
Wayne