设为首页收藏本站
开启辅助访问
切换到宽版

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索
查看: 5759|回复: 0
打印 上一主题 下一主题

使用PHP和jQuery实现截图自定义头像

[复制链接]

我玩的应用:

跳转到指定楼层
楼主
发表于 2012-10-14 13:22:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在一些SNS网站上常会有一些自定义头像的功能,这里介绍一个用PHP和jQuery实现截图自定义头像的方法,需要的环境和前提如下:1,PHP 需要 GD 2.0.1 或更高版本
  2,jQuery库
  3,jQuery图片处理插件imgareaselect
  其中imgareaselect插件可以参考其官方网站:http://odyniec.net/projects/imgareaselect/ ,也可浏览本博客的相关内容:jQuery的截图插件imgAreaSelect,这里介绍了设置 imgAreaSelect 插件里所有参数及其描述。
  HTML头需要引入的文件:

  1.        <head>
  2.     <link rel="stylesheet" type="text/css" href="css/imgareaselect-default.css" />
  3.     <script type="text/javascript" src="scripts/jquery.min.js"></script>
  4.     <script type="text/javascript" src="scripts/jquery.imgareaselect.pack.js"></script>
  5.     <head>   
复制代码
其中CSS文件是jQuery的截图插件imgAreaSelect压缩包里自带的CSS样式,jquery.min.js 是jQuery库文件,jquery.imgareaselect.pack.js是imgareaselect插件的文件。
  
  Javascript 代码:

  1.        <script type="text/javascript">
  2.     function preview(img, selection) {
  3.     var scaleX = 100 / selection.width;//100指的是新图的宽
  4.     var scaleY = 100 / selection.height;//100指的是新图的高
  5.     $('#thumbnail img').css({
  6.     width: Math.round(scaleX * 800) + 'px', //800指的是原图的宽
  7.     height: Math.round(scaleY * 600) + 'px', //600指的是原图的高
  8.     marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px',
  9.     marginTop: '-' + Math.round(scaleY * selection.y1) + 'px'
  10.     });
  11.     $('#x1').val(selection.x1);
  12.     $('#y1').val(selection.y1);
  13.     $('#x2').val(selection.x2);
  14.     $('#y2').val(selection.y2);
  15.     $('#w').val(selection.width);
  16.     $('#h').val(selection.height);
  17.     }
  18.     $(document).ready(function () {
  19.     $('img#photo').imgAreaSelect({ x1: 0, y1: 0, x2: 50, y2: 50, fadeSpeed:10,aspectRatio: '1:1', handles:"corners",onSelectChange: preview});
  20.     $('#save_thumb').click(function() {
  21.     var x1 = $('#x1').val();
  22.     var y1 = $('#y1').val();
  23.     var x2 = $('#x2').val();
  24.     var y2 = $('#y2').val();
  25.     var w = $('#w').val();
  26.     var h = $('#h').val();
  27.     if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
  28.     alert("image is null");
  29.     return false;
  30.     }else{
  31.     return true;
  32.     }
  33.     });
  34.     });
  35.      </script>
复制代码
其中preview函数是当选择截图的时候,有一个预览的效果
  HTML代码:
  1.     <div style="float:left;" class="frame">
  2.     <img src="Water lilies.jpg" id="photo"/>
  3.     </div>
  4.     <div style="float:left; width: 100px; height: 100px;" id="thumbnail">
  5.        <div style="overflow: hidden; width: 100px; height: 100px;" id="preview">
  6.         <img src="Water lilies.jpg"/>
  7.        </div>
  8.     </div>
  9.     <div style="margin-top:10px;">
  10.     <form name="thumbnail" action="imgareaselect.php" method="post">
  11.     <input type="hidden" name="x1" value="" id="x1" />
  12.     <input type="hidden" name="y1" value="" id="y1" />
  13.     <input type="hidden" name="x2" value="" id="x2" />
  14.     <input type="hidden" name="y2" value="" id="y2" />
  15.     <input type="hidden" name="w" value="" id="w" />
  16.     <input type="hidden" name="h" value="" id="h" />
  17.     <input type="submit" name="upload_thumbnail" value="save thumb" id="save_thumb" />
  18.     </form>
  19.     </div>
复制代码
PHP代码:
  1.     $ifn = "Water lilies.jpg";
  2.     $ofn = "test_thumb.jpg";
  3.     $ext = strtoupper(end(explode('.',$ifn)));
  4.     if(is_file($ifn) && ($ext == "JPG" || $ext == "JPEG")){
  5.     $source = imagecreatefromjpeg($ifn);
  6.     }elseif(is_file($ifn) && $ext == "PNG"){
  7.     $source = imagecreatefromPNG($ifn);
  8.     }elseif(is_file($ifn) && $ext == "GIF"){
  9.     $source = imagecreatefromGIF($ifn);
  10.     }
  11.     $sourceWidth  = imagesx($source);
  12.     $sourceHeight = imagesy($source);
  13.     $thumbWidth = $_POST['w'];
  14.     $thumbHeight = $_POST['h'];
  15.     $thumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
  16.     $x1 = $_POST['x1'];
  17.     $y1 = $_POST['y1'];
  18.     $x2 = $_POST['x2'];
  19.     $y2 = $_POST['y2'];
  20.     imagecopyresampled($thumb,
  21.     $source,
  22.     0,
  23.     0,
  24.     $x1,
  25.     $y1,
  26.     $thumbWidth,
  27.     $thumbHeight,
  28.     $thumbWidth,
  29.     $thumbHeight
  30.     );
  31.     imagejpeg($thumb, $ofn);
复制代码
这里重点介绍一下imagecopyresampled函数相关资料,
说明:imagecopyresampled 重采样拷贝部分图像并调整大小
原型:bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )dst_image 和 src_image 分别是目标图像和源图像的标识符。
其它:imagecopyresampled(目标图像,源图像,存宽度,存高度,坐标X,坐标Y,源宽度,源高度) 如果成功则返回 TRUE,失败则返回 FALSE。 imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如果 dst_image 和 src_image 相同的话)区域,但如果区域交迭的话则结果不可预知。
注意事项: 因为调色板图像限制(255+1 种颜色)有个问题。重采样或过滤图像通常需要多于 255 种颜色,计算新的被重采样的像素及其颜色时采用了一种近似值。对调色板图像尝试分配一个新颜色时,如果失败我们选择了计算结果最接近(理论上)的颜色。这并不总是视觉上最接近的颜色。这可能会产生怪异的结果,例如空白(或者视觉上是空白)的图像。要跳过这个问题,请使用真彩色图像作为目标图像,例如用 imagecreatetruecolor() 创建的。
版本要求: 本函数需要 GD 2.0.1 或更高版本(推荐 2.0.28 及更高版本)。

from:http://www.ityizhan.com/php-jquery-screenshot/
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-9-22 07:20 , Processed in 0.084951 second(s), 25 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表