Hi,
Well I just spen about 3 hours going through this board and Google but couldn't accomplish what I had hoped. I have paths to member's images stored in mysql. There are pages that show about 15 or so member's pictures so I would like to generate thumbs of the images on the fly to save on bandwidth. The only problem is that I couldn't seem to find a way to do any of the GD functions outside of the header. From what I could tell the header requires"("Content-type: image/jpeg");" ..but what good does that do me on an html page? I tried changing it to text/html but no go. Whenever I try to display the pic I get a gibberish string of characters.
If I use only the header and do "Select image from db where user id = '$userid',; and then send that variable through the code it displays fine. I'm unable to do any sort of a loop because from what I've read image/jpeg is only for one image.
I was able to use this code to display ONE image.
So is my line of thinking off here? Would it be ideal to pre-generate all of the thumbs and store them on the server? (And also figure out how to create them on the fly for new members,..) If so,.I would I use GD to take an entire folder on the server and generate thumbs into a seperate folder.
I would really like the option that I was trying to get rolling in action but it's not looking possible.
Any help>?
Thanks
<?php
# Constants
define(IMAGE_BASE, 'http://www.mysite.com/pics');
define(MAX_WIDTH, 25);
define(MAX_HEIGHT, 25);
$bandid=$_GET['bandid'];
$getpic = "SELECT userid FROM users LIMIT 10";
$picresult = mysql_query($getpic, $conn) or die(mysql_error());
while($row9 = mysql_fetch_assoc($picresult)){
$userid = $row9[userid ];
# Get image location
$getpic = "SELECT image from user_table WHERE userid = '$userid'";
$picresult = mysql_query($getpic, $conn) or die(mysql_error());
$image = mysql_result($picresult, 0);
$image_file = str_replace('..', '', $image);
$image_path = IMAGE_BASE . "/$image_file";
# Load image
$img = null;
$ext = strtolower(end(explode('.', $image_path)));
if ($ext == 'jpg' || $ext == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($ext == 'png') {
$img = @imagecreatefrompng($image_path);
# Only if your version of GD includes GIF support
} else if ($ext == 'gif') {
$img = @imagecreatefrompng($image_path);
}
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
//echo "Width is $width and height is $height<br>";
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
}
# Create error image if necessary
if (!$img) {
$img = imagecreate(MAX_WIDTH, MAX_HEIGHT);
imagecolorallocate($img,0,0,0);
$c = imagecolorallocate($img,70,70,70);
imageline($img,0,0,MAX_WIDTH,MAX_HEIGHT,$c2);
imageline($img,MAX_WIDTH,0,0,MAX_HEIGHT,$c2);
}
# Display the image
//header("Content-type: image/jpeg");
imagejpeg($img);
}
?>