Jump to content

Image Resizing


isedeasy

Recommended Posts

I have a slight problem, the code works but it does not do exactally as I want

 

//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($file_tmp);

//calculate the image ratio
$imgratio = $width/$height;

if ( $imgratio > 1 ) {

$newwidth = THUMB_WIDTH;
$newheight = THUMB_WIDTH/$imgratio;

} else {

$newheight = THUMB_WIDTH;
$newwidth = THUMB_WIDTH*$imgratio;

}

//function for resize image.
$resized_img = imagecreatetruecolor($newwidth,$newheight);

//the resizing is going on here!
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//finally, save the image
imagejpeg ($resized_img, 'd:/www'.THUMB_DIR.$image_name, 100);

//move_uploaded_file ($new_img, THUMB_DIR.$image_name);
ImageDestroy ($new_img);
ImageDestroy ($resized_img);

 

The above code scales the image whilst keeping proportions, that works fine. The problem is that I want the image to always be 210px by 210px. Depending on the image either the height or width may be less than 210px. I need to keep the proportions the same but some how add white-space to compensate the shorter edge.

 

Is this possible?

Link to comment
Share on other sites

I do not know of a way that this can be done the way you want, but what I would do is to center the resultant image in a 210x210 px div with a white background

 

That's how I am doing it at the moment, the problem with this method is when you get an image that's not 210px high. I can't vertically centre it in a div which makes it look amatuar.

Link to comment
Share on other sites

Thanks thats what I am looking for.

 

I have got the image positioning working but I can't seem to set the background to white. What ever I try I get a black bg.

 

//list the width and height and keep the height ratio.
list($width, $height) = getimagesize($file_tmp);

//calculate the image ratio
$imgratio = $width/$height;

if ( $imgratio > 1 ) {

$newwidth = THUMB_WIDTH;
$newheight = THUMB_WIDTH/$imgratio;
$y = (THUMB_WIDTH - $newheight)/2;
$x = 0;

} else {

$newheight = THUMB_WIDTH;
$newwidth = THUMB_WIDTH*$imgratio;
$x = (THUMB_WIDTH - $newwidth)/2;
$y = 0;

}

//function for resize image.
$resized_img = imagecreatetruecolor(THUMB_WIDTH,THUMB_WIDTH);
imagecolorallocate($resized_img, 255, 225, 225);

//the resizing is going on here!
imagecopyresampled ($resized_img, $new_img, $x, $y, 0, 0, $newwidth, $newheight, $width, $height);

//finally, save the image
imagejpeg ($resized_img, 'd:/www'.THUMB_DIR.$image_name, 100);

//move_uploaded_file ($new_img, THUMB_DIR.$image_name);
ImageDestroy ($new_img);
ImageDestroy ($resized_img);

Link to comment
Share on other sites

Ok this one is solved, thanks for the help guys

 

    $resized_img = imagecreate(THUMB_WIDTH,THUMB_WIDTH);
    
    imagecolorallocate($resized_img, 255, 255, 255);
    
    imagecopyresampled ($resized_img, $new_img, $x, $y, 0, 0, $newwidth, $newheight, $width, $height);
    
    imagejpeg ($resized_img, 'd:/www'.THUMB_DIR.$image_name, 100);
    
    ImageDestroy ($new_img);
    ImageDestroy ($resized_img);

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.