Jump to content

PHP image cropping


tiki

Recommended Posts

Ok so I have this class that uploads and images and resizes. It all works perfectly, but now I need to "crop" the image if the $maxHeight is set but I cant seem to get it working correctly.

 

So if $maxHeight is null it just resizes the image depending on the $maxWidth.

 

Secondly if $maxHeight is set, it will resize the image to the $maxWidth but also crop the height at $maxHeight.

 

Cant figure out the correct coordinates, any help would be awesome.

 

	/**
 * Uploads to tmp and resizes an image
 * @param array $file
 * @param int $maxWidth
 * @param int $maxHeight
 * @param int $quality
 * @return void
 */
function resize($file, $maxWidth, $maxHeight = NULL, $quality = 75) {
	$image = $this->upload($file);
	if ($image) {

		// Capture the original size of the uploaded image
		list($width, $height) = getimagesize($image);

		// Figure out the new image dimensions
		if ($width <= $maxWidth) {
			return $image;
		} else {
			$newWidth = $maxWidth;

			if (isset($maxHeight)) {
				$newHeight = $maxHeight;
			} else {
				$newHeight = ($height / $width) * $maxWidth;
			}
		}

		$path = $this->temp . $maxWidth .'_'. $file['name'];

		// Create an image to work with
		switch ($file['type']) {
			case 'image/gif':  $src = imagecreatefromgif($image); break;
			case 'image/png':  $src = imagecreatefrompng($image); break;
			case 'image/jpg':
			case 'image/jpeg': $src = imagecreatefromjpeg($image);  break;
			default:  
				trigger_error('Unsupported filetype!', E_USER_WARNING);  
			break;
		}

		$tmp = imagecreatetruecolor($newWidth, $newHeight);

		// If gif,png allow transparencies
		if ($file['type'] == 'image/gif' || $file['type'] == 'image/png') {
			imagealphablending($tmp, false);
			imagesavealpha($tmp, true);
			$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
			imagefilledrectangle($tmp, 0, 0, $newWidth, $newHeight, $transparent);
		}

		// Lets take our source and apply it to the temporary file and resize
		imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

		// Now write the resized image to the server
		switch ($file['type']) {
			case 'image/gif':  imagegif($tmp, $path); break;
			case 'image/png':  imagepng($tmp, $path); break;
			case 'image/jpg':
			case 'image/jpeg': imagejpeg($tmp, $path, $quality); break;
			default:  
				trigger_error('Image resize has failed!', E_USER_WARNING);  
			break;
		}

		// Clear memory
		imagedestroy($src);
		imagedestroy($tmp);
		unlink($image);

		return $path;
	}

	return false;
}

Link to comment
https://forums.phpfreaks.com/topic/150068-php-image-cropping/
Share on other sites

Wow, that's going to take a lot of brainpower!

 

I would pass three variables into the script: maxWidth, maxHeight, crop.

If crop is true, then the image needs to be cropped.

 

You'll want to have a function dedicated to cropping.

 

Preferably you'd want to crop the image, keeping the center. You set that stuff with imagecopyresampled. You tell it what parts of the source image you want to keep.

Link to comment
https://forums.phpfreaks.com/topic/150068-php-image-cropping/#findComment-788182
Share on other sites

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.