Jump to content

getimagesize() only works sometimes?! Please help


br3nn4n

Recommended Posts

I have some really simple code (that needs to be reworked actually) that just takes a JPEG image and resizes it.

 

For SOME REASON it will only work on maybe half the files I feed it. It returns nothing otherwise for no reason.

 

The errors come out as:

 

Notice: Undefined variable: new_width in /hermes/bosweb/web019/b193/ipw./public_html/img.php on line 28

 

Notice: Undefined variable: new_height in /hermes/bosweb/web019/b193/ipw./public_html/img.php on line 28

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /hermes/bosweb/web019/b193/ipw./public_html/img.php on line 28

 

Notice: Undefined variable: new_width in /hermes/bosweb/web019/b193/ipw./public_html/img.php on line 30

 

Notice: Undefined variable: new_height in /hermes/bosweb/web019/b193/ipw./public_html/img.php on line 30

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /hermes/bosweb/web019/b193/ipw./public_html/img.php on line 30

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /hermes/bosweb/web019/b193/ipw./public_html/img.php on line 33

 

My script is as follows:

 

<?PHP
// The file
$src=$_GET['src'];
if ($src == "") {
$src = "noimage"; 
   }
$filename = "images/articles/" . $src . ".jpg";

// Get new dimensions
list($width, $height) = getimagesize($filename);

if ($width > $height) {
$new_width=350;
$new_height=($height/$width)*350;
} 


// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);


$end = imagejpeg($image_p,'',70);

?>

 

It works for some images, it doesn't for others...? Any idea why? I really need it to always work.

 

Thanks so much in advance!

Link to comment
Share on other sites

I think I see the error.

 

You first get the width & height of the image and store the values in $width & $height

list($width, $height) = getimagesize($filename);

 

Then you have this IF statment that will set the values of $new_width and $new_height

if ($width > $height) {
$new_width=350;
$new_height=($height/$width)*350;
} 

 

Then later in the code you attempt to use additional image functions using $new_width & $new_height.

 

But, if the IF statment was not true (i.e. The width is not greater than height) then the variables $new_width & $new_height are never defined!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.