-
php实现从上传文件创建缩略图的方法
这篇文章主要介绍了php实现从上传文件创建缩略图的方法,涉及php操作上传文件及图片操作的技巧,具有一定参考借鉴价值,需要的朋友可以参考下。
本文实例讲述了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>
出处:http://www.phpfensi.com/php/20210520/15648.html
栏目列表
最新更新
Python获取微信好友数据
Python 的排序方法 sort 和 sorted 的区别
WinForm轻松实现自定义分页 (转载)
Mac系统下使用brew搭建PHP(LNMP/LAMP)开发环境
在win系统安装配置 Memcached for PHP 5.3 图文
箱图在数据预处理中的应用
Python WEB开发:用Tornado框架制作简易【表
自从学会了用python解析视频,都不用去找
Python代码阅读(第50篇):对列表间隔取
Python爬虫+数据分析+可视化展示,分析《
MongoDB常用命令(2)
MongoDB基本介绍与安装(1)
SQLServer触发器调用JavaWeb接口
SQL Server索引的原理深入解析
SqlServer2016模糊匹配的三种方式及效率问题
SQL中Truncate的用法
sqlserver 多表关联时在where语句中慎用tri
链接服务器读取Mysql---出现消息 7347,级别
SQL Server解惑——为什么你拼接的SQL语句换
MySQL视图了解一下
laf.js - 开源的云开发框架(README.md)
javascript创建对象
Node.js 源码分析 - 从 main 函数开始
Node.js 源码分析 - 原生模块(C++模块)的注册
ECharts图标中用的js相关的处理方法
GoJS 使用笔记
单元测试 - 测试场景记录
Node.js 源码分析 - 加载 js 文件
ES6入门
聊聊动效降级