Jump to content

Issues with image resize - so close


genista

Recommended Posts

Hi, The following code is giving me a series of errors and headaches, the errors of which are posted after. It is a script that takes an image and turns it into a thumbnail. the image in question is stored in a file system while the value is called from a database ($image1 would be hello.jpg):

 

$pathToImages = "files/";
$pathToThumbs = "thumbs/";

  // open the directory
  $dir = opendir( $pathToImages );

    // 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' ))
    {
     
     getimagesize($pathToImages.$image1);


      $height = $image1[1];
        $width = $image1[0];
        
          $scale = min(100/$width, 100/$height);

          
          # If the image is larger than the max shrink it
        	if ($scale < 1) {
          $width = floor($scale*$width);
          $height = floor($scale*$height);
        	}

      // create a new temporary image

  $tmp_img = imagecreatetruecolor( $width, $height );
      // copy and resize old image into new image
      imagecopyresized( $tmp_img, 0, 0, 0, 0, $width, $height );


      // save thumbnail into a file
      imagejpeg( $tmp_img, "{$pathToThumbs}{$image1}" );
    }

 

The errors I get are:

 

"Division by zero" on this line:

 

  $scale = min(100/$width, 100/$height);

 

"Wrong parameter count for imagecopyresized() " from this line:

 

imagecopyresized( $tmp_img, 0, 0, 0, 0, $width, $height );

 

and finally:

 

"imagejpeg(): supplied argument is not a valid Image resource" from this line:

 

imagejpeg( $tmp_img, "{$pathToThumbs}{$image1}" );

 

Looking at my code and echoing bits out I believe my issue is due to gettting details from the variable: $image1, but I might be wrong.

 

Anyone have any ideas?

 

 

Thanks,

 

 

G

Link to comment
https://forums.phpfreaks.com/topic/57569-issues-with-image-resize-so-close/
Share on other sites

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.