Hello, and thanks in advance for your help..
My site is run mainly by static php pages, with repeating elements generated using functions found in a dedicated functions script (functions.php).
If you go to http://www.motorsportme.com you can see that I have a small little gallery preview box, about half way down the page. The function that is called from the functions.php file retrieves 3 random jpeg images from a folder and displays them within this orange Div. Obviously they are way too big and slow the entire page down. So I set out to implement a thumbnail generator within the function, as seen below:
function randomImage() {
/* All images named as 1.jpg, 2.jpg etc. */
// Change this to the total number of images in the folder
$total = "32";
// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";
// Change to the location of the folder containing the images
$image_folder = "randomImage";
// Set maximum size variable for thumnail
$maxsize = 115;
// Set a maximum height and width
$width = $maxsize;
$height = $maxsize;
$start = "1";
echo "<h2><i>From the gallery...</i></h2>";
for ($i=1; $i<=3; $i++) {
$random = mt_rand($start, $total);
$image_name = $random . $file_type;
$filename = $image_name;
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($image_folder."/".$filename);
if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($image_folder."/".$filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// Output
header("Content-type: image/jpeg");
echo "<a href=$image_folder/$image_name target=_blank>";
imagejpeg($image_p);
echo "</a>";
imagedestroy($image);
imagedestroy($image_p);
}
}
the code within the index.php that calls the function is as follows:
<div class="randomImages"><?php randomImage(); ?></div>
Unfortanatly I get nothing but machine code, gibberish, where the images should be. I can't figure out why?
I'm guessing its a problem with the HTML output itself; the header information, but I can't see how to get it right... any help is greatly appreciated.
Thank you.
webchef