php怎么对图片不同尺寸显示

php怎么对图片不同尺寸显示

内容导读

收集整理的这篇技术教程文章主要介绍了php怎么对图片不同尺寸显示,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3121字,纯文字阅读大概需要5分钟

内容图文

一张图片可能会在不同的地方显示,大小不同,比例也不同,因此本例介绍的这个图片自动裁切还是比较有用的,有需求的朋友可以看看

如果做过那种门户站的朋友,肯定知道,一张图片可能会在不同的地方显示、大小不同、比例也不同,如果只用一张图的话,那么肯定会变形,而且在显示小图的地方,链接大图,又太浪费了.....用缩略图来处理,也不完美,因为每个地方出现的比例大小可能都不一样 。 (推荐学习:PHP视频教程)

PHP自动裁切相比你们看到过类似那种图片地址/aaaa/abc_200_100.jpg 或者/aaaa/abc_200*100.jpg
我的方式就是在需要图片地方把这个图片地址转化为类似上面的那种地址, 然后通过apache 的rewrite 定向到一个处理程序.根据宽高生成一个图片然后保存起来,在不动原图的任何信息和位置的情况下对图片做处理。

源码如下:

伪静态规则:

RewriteRule ^(.*.(png|jpg))/(.*)$ image.php?url=$1&param=$3 [L]

image.php放到根目录:

<?phpdefine('EMLOG_ROOT', dirname(__FILE__));$imgurl = EMLOG_ROOT.$_GET['url'];$param = $_GET['param'];if(file_exists($imgurl) || $param ==''){	header("HTTP/1.1 404 Not Found");
	header("Status: 404 Not Found");
	exit;
}preg_match_all('#param=(.*)y(.*)#',$param,$info);$width = $info[1][0];$height = $info[2][0];imagecropper($_GET['url'],$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);}?>

以上就是php怎么对图片不同尺寸显示的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的php怎么对图片不同尺寸显示全部内容,希望文章能够帮你解决php怎么对图片不同尺寸显示所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com