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

(0)

相关推荐

  • php上传文件常见问题总结

    把php上传文件时经常碰到的几个问题总结一下吧,以后用到时不用再去找了. 1.先做个最简单的上传文件 复制代码 代码如下: <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  </head>  <body>  <form action="upload_file.php&quo

  • 如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量的

    可以使用以下函数来实现: <?php function posttohost($url, $data) { $url = parse_url($url); if (!$url) return "couldn't parse url"; if (!isset($url['port'])) { $url['port'] = ""; } if (!isset($url['query'])) { $url['query'] = ""; } $enc

  • php实现通过ftp上传文件

    大概原理 遍历项目中的所有非排除文件,然后获取 文件修改时间晚于文件上一次修改时间 的文件 然后将这些文件,通过ftp上传到对应的目录 具体代码如下: 因为只是工具,代码很乱,见谅 <?php error_reporting(7); if ($_SERVER['SERVER_ADDR'])exit;//禁止在web服务器下运行 $_GET['exclude'] = array('number.txt','uploads','Zend','docs','cache','You','managesd

  • 求帮忙修改个php curl模拟post请求内容后并下载文件的解决思路

    下面代码使用curl模拟post请求链接后直接显示出了文件内容,如何修改成不显示内容而直接下载请求到的.torrent格式文件呢 function curl_post($header,$data,$url) { $ch = curl_init(); $res= curl_setopt ($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERI

  • PHP实现ftp上传文件示例

    FTP上传是PHP实现的一个常见且非常重要的应用技巧,今天就来与大家分享一下PHP实现FTP上传文件的简单示例.希望对大家的PHP学习能带来一定的帮助. 主要代码如下: function make_directory($ftp_stream, $dir){ // if directory already exists or can be immediately created return true if ($this->ftp_is_dir($ftp_stream, $dir) || @ftp

  • PHP响应post请求上传文件的方法

    本文实例讲述了PHP响应post请求上传文件的方法.分享给大家供大家参考,具体如下: function send_file($url, $post = '', $file = '') { $eol = "\r\n"; $mime_boundary = md5 ( time () ); $data = ''; $confirmation = ''; date_default_timezone_set ( "Asia/Shanghai" ); $time = date

  • php上传文件问题汇总

    1.先做个最简单的上传文件 复制代码 代码如下: <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  </head>  <body>  <form action="upload_file.php" method="post"  enctype=&

  • PHP整合七牛实现上传文件

    七牛支持抓取远程图片 API,用 access_key + secret_key + url 生成 access_token, 把 access_token 加在 header 里,然后向 post url 就完成上传了. Sample code: <?php /* * * @desc URL安全形式的base64编码 * @param string $str * @return string */ function urlsafe_base64_encode($str){ $find = ar

  • php curl 上传文件代码实例

    假设server端上传文件处理脚本upload.php: 复制代码 代码如下: <?php    print_r($_POST);  print_r($_FILES); 1.使用 CURL 默认的方法 复制代码 代码如下: //如果php文件是utf8编码,系统是GBK编码,那么就需要转下编码,要不然Php在系统中找不到这个文件    $file = realpath(mb_convert_encoding('测试图片.JPG','GBK','utf8'));    $file = realpa

  • PHP判断上传文件类型的解决办法

    分享给大家php判断上传文件类型的方法,大家一起学习学习. /** * 读取文件前几个字节 判断文件类型 * @return String */ function checkTitle($filename){ $file=fopen($filename, "rb"); $bin=fread($file, 2); //只读2字节 fclose($file); $strInfo =@unpack("c2chars", $bin); $typeCode=intval($s

  • PHP文件上传判断file是否己选择上传文件的方法

    本文实例讲述了PHP文件上传判断file是否己选择上传文件的方法.分享给大家供大家参考.具体方法如下: 一个合格的程序员在实现数据入库中时我们都会有一些非常严密的过滤与数据规则,像我们文件上传时在前段要判断用户是否选择上传文件同时在后台也可判断是否有上传的文件,本文实例即对此做一较为深入的分析. 如下html代码所示: 复制代码 代码如下: <form action="?" method="post" enctype='multipart/form-data'

随机推荐