SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    creating thumbnails?

    Given a folder of images
    http://fixmysite.us/masterasp/providers/images/1/
    I'm trying to creaate thumbnail versions and put them within the thumbs directory in that folder
    heres my scrit
    PHP Code:
    createThumbs($dir."/",$dir."/thumbs/",100); 
    and above is where I define the function
    PHP Code:
    function createThumbs$pathToImages$pathToThumbs$thumbWidth 
    {
      
    // open the directory
      
    $dir opendir$pathToImages );

      
    // loop through it, looking for any/all JPG files:
      
    while (false !== ($fname readdir$dir ))) {
        
    // parse path for the extension
        
    $info pathinfo($pathToImages $fname);
        
    // continue only if this is a JPEG image
        
    if ( strtolower($info['extension']) == 'jpg' 
        {
          
    // load image and get image size
          
    $img imagecreatefromjpeg"{$pathToImages}{$fname});
          
    $width imagesx$img );
          
    $height imagesy$img );

          
    // calculate thumbnail size
          
    $new_width $thumbWidth;
          
    $new_height floor$height * ( $thumbWidth $width ) );

          
    // create a new temporary image
          
    $tmp_img imagecreatetruecolor$new_width$new_height );

          
    // copy and resize old image into new image 
          
    imagecopyresized$tmp_img$img0000$new_width$new_height$width$height );

          
    // save thumbnail into a file
          
    imagejpeg$tmp_img"{$pathToThumbs}{$fname});
        }
      }
      
    // close the directory
      
    closedir$dir );

    when I run it, I get this?

    Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home/fixmy1/public_html/masterasp/providers/insert.php on line 16

    Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: 'images/1/1370928156-mockup3.jpg' is not a valid JPEG file in /home/fixmy1/public_html/masterasp/providers/insert.php on line 16

    Warning: imagesx() expects parameter 1 to be resource, boolean given in /home/fixmy1/public_html/masterasp/providers/insert.php on line 17

    Warning: imagesy() expects parameter 1 to be resource, boolean given in /home/fixmy1/public_html/masterasp/providers/insert.php on line 18

    Warning: Division by zero in /home/fixmy1/public_html/masterasp/providers/insert.php on line 22

    Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/fixmy1/public_html/masterasp/providers/insert.php on line 25

    Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in /home/fixmy1/public_html/masterasp/providers/insert.php on line 28

    Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /home/fixmy1/public_html/masterasp/providers/insert.php on line 31

    I dont understand what that error means...
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You most likely have non-jpeg image with jpg extension. While most image viewing applications and all browsers will display it correctly, GD can not process it. Please check the type of all images in that folder. Either that, or the image is damaged in some way so GD can't recognize it as jpeg.

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, the image comes to me as a .png, but its an image upload form so they could also send me a bmp, tiff, gif or anything really. I think I need to convert it to jpg format to protect myself, this changes the extension for my png.
    PHP Code:
            $new str_replace('.png''.jpg'$tmp); 
    So im guessing this is a problem, is there a way to;
    first test If im even dealing with an image?
    If I am change the format to a jpg?
    Thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It is not enough to change extension, as JPG and PNG are completely different formats (JPEG is lossy compression and PNG is lossless). I usually check extension and then if it's jp(e)g I use imagecreatefromjpeg, if it's png, then imagecreatefrompng, gif - imagecreatefromgif, and so on... however that won't help you if you have PNG image with .jpg extension.

  5. #5
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, that makes sense to me.
    ///////So I gather this is a better way to check the image type
    PHP Code:
    $ext pathinfo($imgPATHINFO_EXTENSION);        
    if (
    $ext == "png") {
        
    $new imagecreatefrompng($img);
    } elseif  (
    $ext ==  "gif") {
        
    $new imagecreatefromgif($img);
    } elseif (
    $ext == "jpg") {
        
    $new imagecreatefromjpeg($img);
    } elseif (
    $ext == "bmp") {
        
    $new imagecreatefromwbmp($img);

    is this what you mean?
    "Oh, and Jenkins--apparently your mother died this morning."

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, something like that. Just note that next to "jpg", valid JPEG extensions are: jpeg, jpe, jfif, jfi and jif, though I must admit that I haven't seen jfif in ages, and I can't recall ever seeing jif/jfi

    Src: https://en.wikipedia.org/wiki/JPEG#J...ame_extensions


Bookmarks

Posting Permissions

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