Loading Image

Almost there...

Loading Image

Loading...

Crop image from the center using GD Library

This is a PHP function which makes use of the GD Library, which is included in PHP, to crop an image from the center to the size that you want.
14 April 2017 - 10:00
Downloads: 468
Category: PHP, GD Library
Crop image from the center using GD Library
Overview

This is a PHP function which makes use of the GD Library, which is included in PHP, to crop an image from the center to the size that you want.

PHP Code:
<?php
//$filename = "../images/blog/default.jpg";'
//$newName = "new.jpg";
//$newNamePath = "../images/";

//#################################################################
// CROP IMAGE FROM THE CENTER
//#################################################################
function cropImage($filename, $newName, $newNamePath){
	$width  = imagesx($filename);
	$height = imagesy($filename);
	$centreX = round($width / 2);
	$centreY = round($height / 2);

	$cropWidth  = 100;
	$cropHeight = 100;
	$cropWidthHalf  = round($cropWidth / 2);
	$cropHeightHalf = round($cropHeight / 2);

	$x1 = max(0, $centreX - $cropWidthHalf);
	$y1 = max(0, $centreY - $cropHeightHalf);	

	$temp = imagecreatetruecolor($cropWidth, $cropHeight);		

	imagecopy($temp,$filename,0,0,$x1,$y1,$width,$height);

	imagejpeg($temp,$newNamePath.$newName,90);
	imagedestroy($temp);
}
?>