LNMP 下PHP自动裁剪图片适应显示

  • 内容
  • 相关

功能:
环境 Linux + Nginx + Php 在不修改现有的源代码与图片路径位置的情况下,达到通过传参宽高来动态输出对应大小的图片并缓存。图片的处理要求是通过传参宽高的算出可用最大宽高,得到原图宽高。尽可能不毁图,不填白,不失真。

 实例:
100*100:https://www.caojiefeng.com/content/uploadfile/201711/ba911509726344.png?param=100y100

200*200:https://www.caojiefeng.com/content/uploadfile/201711/ba911509726344.png?param=200y200

400*400:https://www.caojiefeng.com/content/uploadfile/201711/ba911509726344.png?param=400y400

600*400:https://www.caojiefeng.com/content/uploadfile/201711/ba911509726344.png?param=600y400
代码:

//nginx伪静态规则如下:
//只要是带参数的图片均处理,不带参数显示原图
if ($args !~ "^$"){
	set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
	rewrite ^/([\s\S]*?)\.png(.*)$ /image.php?type=png&url=$1&m=$args last;
}
if ($args !~ "^$"){
	set $rule_1 1$rule_1;
}
if ($rule_1 = "1"){
	rewrite ^/([\s\S]*?)\.jpg(.*)$ /image.php?type=jpg&url=$1&m=$args last;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
        expires 30d;
        error_page 404 =200 /default.png;
}

//image.php代码

<?php

header("Cache-Control: max-age=2592000");
header("Pragma: cache");
$offset = 30*60*60*24; // cache 1 month
$ExpStr = "Expires: ".gmdate("D, d M Y H:i:s", time() + $offset)." GMT";
header($ExpStr);
define('EMLOG_ROOT', dirname(__FILE__));
$imgurl = $_GET['url'].'.'.$_GET['type'];
$imgurl1 =  EMLOG_ROOT.$imgurl;
$param = explode("y",$_GET['param']);
$width = $param[0];
$height = $param[1];
if(file_exists($imgurl1) || $width =='' || $height ==''){
	exit('PARAM ERROR');
}
imagecropper($imgurl,$width,$height);
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 'image/gif':
			$source_image = imagecreatefromgif($source_path);
		break;
			case 'image/jpeg':
			$source_image = imagecreatefromjpeg($source_path);
		break;
			case 'image/png':
			$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);
	//保存图片到本地(两者选一)
	//$randNumber = mt_rand(00000, 99999). mt_rand(000, 999);
	//$fileName = substr(md5($randNumber), 8, 16) .".png";
	//imagepng($target_image,'./'.$fileName);
	//imagedestroy($target_image);

	//直接在浏览器输出图片(两者选一)
	header('Content-Type: image/jpeg');
	imagepng($target_image);
	imagedestroy($target_image);
	imagejpeg($target_image);
	imagedestroy($source_image);
	imagedestroy($target_image);
	imagedestroy($cropped_image);
}

?>


本文标签:

收录状态:百度-[百度已收录]360-[360未收录]搜狗-[搜狗已收录]

版权声明:若无特殊注明,本文皆为《曹杰峰》原创,转载请保留文章出处。

本文链接:LNMP 下PHP自动裁剪图片适应显示 - https://www.caojiefeng.com/jyjc/50.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

00:00 / 00:00
顺序播放