Jump to content

Modify my function to resize then crop a thumbnail.


madk

Recommended Posts

Hello all,

 

I'm trying to modify my thumbnail function to crop my thumbnails to 150 x 150.  I don't want to resize the image I just want to crop off the extra area to create the square.  I can't wrap my brain around all of the variables of imagecopyresampled to accomplish this.  Could someone please guide me.  I appreciate it.

 

function thumbJPG($jpgFile)
{
// Get dimensions of original
list($width_orig, $height_orig) = getimagesize($jpgFile);

if($width_orig > $height_orig)
{
	$height = 150;
  		$width = (int) (($height / $height_orig) * $height_orig);
	$image_p = imagecreatetruecolor($width, $height);
	$image = imagecreatefromjpeg($jpgFile);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
}
else
{
	$width = 150;
  		$height = (int) (($width / $width_orig) * $height_orig);
	$image_p = imagecreatetruecolor($width, $height);
	$image = imagecreatefromjpeg($jpgFile);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
}

   	// Output
   	imagejpeg($image_p, $jpgFile, 100);
}

SOLVED

 

function thumbJPG($jpgFile)
{
// Get dimensions of original
list($width_orig, $height_orig) = getimagesize($jpgFile);
$image_p = imagecreatetruecolor(150, 150);
$image = imagecreatefromjpeg($jpgFile);

if($width_orig > $height_orig)
{
	$off_w = ($width_orig-$height_orig)/2;
	$off_h = 0;
    	$width_orig = $height_orig;
}
elseif($height_orig > $width_orig)
{
	$off_w = 0;
    	$off_h = ($height_orig-$width_orig)/2;
    	$height_orig = $width_orig;
}
else
{
	$off_w = 0;
	$off_h = 0;
}

imagecopyresampled($image_p, $image, 0, 0, $off_w, $off_h, 150, 150, $width_orig, $height_orig);

   	// Output
   	imagejpeg($image_p, $jpgFile, 100);
}

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.