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 相关文章推荐
数据库相关问题
Oct 09 PHP
使用sockets:从新闻组中获取文章(三)
Oct 09 PHP
微博短链接算法php版本实现代码
Sep 15 PHP
php之CodeIgniter学习笔记
Jun 17 PHP
php获取301跳转URL简单实例
Dec 16 PHP
PHP设置进度条的方法
Jul 08 PHP
详解Window7 下开发php扩展
Dec 31 PHP
php session 写入数据库
Feb 13 PHP
PHP抓取及分析网页的方法详解
Apr 26 PHP
浅谈PHP发送HTTP请求的几种方式
Jul 25 PHP
PHP二维索引数组的遍历实例分析【2种方式】
Jun 24 PHP
PHP框架实现WebSocket在线聊天通讯系统
Nov 21 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
php include的妙用,实现路径加密
2008/07/29 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
JavaScript 浏览器对象模型BOM使用介绍
2015/04/13 Javascript
jQuery获取URL请求参数的方法
2015/07/18 Javascript
JavaScript 链式结构序列化详解
2016/09/30 Javascript
js图片切换具体实现代码
2016/10/13 Javascript
js实现上传图片预览方法
2016/10/25 Javascript
Javascript Function.prototype.bind详细分析
2016/12/29 Javascript
BootstrapTable refresh 方法使用实例简单介绍
2017/02/20 Javascript
JS+CSS实现滚动数字时钟效果
2017/12/25 Javascript
webpack4简单入门实例
2018/09/06 Javascript
vue.js实现备忘录demo
2019/06/26 Javascript
layui表格 列自动适应大小失效的解决方法
2019/09/06 Javascript
Vuex modules模式下mapState/mapMutations的操作实例
2019/10/17 Javascript
解决vue更新路由router-view复用组件内容不刷新的问题
2019/11/04 Javascript
antd-mobile ListView长列表的数据更新遇到的坑
2020/04/08 Javascript
[31:33]2014 DOTA2国际邀请赛中国区预选赛 TongFu VS DT 第一场
2014/05/23 DOTA
Python批量生成特定尺寸图片及图画任意文字的实例
2019/01/30 Python
详解爬虫被封的问题
2019/04/23 Python
Python @property使用方法解析
2019/09/17 Python
汉森批发:Hansen Wholesale
2018/05/24 全球购物
俄罗斯厨房产品购物网站:COOK HOUSE
2021/03/15 全球购物
linux下进程间通信的方式
2014/12/23 面试题
家长对孩子评语
2014/01/30 职场文书
法律系毕业生自荐信范文
2014/03/27 职场文书
家长通知书教师评语
2014/04/17 职场文书
政治学求职信
2014/06/03 职场文书
入党积极分子个人总结
2015/03/02 职场文书
小学生家长意见
2015/06/03 职场文书
运动会通讯稿300字
2015/07/20 职场文书
公司借款担保书
2015/09/22 职场文书
小学一年级数学教学反思
2016/02/16 职场文书
《清澈的湖水》教学反思
2016/02/17 职场文书
2016年优秀党员教师先进事迹材料
2016/02/29 职场文书
如何书写公司员工保密协议?
2019/06/27 职场文书
记一次Mysql不走日期字段索引的原因小结
2021/10/24 MySQL