Home > Backend Development > PHP Tutorial > PHP image cropping and scaling functions_PHP tutorial

PHP image cropping and scaling functions_PHP tutorial

WBOY
Release: 2016-07-13 10:36:30
Original
691 people have browsed it

According to the customized height and width of the target image, crop and scale the source image according to the aspect ratio of the target image.


Incoming parameters:
$source_path string source image path
$target_width integer target image width
$target_height integer Target image height

Source image supports MIMETYPE: image/gif, image/jpeg, image/png.

function imagecropper($source_path, $target_width, $target_height)
{
	$source_info   = getimagesize($source_path);
	$source_width  = $source_info[0];
	$source_height = $source_info[1];
	$source_mime   = $source_info['mime'];
	$source_ratio  = $source_height / $source_width;
	$target_ratio  = $target_height / $target_width;

	// 源图过高
	if ($source_ratio > $target_ratio)
	{
		$cropped_width  = $source_width;
		$cropped_height = $source_width * $target_ratio;
		$source_x = 0;
		$source_y = ($source_height - $cropped_height) / 2;
	}
	// 源图过宽
	elseif ($source_ratio < $target_ratio)
	{
		$cropped_width  = $source_height / $target_ratio;
		$cropped_height = $source_height;
		$source_x = ($source_width - $cropped_width) / 2;
		$source_y = 0;
	}
	// 源图适中
	else
	{
		$cropped_width  = $source_width;
		$cropped_height = $source_height;
		$source_x = 0;
		$source_y = 0;
	}

	switch ($source_mime)
	{
		case &#39;image/gif&#39;:
			$source_image = imagecreatefromgif($source_path);
			break;

		case &#39;image/jpeg&#39;:
			$source_image = imagecreatefromjpeg($source_path);
			break;

		case &#39;image/png&#39;:
			$source_image = imagecreatefrompng($source_path);
			break;

		default:
			return false;
			break;
	}

	$target_image  = imagecreatetruecolor($target_width, $target_height);
	$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);

	// 裁剪
	imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height); 
	// 缩放
	imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);

	header(&#39;Content-Type: image/jpeg&#39;);
	imagejpeg($target_image);
	imagedestroy($source_image);
	imagedestroy($target_image);
	imagedestroy($cropped_image);
}
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/739133.htmlTechArticleCrop and scale the source image according to the height and width of the target image according to the customized height and width of the target image. Input parameters : $source_pathstring source image path $target_widthinteger target image width $target_heightinteger item...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template