PHP图片自动裁切应付不同尺寸的显示


Posted in PHP onOctober 16, 2014

如果做过那种门户站的朋友,肯定知道,一张图片可能会在不同的地方显示,大小不同,比例也不同,
如果只用一张图的话,那么肯定会变形,而且在显示小图的地方,链接 大图,又太浪费了.....用缩略图来处理,也不完美,因为每个地方出现的比例 大小可能都不一样 ,举个例子!
PHP图片自动裁切应付不同尺寸的显示

请看上图。

在这个地方,其实调去出来的是一个列表,但是 图片的大小是不一样的,有多大宽有的窄,,当遇到这样的情况的时候 你们怎么办呢,如果直接用原来的地址,肯定是会变形的,如果搞缩略图也不靠谱,这个调去是自动调去的,你根本不知道哪个图片需要多大的宽高,
------------------------------------------------------------------------------------------------------------------
下面进入正题:

我一直用一种方法,就是PHP 自动裁切...相比你们看到过类似那种图片地址吧 /aaaa/abc_200_100.jpg 或者/aaaa/abc_200*100.jpg
我的方式就是在需要图片地方把这个图片地址转化为 类似上面的那种地址, 然后通过apache 的rewrite 定向到一个处理程序.根据宽高生成一个图片然后保存起来,

这样做的好处有几个地方:

第一,非常灵活,在有图片地方,你需要多宽多高,都可以随意 控制,不会变形,而且程序永远会让图片内容显示的最多
第二个,当图片生成过一次的时候,apache下次就不会再重定向到程序了, 因为在规则前面 有 !d !f 这个判断,意思就是当前文件不存在的时候才会定向走,下次图片存在了,就不会再出来了 直接就是真是的图片了

不好的地方,就是生成的图片可能会比较多,占用的空间也比较大,但是如果是自己服务器 那就无所谓了,可以归类整理下

OK 奉上代码,我们就以discuz为例

function crop_img($img, $width = 200, $height = 200) {

$img_info = parse_url($img);

/* 外部链接直接返回图片地址 */

if (!empty($img_info['host']) && $img_info['host'] != $_SERVER['HTTP_HOST']) {

return $img;

} else {

$pos = strrpos($img, '.');

$img = substr($img, 0, $pos) . '_' . $width . '_' . $height . substr($img, $pos);

return $img;

}

}
function img($img,$width,$height){

$img_info = parse_url($img);

/* 外部链接直接返回图片地址 */

if (!empty($img_info['host']) && $img_info['host'] != $_SERVER['HTTP_HOST']) {

return $img;

} else {

$pos = strrpos($img, '.');

$img = substr($img, 0, $pos) . '_' . $width . '_' . $height . substr($img, $pos);

echo '<img src="'.$img.'" width="'.$width.'" height="'.$height.'" />';

return ;

} 

}

函数的用法 crop_img('原图地址','宽度','高度'); 这个函数返回处理过的图片地址,img 函数直接返回图片标签字符串,比如在discuz模板里面调用这个函数 {eval img($pic,200,100)}
这样返回的地址就是/data/attachment/forum/aaaaaa_200_100.jpg 目前来说 这个图片是不存在 那么看第二步

第二步 需要添加apache的rewrite规则

<IfModule mod_rewrite.c> 

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteRule ^data/attachment/(.*)$ images.php?url=$1 [L]

</IfModule>

上面的意思,就是 data/attachement/这个地址开头不存在的文件都定向到image.php来处理,并且把url当参数传过去

第三部就是image.php 这个里面的代码里

<?php
$url = $_GET['url'];

$src = './data/attachment/' . preg_replace('/_(\d+)_(\d+)/', '', $url);

$filename = './data/attachment/' . $url;

if (file_exists($filename)) {

ob_start();

header('Content-type:image/jpeg');

readfile($filename);

ob_flush();

flush();

} else {

if(!preg_match('/_(\d+)_(\d+)/', $url, $wh)){

defulat();

exit();

}

$width = $wh[1];

$height = $wh[2];

thumb(realpath($src), $width, $height, $filename, 'crop', '85');

}

function thumb($src, $width, $height, $filename, $mode = 'scale', $quality = '100') {

try {

$imageValue = getimagesize($src);

$sourceWidth = $imageValue[0]; //原图宽

$sourceHeight = $imageValue[1]; //原图高

$thumbWidth = $width; //缩略图宽

$thumbHeight = $height; //缩略图高

$_x = 0;

$_y = 0;

$w = $sourceWidth;

$h = $sourceHeight;

if ($mode == 'scale') {

if ($sourceWidth <= $thumbWidth && $sourceHeight <= $thumbHeight) {

$_x = floor(($thumbWidth - $sourceWidth) / 2);

$_y = floor(($thumbHeight - $sourceHeight) / 2);

$thumbWidth = $sourceWidth;

$thumbHeight = $sourceHeight;

} else {

if ($thumbHeight * $sourceWidth > $thumbWidth * $sourceHeight) {

$thumbHeight = floor($sourceHeight * $width / $sourceWidth);

$_y = floor(($height - $thumbHeight) / 2);

} else {

$thumbWidth = floor($sourceWidth * $height / $sourceHeight);

$_x = floor(($width - $thumbWidth) / 2);

}

}

} else if ($mode == 'crop') {

if ($sourceHeight < $thumbHeight) { //如果原图尺寸小于当前尺寸 

$thumbWidth = floor($thumbWidth * $sourceHeight / $thumbHeight);

$thumbHeight = $sourceHeight;

}

if ($sourceWidth < $thumbWidth) {

$thumbHeight = floor($thumbHeight * $sourceWidth / $thumbWidth);

$thumbWidth = $sourceWidth;

}

$s1 = $sourceWidth / $sourceHeight; //原图比例

$s2 = $width / $height; //新图比例

if ($s1 == $s2) {

} else if ($s1 > $s2) { //全高度 

$y = 0;

$ax = floor($sourceWidth * ($thumbHeight / $sourceHeight));

$x = ($ax - $thumbWidth) / 2;

$w = $thumbWidth / ($thumbHeight / $sourceHeight);

} else { //全宽度 

$x = 0;

$ay = floor($sourceHeight * ($thumbWidth / $sourceWidth)); //模拟原图比例高度

$y = ($ay - $thumbHeight) / 2;

$h = $thumbHeight / ($thumbWidth / $sourceWidth);

}

}

switch ($imageValue[2]) {

case 2: $source = imagecreatefromjpeg($src);

break;

case 1: $source = imagecreatefromgif($src);

break;

case 3: $source = imagecreatefrompng($src);

break;

case 6: $source = imagecreatefromwbmp($src);

break;

default: defulat();

return;

}

header("Content-type: image/jpeg");

$thumb = imagecreatetruecolor($width, $height);

imagefill($thumb, 0, 0, imagecolorallocate($thumb, 255, 255, 255));

imagecopyresampled($thumb, $source, 0, 0, $x, $y, $width, $height, $w, $h);

imagejpeg($thumb, null, $quality);

// if ($_SERVER['HTTP_REFERER'] || false !== stripos($_SERVER['HTTP_REFERER'], 'http://' . $_SERVER['SERVER_NAME'])) {

imagejpeg($thumb, $filename, $quality);

// }

imagedestroy($thumb);

imagedestroy($source);

} catch (Exception $ex) {

defulat();

}

}

function defulat() {

$default_img = realpath('media/images/nopic.jpg');

ob_start();

header('Content-type:image/jpeg');

readfile($default_img);

ob_flush();

flush();

}

thumb 函数 可以控制 裁切方式,scale 为等比缩放,不裁切,不够的地方 用白色填充,crop 为裁切,如果要求的宽高比 大于原图宽高比,那么就保持最大显示宽度,居中裁切上下多余部分,如果要求宽高比小于原图宽高比,那么就保持最大高度,居中裁切左右多余部分,总而言之,在保持不变形的前提下 ,把图片缩小,而且最大保留图片的内容.哈哈 这个代码有多叼,试试知道了,,,当然你需要支持rewrite功能和GD2 支持
PHP 相关文章推荐
同时提取多条新闻中的文本一例
Oct 09 PHP
第八节--访问方式
Nov 16 PHP
某大型网络公司应聘时的笔试题目附答案
Mar 27 PHP
PHP 图片水印类代码
Aug 27 PHP
PHP实现的MongoDB数据库操作类分享
May 12 PHP
php设计模式之简单工厂模式详解
Sep 04 PHP
PHP数组排序之sort、asort与ksort用法实例
Sep 08 PHP
Smarty中调用FCKeditor的方法
Oct 27 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
Nov 06 PHP
php使用cookie保存登录用户名的方法
Jan 26 PHP
使用php自动备份数据库表的实现方法
Jul 28 PHP
thinkphp 抓取网站的内容并且保存到本地的实例详解
Aug 25 PHP
PHP 抽象方法与抽象类abstract关键字介绍及应用
Oct 16 #PHP
php开启与关闭错误提示适用于没有修改php.ini的权限
Oct 16 #PHP
php实现扫描二维码根据浏览器类型访问不同下载地址
Oct 15 #PHP
ThinkPHP基于PHPExcel导入Excel文件的方法
Oct 15 #PHP
ThinkPHP分页实例
Oct 15 #PHP
PHP中使用Session配合Javascript实现文件上传进度条功能
Oct 15 #PHP
jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码
Oct 15 #PHP
You might like
PHP获取网站域名和地址的代码
2008/08/17 PHP
php 如何获取数组第一个值
2013/08/06 PHP
php根据用户语言跳转相应网页
2015/11/04 PHP
php mysqli查询语句返回值类型实例分析
2016/06/29 PHP
JQuery的Alert消息框插件使用介绍
2010/10/09 Javascript
最短的IE判断代码
2011/03/13 Javascript
用jquery仿做发微博功能示例
2014/04/18 Javascript
JavaScript头像上传插件源码分享
2016/03/29 Javascript
详解JavaScript节流函数中的Throttle
2016/07/16 Javascript
jquery获取input type=text中的值的各种方式(总结)
2016/12/02 Javascript
基于JQuery实现的跑马灯效果(文字无缝向上翻动)
2016/12/02 Javascript
原生JS下拉加载插件分享
2016/12/26 Javascript
AngularJS使用ng-class动态增减class样式的方法示例
2017/05/18 Javascript
vue axios同步请求解决方案
2017/09/29 Javascript
JS扩展String.prototype.format字符串拼接的功能
2018/03/09 Javascript
React 无状态组件(Stateless Component) 与高阶组件
2018/08/14 Javascript
在Vue 中使用Typescript的示例代码
2018/09/10 Javascript
微信小程序时间戳转日期的详解
2019/04/30 Javascript
微信小程序接入vant Weapp组件的详细步骤
2020/10/28 Javascript
[51:00]Secret vs VGJ.S 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
Ubuntu下创建虚拟独立的Python环境全过程
2017/02/10 Python
Python数据分析之双色球中蓝红球分析统计示例
2018/02/03 Python
Python中如何将一个类方法变为多个方法
2019/12/30 Python
python3.x中安装web.py步骤方法
2020/06/23 Python
关于Kotlin中SAM转换的那些事
2020/09/15 Python
Python加载数据的5种不同方式(收藏)
2020/11/13 Python
MoviePy简介及Python视频剪辑自动化
2020/12/18 Python
全球领先的鞋类零售商:The Walking Company
2016/07/21 全球购物
校三好学生主要事迹
2014/01/11 职场文书
医院保洁员岗位职责
2015/02/13 职场文书
酒店保洁员岗位职责
2015/02/26 职场文书
健康证明
2015/06/19 职场文书
2016年党支部公开承诺书
2016/03/25 职场文书
解决Tkinter中button按钮未按却主动执行command函数的问题
2021/05/23 Python
Python3 类型标注支持操作
2021/06/02 Python
Vue的生命周期一起来看看
2022/02/24 Vue.js