Resizing image issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Resizing image issue

Post by genista »

Hi all,

I have been given a function that reads a folder and then creates thumbnails for every image in that folder, placing the thumb elsewhere. I now need to modify it so that it looks for one image only, and the value of that image is stored in a database. the script works, until now. Therefore I have posted it before, and then after so you can see where I am at:

Code: Select all

$pathToImages = "files/";
	$pathToThumbs = "thumbs/";
	function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
  while (false !== ($fname1 = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $image);

    // continue only if this is a JPEG image
    if (( strtolower($info['extension']) == 'jpg' )|| ( strtolower($info['extension']) == 'gif' ))
    {
      echo "Creating thumbnail for {$image} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$image}" );
      $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}{$image}" );
    }
  }
  // close the directory
  closedir( $dir );


// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("files/","thumbs/",100);
}
Now here it is with $image1 incorporated and it is not working, I cannot echo any of the variables including $info, $image1 to begin debugging, I have no error messages and no thumbs created in the thumbs folder, please see any notes in CAPITALS:

Code: Select all

$pathToImages = "files/";
	$pathToThumbs = "thumbs/";
	function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );

  // loop through it, looking for any/all JPG files:
//////NOTICE HERE THAT I HAVE COMMENTED OUT THE PART WHERE IT SEARCHES THE ENTIRE DIRECTORY, MAYBE I SHOULD LEAVE IT IN?
  //while (false !== ($fname1 = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $image1);

    // continue only if this is a JPEG image
    if (( strtolower($info['extension']) == 'jpg' )|| ( strtolower($info['extension']) == 'gif' ))
    {
      echo "Creating thumbnail for {$image1} <br />";

      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$image1}" );
      $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}{$image1}" );
    }
  }
  // close the directory
  closedir( $dir );


// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("files/","thumbs/",100);
}
Any ideas?

Thanks,


G
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I think that instead of commenting out parts of a script that used to work, you start debugging it. Exactly where does it stop working?
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Its interesting because I have tried printing things like $img_file and some other variables, but when I try and echo them inside the code nothing gets printed, when I print outside I get undefined variable errors, making it impossible to debug as things stand.

Any suggestions?

Thanks,

G
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

That probably means that either the script isn't getting that far, or those variables are empty. Try marking each step of the script with an echo until you see where it stops so we know what to look at.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Ok, I have had a change and now I have got some errors being output:

Undefined variable on this line:

Code: Select all

$info = pathinfo($pathToImages . $image1);

$image1 could be sunset.jpg for example, whatever value is stored in mysql database, while the file itself is in a server folder called files/ (which I have defined against $pathtoimages).

After that I get "Undefined index: extension" from this line:

Code: Select all

if (( strtolower($info['extension']) == 'jpg' )|| ( strtolower($info['extension']) == 'gif' ))
    {

But then if it cant get image1 it cant go on and get the extension from it, so solving problem 1 should solve problem 2.

Any ideas?

Thanks,


G
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

If PHP says it's undefined, it means you haven't defined it yet. Echo everything that PHP says is undefined and tell me what you get.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

ok when I try and echo $image1 in the function there is nothing there, when I echo $image1 outside fo the function it is fine, I get sunset.jpg for example.

SO $image1 is being lost inside the function...
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Are you ever sending $image1 to the function? Functions can only access variables within their scope.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

I have tried:

Code: Select all

function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth, $IMAGE1 )
{
But I get a missing argument message, so even at the tp level image1 is not getting passed through, how could I do that?

Thanks,

G
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

I'm not sure that you understand the way that functions work.

PHP For Beginners - Functions

Give it a look. You have to pass $image1 as a parameter INTO the function. The function names that parameters whatever it wants to.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Ok, its late here, a good sleep and a fresh approach should get me going, thanks for your help and hanging in there! I'll pick it up tomorrow.


G
Post Reply