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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

php集成开发环境详解

HP开发使用的集成环境,可用PHPStorm, 或者用免费版的 IDEA 加 PHP 插件,两者功能基本相同,只是后者安装起来略折腾。 PHPStorm的特点:跨平台,我在 Window...

php bootstrap实现简单登录

php bootstrap实现简单登录

本文实例为大家分享了php bootstrap实现简单登录的方法,供大家参考,具体内容如下 所有文件 记住是获取name键值对 from 里面 action就是你的动作 si...

利用PHP判断是手机移动端还是PC端访问的函数示例

前言 最近在开发项目的时候,分别开发了PC端和手机端,需要实现,用手机访问PC端WWW域名的时候,自动判断跳转到移动端,用电脑访问M域名手机网站的时候,自动跳转到PC端网站,于是有了下面...

PHP基于面向对象封装的分页类示例

本文实例讲述了PHP基于面向对象封装的分页类。分享给大家供大家参考,具体如下: <?php class Page { protected $num;//...

PHP中HTML标签过滤技巧

在开发文章系统中正常需要用到HTML标签、JS脚本等其他脚本代码的过滤,稍微尝试了下,感觉简单的htmlspecialchars()函数的过滤效果始终不如strip_tags()函数的过...