Jump to content


Photo

resize only works for width not height


  • Please log in to reply
5 replies to this topic

#1 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 15 June 2006 - 09:24 PM

the following code works if the width is more than the width i state, but if the height is more it does nothing.

if image is 600x200 and i use the following...
function resize_jpeg_image($src_image, $dst_image, 100, 150);
then it only goes to the size around 500x100 or near this, i need both x < 100 and y < 150.
how do i change the following to do this, or is there another code that does this that i should use?

thanks





<?
function resize_jpeg_image($src_image, $dst_image, $max_width, $max_height)
{
if (!($src_img = imagecreatefromjpeg($src_image)))
return 0;
if (imagesx($src_img) > $max_width || imagesy($src_img) > $max_height)
{
$origwidth = imagesx($src_img);
$origheight = imagesy($src_img);
$scale = $max_width / $origwidth;
$height = $scale * $origheight;
$width = $scale * $origwidth;

if (!($dst_img = imagecreatetruecolor($width, $height))
|| !imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $origwidth, $origheight)
|| !imagejpeg($dst_img, $dst_image )
|| !imagedestroy($dst_img))
return 0;
}
else if (!copy($src_image, $dst_image))
return 0;

return 1;
}
?>

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 June 2006 - 09:50 PM

You to rescale differently depending on which dimension is oversized:

$scale = 1;
$origwidth = imagesx($src_img);
$origheight = imagesy($src_img);
if ($origwidth > $max_width) {
    $scale = $max_width / $origwidth;
} else {
if ($origheight > $max_height) {
    $scale = $max_height / $origheight;
}
$height = $scale * $origheight;
$width = $scale * $origwidth;

Legend has it that reading the manual never killed anyone.
My site

#3 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 15 June 2006 - 11:26 PM

errors??

i have tried changing what you gave to this...


<?
function resize_jpeg_image($src_image, $dst_image, $max_width, $max_height)
{
if (!($src_img = imagecreatefromjpeg($src_image)))
return 0;
if (imagesx($src_img) > $max_width || imagesy($src_img) > $max_height)
{
$scale = 1;
$origwidth = imagesx($src_img);
$origheight = imagesy($src_img);
if ($origwidth > $max_width && ($max_height / $origheight) < $max_width) {
$scale = $max_width / $origwidth;
} else {
$scale = $max_width / $origwidth;
}
if ($origheight > $max_height && ($max_width / $origwidth) < $max_height) {
$scale = $max_height / $origheight;
} else {
$scale = $max_height / $origheight;
}
$height = $scale * $origheight;
$width = $scale * $origwidth;

if (!($dst_img = imagecreatetruecolor($width, $height))
|| !imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $origwidth, $origheight)
|| !imagejpeg($dst_img, $dst_image )
|| !imagedestroy($dst_img))
return 0;
}
else if (!copy($src_image, $dst_image))
return 0;

return 1;
}
?>





but still not getting anywhere fast!!

need to check both dimension

if image is 497x176 and i only allow 200 x 110 it reduce to 310x100

i need it to check both so the smallest size is allowed.

so it reduces to 200x71

all figures are approx, as long as i can state a maximum size both ways.


thank you in advance


[!--quoteo(post=384383:date=Jun 15 2006, 10:50 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 15 2006, 10:50 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You to rescale differently depending on which dimension is oversized:

$scale = 1;
$origwidth = imagesx($src_img);
$origheight = imagesy($src_img);
if ($origwidth > $max_width) {
    $scale = $max_width / $origwidth;
} else {
if ($origheight > $max_height) {
    $scale = $max_height / $origheight;
}
$height = $scale * $origheight;
$width = $scale * $origwidth;
[/quote]


#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 15 June 2006 - 11:40 PM

You can get a resize function from php.net

Just go to http://us2.php.net/m...pyresampled.php and look at the second example.

NoGray.com


#5 jasonc

jasonc
  • Members
  • PipPipPip
  • Advanced Member
  • 841 posts

Posted 16 June 2006 - 12:30 AM

hi yes that works the same way my original script does.

i need both height and width to be below or equaly to the maximum allow,

so if the image is 400x100

and i only allow 100x100 using the above script and the one i already use leaves the images the same and one of the conditions apply, i need both conditions to apply so it will resize to 100x25. just having one or the other condition apply is not what i need as the image goes off the page if it is lets say 1000x100

i can not seem to get my head around the if else, this is where it goes wrong, i have been working on it for a few days, but still not getting it right, any ideas?

cheers





[!--quoteo(post=384405:date=Jun 16 2006, 12:40 AM:name=nogray)--][div class=\'quotetop\']QUOTE(nogray @ Jun 16 2006, 12:40 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You can get a resize function from php.net

Just go to [a href=\"http://us2.php.net/manual/en/function.imagecopyresampled.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.imagecopyresampled.php[/a] and look at the second example.
[/quote]


#6 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 16 June 2006 - 12:32 AM

Look here...

[a href=\"http://www.nstoia.com/toh/technical/imageresize/\" target=\"_blank\"]http://www.nstoia.com/toh/technical/imageresize/[/a]

Lite...


all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users