Creating thumbnails?

Given a folder of images
I’m trying to creaate thumbnail versions and put them within the thumbs directory in that folder
heres my scrit


and above is where I define the function

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, $img, 0, 0, 0, 0, $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…

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.

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.

        $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?

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.

ok, that makes sense to me.
///////So I gather this is a better way to check the image type

$ext = pathinfo($img, PATHINFO_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?

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 :slight_smile: