Jump to content

Scale image to Fit certain width and height


louis567

Recommended Posts

You can start with something like this and customise it to suit your needs.

 

As for filling the background with white, you'd be better off setting the background area to white with css/html rather than insist the server do all of that extra image processing work.

 

Most of the stuff inside that script is pretty self explanatory and if you've got a single nuance of php ability you should be able to work it out.

Link to comment
Share on other sites

It is possible in PHP (anything is possible in PHP  ;) )

 

check this function

<?php
function ImageResize($filepath,$savefilepath,$imagetype,$w,$h)
{
$filename=$filepath;
$full_url=$savefilepath;
// Get new dimensions
list($width, $height) = getimagesize($filename);
// image width
$sw = $width;//$x[0];
// image height
$sh = $height;//$x[1];
    $percent=0;
$fixH=0;
$fixW=0;
if ($percent > 0) {
    // calculate resized height and width if percent is defined
    $percent = $percent * 0.01;
    $w = $sw * $percent;
    $h = $sh * $percent;
} else {
    if($sw>$sh){
	if($sw>$w){//--take ration
		$fixW=$w;
		$fixH=($w/$sw)*$sh;
		if($fixH>$h){
			$imageErr=0;
			return $imageErr;
		}
	}
	elseif($sw<=$w){
		if($sh>$h){
			$imageErr=1;
			return $imageErr;
		}
		else{
			$fixH=$sh;
			$fixW=$sw;
		}
	}
}
elseif($sw<=$sh){
	if($sh>$h){//--take ration
		$fixH=$h;
		$fixW=round(($h/$sh)*$sw,2);

		if($fixW>$w){
			$imageErr=2;
			return  $imageErr;
		}
	}
	elseif($sh<=$h){
		if($sw>$w){
			$imageErr=3;
			return  $imageErr;
		}
		else{
			$fixH=$sh;
			$fixW=$sw;
		}
	}
}
}




$new_width=$fixW;
$new_height=$fixH;

//echo $fixH."s".$fixW;
//exit;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);

if(strtolower($imagetype)=="jpg"||strtolower($imagetype)=="jpeg"){
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//imagejpeg($image_p, null, 100);

imagejpeg($image_p,$full_url,100);
}
elseif(strtolower($imagetype)=="gif"){	
$image = imagecreatefromgif($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagegif($image_p,$full_url,100);
}
elseif(strtolower($imagetype)=="png"){
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagepng($image_p,$full_url,100);
}

imagedestroy($image); 
$imageErr=-1;
return $imageErr;



}?>

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.