Jump to content


PHP Problem (getimagesize) error


8 replies to this topic

#1 Raven-X

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:New Jersey
  • Interests:I love having fun, programming, web design, graphic design, gaming, anime, manga, and being a student (love learning new software).

Posted 20 July 2008 - 08:34 PM

Basically I was using this site as a reference: http://www.sitepoint...ge-resizing-php

Tried using that script but it's just not resizing the image at all. I've fixed all the other errors, including comment errors made on part by the author.

http://www.anime-cod...etimagesize.txt
http://www.anime-cod...etimagesize.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>GetImageSize Test</title>
</head>

<body>

<?php

function image_resize($width, $height, $target) {

//takes the larger size of the width and height and applies the  
//formula accordingly...this is so this script will work  dynamically with any size image

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);

//returns the new sizes in html image tag format...this is so you
//can plug this function inside an image tag and just get the

return "width=\"$width\" height=\"$height\"";

}

?>


<?php

//get the image size of the picture and load it into an array
$mysock = getimagesize("http://www.anime-coders.com/php/images/sock001.jpg");

?>

<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" <?php image_resize($mysock[0],  
$mysock[1], 150); ?>>


</body>
</html>

I'm new to programming concepts and struggling to get stronger in it but it's a fight I will win. I'm just not sure at all what is going on right now. I already checked with the host support team and they said the function was enabled before anyone asks.

#2 Arsenal19

    Young Padawan

  • Members
  • Pip
  • 41 posts

Posted 20 July 2008 - 10:23 PM

I have done this one before, I'd say search p2l for some image resizing scripts. I've seen several on here.

Check out PHP.nets website under getimagesize. There are several examples there, that will help you.

There doesn't seem to be anything wrong with the php code you have there.

Arsenal19

Edited by Arsenal19, 20 July 2008 - 10:24 PM.


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 21 July 2008 - 12:02 PM

Well, it's hard to help when you don't know what the problem is. Thankfully for you though, this was a reasonably short script (and the tutorial at least explained what you were trying to do).

Here's something that should work for you:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>GetImageSize Test</title>
</head>

<body>

<?php
function image_resize($width, $height, $target) {
	//takes the larger size of the width and height and applies the  
	//formula accordingly...this is so this script will work  dynamically with any size image

	if ($width > $height) {
		$percentage = ($target / $width);
	} else {
		$percentage = ($target / $height);
	}

	//gets the new value and applies the percentage, then rounds the value
	$width = round($width * $percentage);
	$height = round($height * $percentage);

	//returns the new sizes in html image tag format...this is so you
	//can plug this function inside an image tag and just get the

	return "width=\"$width\" height=\"$height\"";
}

//get the image size of the picture and load it into an array
$mysock = array(imagesx('images/sock001.jpg'),
		imagesy('images/sock001.jpg'));
?>

<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" <?php echo image_resize($mysock[0],  
$mysock[1], 150); ?>>

</body>
</html>
I changed 2 things.

1. Removed getimagesize() in favor of imagesx() and imagesy() (also longer accessing the image via a http request but a direct path to the image on your server). Reason: getimagesize() returns more info that necessary for something like this (it's a matter of performance and simplicity basically).
2. image_resize() returned the HTML attributes that you wanted. Since it was only returning the info, nothing was happening, you have to echo the result of the image_resize() function.

#4 Raven-X

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:New Jersey
  • Interests:I love having fun, programming, web design, graphic design, gaming, anime, manga, and being a student (love learning new software).

Posted 21 July 2008 - 06:34 PM

Thanks for the reply.

I tried it but it didn't quite work the way we expected it to...http://www.anime-coders.com/php/getimagesize.php

It shows the errors.

#5 PurdueKenny

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 21 July 2008 - 10:40 PM

It has been resolved. He wasn't calling or defining any variables.

Edited by PurdueKenny, 21 July 2008 - 10:42 PM.


#6 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 23 July 2008 - 01:17 AM

View PostPurdueKenny, on Jul 21 2008, 08:40 PM, said:

It has been resolved. He wasn't calling or defining any variables.
:biggrin:

1. PHP doesn't require that you define variables (perks of being an interpreted language).
2. He is calling the variables just fine. The issue here lies in what variables are being called and whether or not they were set correctly.

Since it appears as things are actually not working though, here is an updated bit of code to help debug the situation.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>GetImageSize Test</title>
</head>

<body>

<?php
function image_resize($width, $height, $target) {
	//takes the larger size of the width and height and applies the  
	//formula accordingly...this is so this script will work  dynamically with any size image

	if ($width > $height) {
		$percentage = ($target / $width);
	} else {
		$percentage = ($target / $height);
	}

	//gets the new value and applies the percentage, then rounds the value
	$width = round($width * $percentage);
	$height = round($height * $percentage);

	//returns the new sizes in html image tag format...this is so you
	//can plug this function inside an image tag and just get the

	return "width=\"$width\" height=\"$height\"";
}

if(!is_file('images/sock001.jpg')){
	die('images/sock001.jpg is not a file in: '.$_SERVER['DOCUMENT_ROOT']);
}

//get the image size of the picture and load it into an array
$mysock = array(imagesx('images/sock001.jpg'),
		imagesy('images/sock001.jpg'));

echo '<pre>'.htmlspecialchars(print_r($mysock,1),1).'</pre>';
?>

<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" <?php echo image_resize($mysock[0],  
$mysock[1], 150); ?>>

</body>
</html>
Copy/paste the output of that code here (the actual source for the resulting <img> tag would be convenient to have as well).

#7 Raven-X

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:New Jersey
  • Interests:I love having fun, programming, web design, graphic design, gaming, anime, manga, and being a student (love learning new software).

Posted 23 July 2008 - 08:09 AM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>GetImageSize Test</title>
</head>

<body>

<br />
<b>Warning</b>:  imagesx(): supplied argument is not a valid Image resource in <b>/home/deright/public_html/php/getimagesize.php</b> on line <b>36</b><br />

<br />
<b>Warning</b>:  imagesy(): supplied argument is not a valid Image resource in <b>/home/deright/public_html/php/getimagesize.php</b> on line <b>37</b><br />
<pre>Array
(
	[0] =&gt; 
	[1] =&gt; 
)
</pre>
<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" <br />

<b>Warning</b>:  Division by zero in <b>/home/deright/public_html/php/getimagesize.php</b> on line <b>18</b><br />
width="0" height="0">

</body>
</html>
http://www.anime-cod...etimagesize.php

Edited by Raven-X, 23 July 2008 - 08:10 AM.


#8 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 23 July 2008 - 12:11 PM

Now that i can work with. Unfortuantely, i'm an idiot and forgot that imagesx() takes in image resource, not a path to an image... So we'll switch back to getimagesize() because i'm too lazy to make a resource :biggrin:
//get the image size of the picture and load it into an array
$mysock = getimagesize('images/sock001.jpg');
Just change that line (where $mysock = array()) to what is above. That should produce something useful (maybe even something that works).

Edited by rc69, 23 July 2008 - 12:12 PM.


#9 Raven-X

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:New Jersey
  • Interests:I love having fun, programming, web design, graphic design, gaming, anime, manga, and being a student (love learning new software).

Posted 23 July 2008 - 09:14 PM

Almost there but code shows up on the page now, lol.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

<title>GetImageSize Test</title>
</head>

<body>

<pre>Array
(
	[0] =&gt; 230
	[1] =&gt; 240
	[2] =&gt; 2
	[3] =&gt; width="230" height="240"
	[bits] =&gt; 8
	[channels] =&gt; 3
	[mime] =&gt; image/jpeg
)

</pre>
<!--using a standard html image tag, where you would have the  
width and height, insert your new imageResize() function with  
the correct attributes -->

<img src="images/sock001.jpg" width="120" height="125">

</body>
</html>

http://www.anime-cod...etimagesize.php





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users