Jump to content

Archived

This topic is now archived and is closed to further replies.

jasonc

resize only works for width not height

Recommended Posts

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;
}
?>

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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) [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]

Share this post


Link to post
Share on other sites
You can get a resize function from php.net

Just go to http://us2.php.net/manual/en/function.imagecopyresampled.php and look at the second example.

Share this post


Link to post
Share on other sites
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) [snapback]384405[/snapback][/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]

Share this post


Link to post
Share on other sites
Look here...

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

Lite...

Share this post


Link to post
Share on other sites

×

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.