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

yipeiwu_com5年前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扩展编写点滴 技巧收集

红色部分是我的注释。 更多信息参看: 1.常用的通用功能已经封装好了,在如zen_API.h 头文件中,不用费力查看内部细节,浪费时间。(参考:Extending and Embeddi...

深入理解require与require_once与include以及include_once的区别

PHP具有快速、可靠、跨平台应用、源代码开放等特点,使得PHP成为最受欢迎的服务器端Script语言之一。我根据自己在工作中体会到的,向大家介绍PHP使用的心得,希望对大家有所帮助。 利...

php实现快速排序法函数代码

代码1: 复制代码 代码如下: <?php function quicksort($str){ if(count($str)<=1) return $str;//如果个数不大...

PHP 截取字符串专题集合

1、UTF-8、GB2312都支持的汉字截取函数 复制代码 代码如下: <?php /* Utf-8、gb2312都支持的汉字截取函数 cut_str(字符串, 截取长度, 开始长...

PHP获取文件行数的方法

本文实例讲述了PHP获取文件行数的方法。分享给大家供大家参考。具体分析如下: 提供两种实现方法,虽然第二种简单易懂,但是第一种效率最好 第一种: <?php $fil...