My Thumbnail Code, I need help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Simulacra
Forum Newbie
Posts: 7
Joined: Sun Sep 11, 2005 9:25 am

My Thumbnail Code, I need help

Post by Simulacra »

Ok so I found this but I having trouble modifying it to the way I want to use it. What I want to do is, get the url of a pic from my database and make a thumbnail out of it, heres my code so far.

Code: Select all

<?php

  $connection = mysql_connect($host,$user,$password)    #13
       or die ("couldn't connect to server");
  $db = mysql_select_db($database,$connection)          #15
       or die ("Couldn't select database");


		$result2 = mysql_query("SELECT pic FROM pics WHERE person='Simulacra'");
             for ($i=0; $i<mysql_num_rows($result2); $i++)
              {
              $pic2 = mysql_result($result2,($i));
			  
			  
# Constants

$mAX_WIDTH = 125;
$mAX_HEIGHT = 180;

# Get image location
$image_file = $pic2;
$image_path = $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);

    # 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);
}

?>
The problem is it only shows one pic, and when I put it into the page I want it to go into I get a error message saying that it cannot modify headers, thats thanks to the second last line, so I need that changed too, but I'm out of ideas
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Im new to php, but i will try anyways...

First off its only going to show One image because you have the SQL selecting pic FROM pic WHERE person='Simulacra', it only take the image of Simulacra.

Second you get hte error Can not moddify headers because of "header("Content-type: image/jpeg"); ". You should have this before the HTML Headers, or if yuo dont have any HTML make sure the begining of <?php is on the very top of the page.

-NSF
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

you can only output one image per script. basically, that header tells the browser that this in an image file, and ONLY ONE.

if you want to show multiple images, you should make one page that resize an image, and another page that loop through the paths and use the first file to make the images.

In the page that rezise an image, it would take a path from $_GET, then resize it, and generate it.
The other page would access this page, give it the file path using $_GET, and grab the output, using

Code: Select all

<img src="resize_image.php?filename=pic1.png">
<img src="resize_image.php?filename=pic2.png">
Another thing you could do is to save those images into a folder using imagejpeg($img,$path) instead of directly displaying them in the script (in this case you don't need the header(). Then you can just access them and display them with the IMG tag. Make sure to change the folder permission to 777 or you would not be able to write the images to files.

Hope this helped.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

also, this will eat up your memory, you should save the thumbnailed copy of the image to your hard drive, then you can just call up the thumbnailed image when necessary :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply