jasonc Posted June 15, 2006 Share Posted June 15, 2006 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;}?> Quote Link to comment https://forums.phpfreaks.com/topic/12108-resize-only-works-for-width-not-height/ Share on other sites More sharing options...
AndyB Posted June 15, 2006 Share Posted June 15, 2006 You to rescale differently depending on which dimension is oversized:[code]$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;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12108-resize-only-works-for-width-not-height/#findComment-46097 Share on other sites More sharing options...
jasonc Posted June 15, 2006 Author Share Posted June 15, 2006 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 dimensionif image is 497x176 and i only allow 200 x 110 it reduce to 310x100i need it to check both so the smallest size is allowed.so it reduces to 200x71all 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) [snapback]384383[/snapback][/div][div class=\'quotemain\'][!--quotec--]You to rescale differently depending on which dimension is oversized:[code]$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;[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/12108-resize-only-works-for-width-not-height/#findComment-46117 Share on other sites More sharing options...
nogray Posted June 15, 2006 Share Posted June 15, 2006 You can get a resize function from php.netJust go to http://us2.php.net/manual/en/function.imagecopyresampled.php and look at the second example. Quote Link to comment https://forums.phpfreaks.com/topic/12108-resize-only-works-for-width-not-height/#findComment-46119 Share on other sites More sharing options...
jasonc Posted June 16, 2006 Author Share Posted June 16, 2006 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 400x100and 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 1000x100i 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) [snapback]384405[/snapback][/div][div class=\'quotemain\'][!--quotec--]You can get a resize function from php.netJust 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] Quote Link to comment https://forums.phpfreaks.com/topic/12108-resize-only-works-for-width-not-height/#findComment-46129 Share on other sites More sharing options...
litebearer Posted June 16, 2006 Share Posted June 16, 2006 Look here...[a href=\"http://www.nstoia.com/toh/technical/imageresize/\" target=\"_blank\"]http://www.nstoia.com/toh/technical/imageresize/[/a]Lite... Quote Link to comment https://forums.phpfreaks.com/topic/12108-resize-only-works-for-width-not-height/#findComment-46130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.