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

 更新时间:2015年04月02日 11:29:44   作者:不吃皮蛋  
这篇文章主要介绍了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>

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

相关文章

  • Look And Say 序列php实现代码

    Look And Say 序列php实现代码

    Look And Say序列,简单的说就是根据你看到的数字,写出下一个数字
    2011-05-05
  • php生成excel列名超过26列大于Z时的解决方法

    php生成excel列名超过26列大于Z时的解决方法

    这篇文章主要介绍了php生成excel列名超过26列大于Z时的解决方法,是针对采用PHPExcel生成excel列数过多的情况下的完善技巧,具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • 利用PHP如何统计Nginx日志的User Agent数据

    利用PHP如何统计Nginx日志的User Agent数据

    这篇文章主要给大家介绍了关于如何利用PHP统计Nginx日志的User Agent数据的相关资料,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • php简单生成随机数的方法

    php简单生成随机数的方法

    这篇文章主要介绍了php简单生成随机数的方法,该功能可用于生成验证码,涉及php字符串及mt_rand方法的使用技巧,需要的朋友可以参考下
    2015-07-07
  • 两款万能的php分页类

    两款万能的php分页类

    这篇文章主要介绍了万能的php分页类,特别好用,需要使用php分页类的朋友不要错过。
    2015-11-11
  • 如何用PHP实现分布算法之一致性哈希算法

    如何用PHP实现分布算法之一致性哈希算法

    进行大型网站的web开发时,分布式这个词经常出现在我们面前。如: memcache、redis服务器等缓存服务器的负载均衡(分布式cache)、 MySQL的分布式集群,这些都会用到分布式的思想,都要理解分布式算法。接下来以缓存服务器的负载均衡来谈一下一致性哈希算法。
    2021-05-05
  • php在linux下检测mysql同步状态的方法

    php在linux下检测mysql同步状态的方法

    这篇文章主要介绍了php在linux下检测mysql同步状态的方法,是Linux下使用php检测mysql同步状态的实用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • 让PHP COOKIE立即生效,不用刷新就可以使用

    让PHP COOKIE立即生效,不用刷新就可以使用

    让PHP COOKIE立即生效,不用刷新就可以使用,需要的朋友可以参考下。
    2011-03-03
  • php CI框架插入一条或多条sql记录示例

    php CI框架插入一条或多条sql记录示例

    本节主要为大家介绍了php CI框架如何插入一条或多条sql记录,示例代码如下,不了解的朋友不要错过
    2014-07-07
  • php中数据格式判断方法总结

    php中数据格式判断方法总结

    这篇文章主要为大家详细介绍了php中数据格式判断方法,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-12-12

最新评论