php实现从上传文件创建缩略图的方法


Posted in PHP onApril 02, 2015

本文实例讲述了php实现从上传文件创建缩略图的方法。分享给大家供大家参考。具体实现方法如下:

<?php
if ($_REQUEST['action']=="add"){
$userfile = $HTTP_POST_FILES['photo']['tmp_name'];
$userfile_name = $HTTP_POST_FILES['photo']['name'];
$userfile_size = $HTTP_POST_FILES['photo']['size'];
$userfile_type = $HTTP_POST_FILES['photo']['type'];
///////////////////////// 
//GET-DECLARE DIMENSIONS //
$dimension = getimagesize($userfile);
$large_width = $dimension[0]; // GET PHOTO WIDTH
$large_height = $dimension[1]; //GET PHOTO HEIGHT
$small_width = 120; // DECLARE THUMB WIDTH
$small_height = 90; // DECLARE THUMB HEIGHT
/////////////////////////
//CHECK SIZE //
if ($userfile_size>102400){
   $error=1;
   $msg = "The photo is over 100kb. Please try again.";
}
////////////////////////////////
// CHECK TYPE (IE AND OTHERS) //
if ($userfile_type="image/pjpeg"){
  if ($userfile_type!="image/jpeg"){
    $error=1;
    $msg = "The photo must be JPG";
  }
}
//////////////////////////////
//CHECK WIDTH/HEIGHT //
if ($large_width!=600 or$large_height!=400){
$error=1;
$msg = "The photo must be 600x400 pixels";
}
///////////////////////////////////////////
//CREATE THUMB / UPLOAD THUMB AND PHOTO ///
if ($error<>1){
  $image = $userfile_name; //if you want to insert it to the database
  $pic = imagecreatefromjpeg($userfile);
  $small = imagecreatetruecolor($small_width,$small_height);
  imagecopyresampled($small,$pic,0,0,0,0, $small_width, $small_height, $large_width, $large_height);
  if (imagejpeg($small,"path/to/folder/to/upload/thumb".$userfile_name, 100)){  
    $large = imagecreatetruecolor($large_width,$large_height);
  imagecopyresampled($large,$pic,0,0,0,0, $large_width, $large_height, $large_width, $large_height);
    if (imagejpeg($large,"path/to/folder/to/upload/photo".$userfile_name, 100))
   {}
      else {$msg="A problem has occured. Please try again."; $error=1;}
  }
  else {
   $msg="A problem has occured. Please try again."; $error=1;
  }
}
//////////////////////////////////////////////
/// If everything went right a photo (600x400) and
/// a thumb(120x90) were uploaded to the given folders
}
?>
<html><head><title>create thumb</title></head>
<body>
<form name="form1" enctype="multipart/form-data" action="thisfile.php?action=add" method="post">
Select Photo: <input type="file" name="photo">
<input type="submit" name="submit" value="CREATE THUMB AND UPLOAD">
</form>
</body
</html>

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
1.PHP简介
Oct 09 PHP
我的论坛源代码(四)
Oct 09 PHP
php你的验证码安全码?
Jan 02 PHP
精通php的十大要点(上)
Feb 04 PHP
php小技巧 把数组的键和值交换形成了新的数组,查找值取得键
Jun 02 PHP
PHP笔记之:基于面向对象设计的详解
May 14 PHP
Zend studio文件注释模板设置方法
Sep 29 PHP
PHP5.3安装Zend Guard Loader图文教程
Sep 29 PHP
ThinkPHP模版引擎之变量输出详解
Dec 05 PHP
Symfony2学习笔记之控制器用法详解
Mar 17 PHP
Laravel5权限管理方法详解
Jul 26 PHP
Thinkphp框架使用list_to_tree 实现无限级分类列出所有节点示例
Apr 04 PHP
php调用KyotoTycoon简单实例
Apr 02 #PHP
PHP中数据类型转换的三种方式
Apr 02 #PHP
php在apache环境下实现gzip配置方法
Apr 02 #PHP
PHP中使用socket方式GET、POST数据实例
Apr 02 #PHP
php获取百度收录、百度热词及百度快照的方法
Apr 02 #PHP
php中实现获取随机数组列表的自定义函数
Apr 02 #PHP
PHP版微信公众平台红包API
Apr 02 #PHP
You might like
在smarty中调用php内置函数的方法
2013/02/07 PHP
php实现上传图片生成缩略图示例
2014/04/13 PHP
PHP采用get获取url汉字出现乱码的解决方法
2014/11/13 PHP
在WordPress的后台中添加顶级菜单和子菜单的函数详解
2016/01/11 PHP
thinkphp5.1 文件引入路径问题及注意事项
2018/06/13 PHP
PHP mongodb操作类定义与用法示例【适合mongodb2.x和mongodb3.x】
2018/06/16 PHP
PHP实现图片防盗链破解操作示例【解决图片防盗链问题/反向代理】
2020/05/29 PHP
JS事件Event元素(兼容IE,Firefox,Chorme)
2012/11/01 Javascript
如何在一个页面显示多个百度地图
2013/04/07 Javascript
node.js中的fs.readFileSync方法使用说明
2014/12/15 Javascript
jQuery实现购物车数字加减效果
2015/03/14 Javascript
jQuery获取table行数并输出单元格内容的实现方法
2016/06/30 Javascript
使用bootstraptable插件实现表格记录的查询、分页、排序操作
2017/08/06 Javascript
详解Vue中数组和对象更改后视图不刷新的问题
2018/09/21 Javascript
vue el-table实现自定义表头
2019/12/11 Javascript
解决angular 使用原生拖拽页面卡顿及表单控件输入延迟问题
2020/04/21 Javascript
Vue登录拦截 登录后继续跳转指定页面的操作
2020/08/04 Javascript
python妙用之编码的转换详解
2017/04/21 Python
Sanic框架流式传输操作示例
2018/07/18 Python
Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】
2018/12/26 Python
python函数不定长参数使用方法解析
2019/12/14 Python
python多项式拟合之np.polyfit 和 np.polyld详解
2020/02/18 Python
Python如何定义接口和抽象类
2020/07/28 Python
python基于pygame实现飞机大作战小游戏
2020/11/19 Python
一个入门级python爬虫教程详解
2021/01/27 Python
详解android与HTML混合开发总结
2018/06/06 HTML / CSS
在Ajax应用中信息是如何在浏览器和服务器之间传递的
2016/05/31 面试题
小学评语大全
2014/04/22 职场文书
财政专业大学生职业生涯规划书
2014/09/17 职场文书
党员违纪检讨书怎么写
2014/11/01 职场文书
评先进个人材料
2014/12/29 职场文书
部队个人年终总结
2015/03/02 职场文书
社团个人总结范文
2015/03/05 职场文书
大学生受助感言
2015/08/01 职场文书
解决Mysql中的innoDB幻读问题
2022/04/29 MySQL
Flink 侧流输出源码示例解析
2022/09/23 Servers