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

    looks ok to me?? I am seeing three images ok!

      I'm seeing images, not gibberish.

        You can't send header information once content is already sent. Also, you don't have ANY image defined at all within your HTML code (i.e., you have no <img> tags). My advice: move the thumbnail generation code into a separate page (thumbnail.php), and set that as the source of the image:

        <img src="thumbnail.php" width="100" height="100" />

        Edit: didn't see the other's posts. I'm seeing images too...

          you see the pics?

          You guys duplicated my enviornment and you saw images?

          then why can't I?

          Does the fact that my installation is all done by XAMPP have anything to do with it?

          I'll try moving the function to its own page and calling it from within an image tag, but will that actually print all three images?

          Im just not sure why it doesn't work as it is.. it should.. and apparently for you guys.. it does!... hmmm.. thanks for your help

            webchef wrote:

            you see the pics?

            Yes

            webchef wrote:

            You guys duplicated my enviornment and you saw images?

            No

            webchef wrote:

            then why can't I?

            Dunno

              I probably should have mentioned, the link i gave is the prodcution site. the final version that is hosted far far aways and is using the old method to show the pictures (html width percentage resize).... the script above is being run, tested, and frustratingly caressed on my local machine. So when you guys say you see the images, do you mean the images at motorsportme.com?

                Ok.. i still can't get it to work... I tried all sort of combinations and i still see gibberish being printed where the images should be... this is driving me crazy...

                  webchef wrote:

                  So when you guys say you see the images, do you mean the images at motorsportme.com?

                  That is the page you linked to.

                  I see images. But they're the full-sized images squeezed into small boxes, not actual thumbnails.

                  However, as LoganK has already pointed out, if you want to generate thumbnails on the fly, then your <img> tag has to link to the script that generates the thumbnails in its src= attribute. Then that script sends out the image data (and only the image data - it's sending an image, not an HTML page).

                    I tried placing the thumbnail function in a seperate page, take the code out of the function (as well as leaving the function intact, and then calling the function from within the same page) and calling it from the Image tag... i still get gibberish... could it be because the code that prints the image is in a for loop?

                    Maybe the images themselves are way to big for GD lib to handle?

                    I'm uploading what ive got right now to the production site to see if maybe its my web server and PHP installation that is causing the problem. Though i don't think it is.. worth a shot anyway.

                      Nope.. not a problem with my installations.. i get the same problems online...

                      anybody willing to take my files and give it a shot on their development servers?

                        webchef wrote:

                        could it be because the code that prints the image is in a for loop?

                        Could well be - how many images do you think can fit in an <img> tag?

                        webchef wrote:

                        Maybe the images themselves are way to big for GD lib to handle?

                        Judging from the random ones I've seen, they're hardly "big".

                          yeah.. i didn't think they were that big..

                          if you got to the production site motorsportme.com you'll see the trouble im getting...

                          I'm gonna attempt to remove the for loop (which by the way, at the moment only prints one image for testing reasons.. ) but maybe if i take it out of the loop it will work, and then i'll modify the code to print all 3 seperately.. but.. how do I call it from the IMG tag? do i have to have a seperate thumbnail script for each image?

                          .o0(i think the whole image aspect of PHP needs to be rethought....)

                            Instead of having the thumbnail.php output three, random images, have it output 1 image, depending on the ID in the GET vars. Like this:

                            thumbnail.php?id=42

                            Then PHP will query the DB for the image with ID #42, and output that particular image. From here, it's a cinch in your main PHP to just call thumbnail.php 3 times in a while loop, each with a different, random ID.

                              nope.. no difference.. i got rid of the loop and im still having problems.. this sucks.

                              Thumbnail.php:

                              <?php
                              	//function randomImage() {
                              		 /* Name your images 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;
                              
                              	// You do not need to edit below this line
                              	$start = "1";
                              	echo "<h2><i>From the gallery...</i></h2>";
                              	//header("Content-type: image/jpeg");
                              	//for ($i=1; $i<=1; $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
                              		echo "<a href=$image_folder/$image_name target=_blank>";
                              		imagejpeg($image_p);
                              		echo "</a>";			
                              		imagedestroy($image);
                              		imagedestroy($image_p);
                              	//}
                              //}
                              ?>
                              

                              index.php:

                              <div class="randomImages"><img src="code/thumbnail.php"></div>
                              

                                thanks for the tip, logan.. once i fix this problem with the images not showing at all.. i'll use your tip to generate the three images...

                                but im still stuck on this part of the journey...

                                maybe i should switch to ASP..... NO! I DID NOT JUST SAY THAT!

                                  its weekend here now.. so im gonna put the site back to its original form of placing large images in small boxes until the start of next week, when i will tackle this issue again..

                                  regards
                                  mike

                                    A) You're not outputting any content-type headers, so the browser doesn't know what to do, and

                                    😎 You're using echo statements!!!! In an image!!! No!! Bad!! Bad!!! Take the echo statements out and put them in your index.php. There should be absolutely NO echo statements in thumbnail.php at all. It should have one header() command to say the content-type is an image, and that's it!!

                                      Write a Reply...