Jump to content

Image resize code alteration....


nevynev

Recommended Posts

Hi,

 

I have the code below for resizing uploaded images. Currently it resizes them to fit within two boundaries,  $forcedwidth  and  $forcedheight. However, I'd like to change the code so that the uploaded image's shortest edge matches the largest of these two inputs. So if I uploaded an image that was a ratio of 2:3 in dimensions I would want to be able to make sure the image is resized so that it fills the boundary of my two inputs. so if my inputs were 200 px x 220px, whatever size the image is, its shortest edge must be resized so that it is long enough to fill the 220 px requirement.

 

I hope that makes sense, the code is:

 

<?php
function resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp)
   {
   $g_imgcomp=100-$imgcomp;
   $g_srcfile=$sourcefile;
   $g_dstfile=$destfile;
   $g_fw=$forcedwidth;
   $g_fh=$forcedheight;

   if(file_exists($g_srcfile))
       {
       $g_is=getimagesize($g_srcfile);
       if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh))
           {
           $g_iw=$g_fw;
           $g_ih=($g_fw/$g_is[0])*$g_is[1];
           }
           else
           {
           $g_ih=$g_fh;
           $g_iw=($g_ih/$g_is[1])*$g_is[0];    
           }
       $img_src=imagecreatefromjpeg($g_srcfile);
       $img_dst=imagecreatetruecolor($g_iw,$g_ih);
       imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]);
       imagejpeg($img_dst, $g_dstfile, $g_imgcomp);
       imagedestroy($img_dst);
       return true;
       }
       else
       return false;
   } 

?>

 

Thanks so much for any help - such php is beyond me!!

NevyNev

Link to comment
https://forums.phpfreaks.com/topic/119717-image-resize-code-alteration/
Share on other sites

this is what I wrote/use... you can, of course, change it how you like.

 

<?php
function shrinkImage($maxWidth, $maxHeight, $sourceFileLocation, $newFileLocation)
{
// Floor input sizes
$maxWidth = floor($maxWidth);
$maxHeight = floor($maxHeight);

// Get the source image size
list($sourceWidth, $sourceHeight) = getimagesize($sourceFileLocation);

// Determine if we need to resize, and resize if we do
if($sourceWidth > $maxWidth || $sourceHeight > $maxWidth)
{
	// Determine whether to scale by x or y and set the scale value
	$xRatio = $sourceWidth / $maxWidth;
	$yRatio = $sourceHeight / $maxHeight;

	$scaleValue = max($xRatio, $yRatio);

	// Determine the new image dimensions
	$newWidth = $sourceWidth / $scaleValue;
	$newHeight = $sourceHeight / $scaleValue;

	// Floor values
	$sourceWidth = floor($sourceWidth);
	$sourceHeight = floor($sourceHeight);
	$newWidth = floor($newWidth);
	$newHeight = floor($newHeight);

	// Create the new image
	$newImage = imagecreatetruecolor($newWidth, $newHeight);

	// Get the source image
	$sourceImage = imagecreatefromjpeg($sourceFileLocation);

	// Resize
	$result = imagecopyresized($newImage, $sourceImage, 0, 0, 0, 0, 
		$newWidth, $newHeight, $sourceWidth, $sourceHeight);

	if(!$result)
	{
		throw new Exception('Error in master image creation.');
	}

	// header('Content-type: image/jpeg'); // Header for debugging purposes

	// Finally, save the image
	imagejpeg($newImage, $newFileLocation);
}
}
?>

 

Hopefully I understood your requirements correctly, if not you'll have to tell me... I have a couple more image functions.

you know... I used to use similar code...

I don't any more.

I use the one attached (yes, i can distribute it and Simon Jarvis rocks!) and then I just use an include at the top of the page to include it:

include $root.'includes/member_check.php';
include $root.'includes/img_resize.php';

 

Then to actually use it... I do the following...

   $image = new SimpleImage();
   $image->load($_FILES['image_upload']['tmp_name']);
   $image->resize(229,79);
   $image->save('images/banner_1.jpg');
   $my_banner='images/banner_1.jpg';

 

For you to do what you need, you need to get the size of the image you have uploaded using the getimagesize() function built into PHP:

http://uk3.php.net/getimagesize

 

Now all you need to do is replace the 229, 79 (above) with the values of the dimensions you want.

 

So you just test each dimension against your set variables:

// set the maximum height and width:
$max_width=500; // max width
$max_height=600; // max height

 

then something like this:

//work out the size:
list($width, $height, $type, $attr) = getimagesize($tmpName);

if ($width>=$height){
// width is bigger than or equal to height:
$ratio=$height/$max_height;
$height=$max_height;
$width=$width/$ratio;
}
else{
//height is bigger than or equal to width:
$ratio=$width/$max_width;
$width=$max_width;
$height=$height/$ratio;
}

then just replace the numbers in the resize command to $width and $height and hey presto - sorted!

that was a bit more tricky than i thought... My maths is still on the money! woohoo!

 

[attachment deleted by admin]

Archived

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

×
×
  • 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.