Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Creating thumbnails

    Hi,

    I have a picture upload script. I'd like to add automatic thumbnail creation.

    I tried to adapt this code, but I'm getting some errors.

    This is my upload script
    PHP Code:
    $i $teller['aantal'];
    $NewPosition $teller['MaxPosition'];
                
                while(list(
    $key,$value) = each($_FILES[images][name]))
                  {
                      
    $i++;
                      
    $NewPosition++;
                      if(!empty(
    $value))
                              {
                                  list(
    $width) = getimagesize($_FILES['images']['tmp_name'][$key]);
                                  
    $filename $value;
                                  
                                  if(
    $width 900)
                                  {
                                      echo 
    "<div class='fout'>
                                              <img src='../images/error-icon.png' alt='Error' width='16' height='16'/>Error message</div>
                                          <p>&nbsp;</p>"
    ;
                                    
    /*== uploaded file verwijderen ==*/
                                    
    unlink($filename);
                                  }
                                  else
                                  {
                                      
    $filename $muz_foto."-".$i.".jpg";
                                      
    $add "../images/band/$muz_foto/$filename";
                                      echo 
    "<div class='succesvol'>Succes message</div>
                                            <p>&nbsp;</p>"
    ;
                                            
                                      
    copy($_FILES[images][tmp_name][$key], $add);
    //thumbnail                                  
                                      
    createthumb('../images/band/'.$muz_foto.'/'.$filename,'../images/band/'.$muz_foto.'/thumbs/'.$filename,200,200);
                                      

                                      
                                  }
                      }
                  } 
    This is the function:
    PHP Code:
    function createthumb($orig_img,$thumbnail,$new_w,$new_h)
    {
        echo
    "bestandsnaam = ".$orig_img." ";
        
    $system=explode(".",$orig_img);
        if (
    preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($orig_img);}
        if (
    preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($orig_img);}
        print 
    $src_img;
        
    $old_x=imageSX($src_img);
        
    $old_y=imageSY($src_img);
        if (
    $old_x $old_y
        {
            
    $thumb_w=$new_w;
            
    $thumb_h=$old_y*($new_h/$old_x);
        }
        if (
    $old_x $old_y
        {
            
    $thumb_w=$old_x*($new_w/$old_y);
            
    $thumb_h=$new_h;
        }
        if (
    $old_x == $old_y
        {
            
    $thumb_w=$new_w;
            
    $thumb_h=$new_h;
        }
        
    $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
        
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
        if (
    preg_match("/png/",$system[1]))
        {
            
    imagepng($dst_img,$thumbnail); 
        } else {
            
    imagejpeg($dst_img,$thumbnail); 
        }
        
    imagedestroy($dst_img); 
        
    imagedestroy($src_img); 

    I'm getting these errors:
    Code:
    bestandsnaam = ../images/band/xandee/xandee-6.jpg
    Warning: imagesx() expects parameter 1 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 120
    
    Warning: imagesy() expects parameter 1 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 121
    
    Warning: imagecopyresampled() expects parameter 2 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 138
    
    Warning: imagedestroy() expects parameter 1 to be resource, null given in G:\xampp\htdocs\drfred2\includes\functions.php on line 146
    So it looks like there's a problem with $src_img, but I can't figure out what it is exactly.
    Maybe the parameters/paths here should be different??
    PHP Code:
    createthumb('../images/band/'.$muz_foto.'/'.$filename,'../images/band/'.$muz_foto.'/thumbs/'.$filename,200,200); 
    GD info
    Code:
    GD Support 	enabled
    GD Version 	bundled (2.0.34 compatible)
    FreeType Support 	enabled
    FreeType Linkage 	with freetype
    FreeType Version 	2.3.11
    T1Lib Support 	enabled
    GIF Read Support 	enabled
    GIF Create Support 	enabled
    JPEG Support 	enabled
    libJPEG Version 	7
    PNG Support 	enabled
    libPNG Version 	1.2.40
    WBMP Support 	enabled
    XBM Support 	enabled
    JIS-mapped Japanese Font Support 	enabled
    
    Directive	Local Value	Master Value
    gd.jpeg_ignore_warning	0	0
    Thank you in advance
    Last edited by friz; 12-06-2010 at 10:05 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Looks like $src_img isn't an image resource. Probably because neither of your "if" conditions evaluate to TRUE, so $src_img doesn't get set to anything. You should probably put the call to imageSX() within the "if" statements, or within another "if" statement of its own, to handle this situation.

  • Users who have thanked Fumigator for this post:

    friz (12-06-2010)

  • #3
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Thx for the (very quick) reply.

    It took it out of the "if" and that worked

    PHP Code:
    if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($orig_img);} 
    to
    PHP Code:
    $src_img=imagecreatefromjpeg($orig_img); 
    Although I don't really understand why.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    copy/paste FTW I guess

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    There's another thread here from earlier along the same lines:

    Image upload nightmare!

    The function may be useful to you.

  • #6
    New Coder
    Join Date
    Oct 2007
    Posts
    73
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    copy/paste FTW I guess
    Me changing the code was probably the problem.
    Anyway, I'll put een file type check in my upload script. I don't need to copy/paste for that

    Quote Originally Posted by MattF View Post
    There's another thread here from earlier along the same lines:

    Image upload nightmare!

    The function may be useful to you.
    That script from blazonry was my first google result, but this one above looked better for what I needed/know (I thought)

    I will certainly have a look at the script you provided. Looks like I can learn from that.
    Thx!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •