Jump to content

Help with my function to make a thumbnail


elkidogz

Recommended Posts

ok, I'm pretty raw to PHP, but basically what i am doing here is making a thumbnail of an image that was uploaded.

the codes here:

function createAThumb( $pathToImages, $pathToThumbs, $fname, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );
    // parse path for the extension
  $info = pathinfo($pathToImages . $fname);
    // continue only if this is a JPEG image
    if ( strtolower($info['extension']) == 'jpg' )
    {
        //echo "Creating thumbnail for {$fname} <br />";

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

 

where i am getting messed up is the $fname it's getting over wrote by the

 

  $info = pathinfo($pathToImages . $fname); line and i need that to make sure it's a jpg for the image to be resized.

 

what should i try

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.