41 users online (0 members and 41 guests)  


  Results 1 to 10 of 10

Related

  1. image tag problem    Forum: HTML Forum
    Replies: 3
  2. Image problem    Forum: Myspace Forum
    Replies: 1
  3. Problem with background image    Forum: CSS Forum
    Replies: 0
  4. Image Positioning Problem?    Forum: CSS Forum
    Replies: 5
  1. #1
    pegboy's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Aug 2001
    Location
    Knoxville, TN
    Posts
    76

    another image problem

    here we go again...

    ok, the idea: read a directory full of images. for each image reduce 50%, crop out a 50x50 thumbnail from the center and save the jpg to a different directory.

    the script works when i specify one image... but when i throw in the while() loop all the thumbs are solid black... i'm stumped.

    here's the code:
    Code:
    <?php
    // open dircetory
    $upload_dir = "./images/";
    $rep=opendir($upload_dir);
    while ($file = readdir($rep)) {
    if($file != '..' && $file !='.' && $file !='') {
    if (!is_dir($file)){
    
    // the file
    //$file = 'band.jpg';
    $filename = "./images/" . "$file";
    $percent = .50; // scale down
    $imagethumbsize = 50; // thumbnail size
    // content type
    header('Content-type: image/jpeg');
    
    // the new dimensions
    list($width, $height) = getimagesize($file);
    $new_width = $width * $percent;
    $new_height = $height * $percent;
    
    // resample
    $image_p = imagecreatetruecolor($imagethumbsize , $imagethumbsize); 
    $image = imagecreatefromjpeg($file);
    
    // the trick
    imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
    
    // the output
    
    imagejpeg($image_p, "./thumbs/$file", 100);
    imagedestroy($image_p);
    echo "<img src=./thumbs/$file> <img src=$filename> <br>";
    }
    }
    }
    closedir($rep);
    clearstatcache();
    ?>
    robert koons

  2. #2
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: another image problem

    hmmm... without testing the code my self, i am getting the fealing it is a problem with one of the variables. i think what is happening is that a variable is not getting cleared or somthing, which intern is either overfilling it or creating an error, whn you try and do somthing with it again.

    i dont personally know PHP all that well, so i dont know how, or what this line even does:
    Code:
    imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
    but check that line over and see if one of those variables is not correct when it is running though it.

    put an echo in it where you can see the variables your self, somthing like:
    Code:
    echo "{put all variables here}\n";

  3. #3
    pegboy's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Aug 2001
    Location
    Knoxville, TN
    Posts
    76

    Re: another image problem

    i dont personally know PHP all that well, so i dont know how, or what this line even does:
    Code:
    imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
    that line does all the work. copies the original images, reduces it by 50%, and crops out a 50x50 thumbnail.

    then, it's saved here:
    Code:
    imagejpeg($image_p, "./thumbs/$file", 100);
    i think part of the problem is in the top part.
    Code:
    $upload_dir = "./images/";
    $rep=opendir($upload_dir);
    while ($file = readdir($rep)) {
    if($file != '..' && $file !='.' && $file !='') {
    if (!is_dir($file)){
    ... because it works when i copied the images to the directory that the script sets in.
    robert koons

  4. #4
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662

    Re: another image problem

    Hi,

    Code:
    $image = imagecreatefromjpeg($file);
    SHouldn't that be $filename? I may be wrong.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  5. #5
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: another image problem

    i think i found it, although i dont have a php program to test it on anymore, it is in this line:
    Code:
    while ($file = readdir($rep))
    you are saying "run though loop untill $file equals the number of files in that directory"

    you never said what $files contains, you could try somthing like this:
    Code:
    <?php
    // open dircetory
    $upload_dir = "./images/";
    $rep=opendir($upload_dir);
    $file = readdir($rep)
    while($file != ''{
    $file = readdir($rep)
    if($file != '..' && $file !='.') {
    if (!is_dir($file)){
    
    // the file
    //$file = 'band.jpg';
    $filename = "./images/" . "$file";
    $percent = .50; // scale down
    $imagethumbsize = 50; // thumbnail size
    // content type
    header('Content-type: image/jpeg');
    
    // the new dimensions
    list($width, $height) = getimagesize($file);
    $new_width = $width * $percent;
    $new_height = $height * $percent;
    
    // resample
    $image_p = imagecreatetruecolor($imagethumbsize , $imagethumbsize); 
    $image = imagecreatefromjpeg($file);
    
    // the trick
    imagecopyresampled($image_p, $image, -($new_width/2) + ($imagethumbsize/2), -($new_height/2) + ($imagethumbsize/2), 0, 0, $new_width , $new_width , $width, $height);
    
    // the output
    
    imagejpeg($image_p, "./thumbs/$file", 100);
    imagedestroy($image_p);
    echo "<img src=./thumbs/$file> <img src=$filename> <br>";
    }
    }
    }
    closedir($rep);
    clearstatcache();
    ?>

  6. #6
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662

    Re: another image problem

    The while loops work as-is as $file is set to each entry in the directory for each iteration of the loop.

    So if the directory contains a.jpg and b.jpg, then the first time the loop goes round, $file = a.jpg. The second time, $file = b.jpg

    That shouldn't be the problem, I think.
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  7. #7
    pegboy's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Aug 2001
    Location
    Knoxville, TN
    Posts
    76

    Re: another image problem

    well, i reworked my original idea... instead of uploading a batch of photos, then having then cropped i'm doing the crop function directly after the photo is uploaded. i'm not sure why i didn't do that in the first place.

    i do have another problem though...

    right now i'm reading the thumbs directory and then printing all the thumbs to a page....
    Code:
    $upload_dir = "../images/thumbs/";
    $rep=opendir($upload_dir);
    while ($file = readdir($rep)) {
    if($file != '..' && $file !='.' && $file !='') {
    if (!is_dir($file)){
    list($width, $height) = getimagesize("../images/photos/$file");
    echo " <a href=\"#\" onClick=\"window.open('../photo.php?id=$file', 'pic', config='height=$height,width=$width,')\"><img src=\"../images/thumbs/$file\" width=\"50\" height\"50\" border=\"0\" class\"border\" hspace=\"3\" vspace=\"3\"></a> ";
    }
    }
    }
    closedir($rep);
    clearstatcache();
    it's works, but i'd rather put each image into a table cell. i can't figure out how to print four cells per row.
    robert koons

  8. #8
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: another image problem

    Code:
    <?php
    $upload_dir = "./images/";
    $rep=opendir($upload_dir);
    echo "<table>\n";
    while ($file = readdir($rep)) {
    if($file != '..' && $file !='.' && $file !='') {
    if (!is_dir($file)){
    list($width, $height) = getimagesize("../images/photos/$file");
    echo "\n$i\n";
    if(($i % 4 == 0)&&($i!=0)){
    echo "<tr>";
    }
    $i++;
    echo "\n<td><a href=\"#\" onClick=\"window.open('../photo.php?id=$file', 'pic', config='height=$height,width=$width,')\"><img src=\"../images/thumbs/$file\" width=\"50\" height\"50\" border=\"0\" class\"border\" hspace=\"3\" vspace=\"3\"></a></td>";
    }
    }
    }
    echo "\n</table>";
    ?>

  9. #9
    pegboy's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Aug 2001
    Location
    Knoxville, TN
    Posts
    76

    Re: another image problem

    you just made my x-mas list.

    thanks again!
    robert koons

  10. #10
    ALL's Avatar
    Super Dooper Nerd

    Status
    Offline
    Join Date
    Feb 2005
    Location
    localhost
    Posts
    382

    Re: another image problem

    no problem



Tags for this Thread