php使用GD生成图片的缩略图

本文介绍了一个简单的文件上传功能实现,包括HTML代码展示、PHP后端处理逻辑,以及上传过程中的错误处理与缩略图生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<!--
 * Created on 2015-5-23
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
-->
 <head>
  <title> 文件上传</title>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
 </head>
 <body>
 <form action="Thumb.php" method="post" enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
  请选择要上传的文件:<input type="file" id="upfile" name="upfile"/>
  <input type="submit" value="上传" id="submit">
 
 </form>
 </body>
</html>


#############################################

##########################################

<?php
/*
 * Created on 2015-5-23
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 header("Content-Type:text/html;charset=utf-8");
 $upload="Thumbnail.html";
 $dir=dirname(realpath(__FILE__)).DIRECTORY_SEPARATOR;
 $maxUploadSize=ini_get('upload_max_filesize');
 $err_msg=false;
 if(!isset($_FILES['upfile'])){
  $err_msg="表单不完全!";
 }else{
  $fileImg=$_FILES['upfile'];
 }
 echo $fileImg['error'];
 switch($fileImg['error']){
  case UPLOAD_ERR_INI_SIZE:
  $err_msg="文件超过最大上传限制:$maxUploadSize\n";
  break;
  case UPLOAD_ERR_PARTIAL:
  $err_msg="文件上传不完全,请重新<a href='{$upload}'>上传</a>\n";
  break;
  case UPLOAD_ERR_NO_FILE:
  $err_msg="没有选择文件,请重新<a href='{$upload}'>上传</a>\n";
  break;
 // case UPLOAD_ERR_FROM_SIZE:
 // $err_msg="文件超过页面最大上传限制.";
 // echo "okkoko";
 // break;
  case UPLOAD_ERR_CANT_WRITE:
  $err_msg="文件写入失败,请重新<a href='{$upload}'>上传</a>\n";
  break;
  case UPLOAD_ERR_NO_TMP_DIR:
  $err_msg="没有临时文件夹,请重新<a href='{$upload}'>上传</a>\n";
  break;
  case UPLOAD_ERR_OK:
  break;
  default:
  $err_msg="未知错误.请重新<a href='{$upload}'>上传</a>\n";
 }
 
 //echo $fileImg['type'];
// if(!in_array($fileImg['type'],array('image/jpeg','image/pjpeg','image/png','image/gif'))){
 // $err_msg="只允许上传.png或.jpeg图片,请重新<a href='{$upload}'>上传</a>\n";
 //}
 if(!$err_msg){
  if(!move_uploaded_file($fileImg['tmp_name'],$dir.$fileImg['name'])){
  $err_msg="移动文件失败,请重新<a href='{$upload}'>上传</a>\n";
  }
 }
 
 $src_file=$fileImg['name'];
 $dest_file="small_".$fileImg['name'];
 
 $imginfo=getimagesize($src_file);
 $srcWidth=$imginfo[0];
 $srcHeight=$imginfo[1];
 $quantity="80";
 
 $new_size=array('width'=>100,'height'=>78);
 $radio=max(($srcWidth/$new_size['width']),($srcHeight/$new_size['height']));
 $destWidth=(int)$srcWidth/$radio;
 $destHeight=(int)$srcHeight/$radio;
 
 if($imginfo[2]==2){
  $src_img=imagecreatefromjpeg($src_file);
 }elseif($imginfo[2]==3){
  //echo "png";
  $src_img=imagecreatefrompng($src_file);
 }else{
  $src_img=imagecreatefromgif($src_file);
 }
 
 if(!$src_img){
  $err_msg="生成缩略图失败!请重新<a href='{$upload}'>上传</a>\n";
 }else{
  $dst_img=imagecreatetruecolor($destWidth,$destHeight);
  imagecopyresampled($dst_img,$src_img,0,0,0,0,$destWidth,$destHeight,$srcWidth,$srcHeight);
  imagejpeg($dst_img,$dest_file,$quantity);
  imagedestroy($src_img);
  imagedestroy($dst_img);
 }
// echo $fileImg['size'];
 if($err_msg){
  echo $err_msg;
 }else{
  echo "<img src='{$fileImg['name']}' alt='上传的原始文件' title='上传的原始文件' />";
  echo "<img src='{$dest_file}' alt='缩略图' title='缩略文件'/>";
  echo "上传成功,生成缩略图成功.";
 }
 
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值