文件上传并生成几种大小的缩略图之二-阿里云开发者社区

开发者社区> y0umer> 正文

文件上传并生成几种大小的缩略图之二

简介: Image uploader Ajax图片上传生成缩略图,原版是国外一个有名的图片上传程序,采用无刷新的Ajax上传方式,上传成功后,可生成四张不同大小的缩略图,参数可以自己调整,演示效果可看如本站首页。
+关注继续查看
<?php

//----------------------------------------- start edit here ---------------------------------------------//
$script_location = "http://up.online.cm"; // location fo the script
$maxlimit = 9048576; // maxim image limit
$folder = "images"; // folder where to save images

// requirements
$minwidth = 500; // minim width
$minheight = 500; // minim height
$maxwidth = 2560; // maxim width
$maxheight = 1920; // maxim height

// allowed extensions
$extensions = array('.png', '.gif', '.jpg', '.jpeg','.PNG', '.GIF', '.JPG', '.JPEG');
//----------------------------------------- end edit here ---------------------------------------------//

	// check that we have a file
	if((!empty($_FILES["uploadfile"])) && ($_FILES['uploadfile']['error'] == 0)) {

	// check extension
	$extension = strrchr($_FILES['uploadfile']['name'], '.');
	if (!in_array($extension, $extensions))	{
		echo 'wrong file format, alowed only .png , .gif, .jpg, .jpeg
		<script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
	} else {

// get file size
$filesize = $_FILES['uploadfile']['size'];

	// check filesize
	if($filesize > $maxlimit){ 
		echo "File size is too big.";
	} else if($filesize < 1){ 
		echo "File size is empty.";
	} else {

// temporary file
$uploadedfile = $_FILES['uploadfile']['tmp_name'];

// capture the original size of the uploaded image
list($width,$height) = getimagesize($uploadedfile);

	// check if image size is lower
	if($width < $minwidth || $height < $minheight){ 
		echo 'Image is to small. Required minimum '.$minwidth.'x'.$minheight.'
		<script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
	} else if($width > $maxwidth || $height > $maxheight){ 
		echo 'Image is to big. Required maximum '.$maxwidth.'x'.$maxheight.'
		<script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
	} else {

// all characters lowercase
$filename = strtolower($_FILES['uploadfile']['name']);

// replace all spaces with _
$filename = preg_replace('/\s/', '_', $filename);

// extract filename and extension
$pos = strrpos($filename, '.'); 
$basename = substr($filename, 0, $pos); 
$ext = substr($filename, $pos+1);

// get random number
$rand = time();

// image name
$image = $basename .'-'. $rand . "." . $ext;

// check if file exists
$check = $folder . '/' . $image;
	if (file_exists($check)) {
		echo 'Image already exists';
	} else {

// check if it's animate gif
$frames = exec("identify -format '%n' ". $uploadedfile ."");
	if ($frames > 1) {
		// yes it's animate image
		// copy original image
		copy($_FILES['uploadfile']['tmp_name'], $folder . '/' . $image);

		// orignal image location
		$write_image = $folder . '/' . $image;
		//ennable form
		echo '<img src="' . $write_image . '" alt="'. $image .'" alt="'. $image .'" width="500" /><br />
<input type="text" name="location" value="[IMG]'.$script_location.''.$write_image.'[/IMG]" class="location corners" />
<script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
	} else {

// create an image from it so we can do the resize
 switch($ext){
  case "gif":
	$src = imagecreatefromgif($uploadedfile);
  break;
  case "jpg":
	$src = imagecreatefromjpeg($uploadedfile);
  break;
  case "jpeg":
	$src = imagecreatefromjpeg($uploadedfile);
  break;
  case "png":
	$src = imagecreatefrompng($uploadedfile);
  break;
 }

// copy original image
copy($_FILES['uploadfile']['tmp_name'], $folder . '/' . $image);

// orignal image location
$write_image = $folder . '/' . $image;

// create first thumbnail image - resize original to 80 width x 80 height pixels 
$newheight = ($height/$width)*80;
$newwidth = 80;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagealphablending($tmp, false);
imagesavealpha($tmp,true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// write thumbnail to disk
$write_thumbimage = $folder .'/thumb-'. $image;
 switch($ext){
  case "gif":
	imagegif($tmp,$write_thumbimage);
  break;
  case "jpg":
	imagejpeg($tmp,$write_thumbimage,100);
  break;
  case "jpeg":
	imagejpeg($tmp,$write_thumbimage,100);
  break;
  case "png":
	imagepng($tmp,$write_thumbimage);
  break;
 }

// create second thumbnail image - resize original to 125 width x 125 height pixels 
$newheight = ($height/$width)*125;
$newwidth = 125;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagealphablending($tmp, false);
imagesavealpha($tmp,true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// write thumbnail to disk
$write_thumb2image = $folder .'/thumb2-'. $image;
 switch($ext){
  case "gif":
	imagegif($tmp,$write_thumb2image);
  break;
  case "jpg":
	imagejpeg($tmp,$write_thumb2image,100);
  break;
  case "jpeg":
	imagejpeg($tmp,$write_thumb2image,100);
  break;
  case "png":
	imagepng($tmp,$write_thumb2image);
  break;
 }

// create third thumbnail image - resize original to 125 width x 125 height pixels 
$newheight = ($height/$width)*250;
$newwidth = 250;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagealphablending($tmp, false);
imagesavealpha($tmp,true);
$transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// write thumbnail to disk
$write_thumb3image = $folder .'/thumb3-'. $image;
 switch($ext){
  case "gif":
	imagegif($tmp,$write_thumb3image);
  break;
  case "jpg":
	imagejpeg($tmp,$write_thumb3image,100);
  break;
  case "jpeg":
	imagejpeg($tmp,$write_thumb3image,100);
  break;
  case "png":
	imagepng($tmp,$write_thumb3image);
  break;
 }

// all is done. clean temporary files
imagedestroy($src);
imagedestroy($tmp);

// image preview
echo "<img src='" . $write_thumbimage . "' alt='". $image ."' /><br />
<input type='text' name='location' value='[IMG]".$script_location."/". $write_thumbimage ."[/IMG]' class='location corners' /><br />
<br />
<img src='" . $write_thumb2image . "' alt='". $image ."' /><br />
<input type='text' name='location' value='[IMG]".$script_location."/". $write_thumb2image ."[/IMG]' class='location corners' /><br />
<br />
<img src='" . $write_thumb3image . "' alt='". $image ."' /><br />
<input type='text' name='location' value='[IMG]".$script_location."/". $write_thumb3image ."[/IMG]' class='location corners' /><br />
<br />
<img src='" . $write_image . "' alt='". $image ."' alt='". $image ."' width='500' /><br />
<input type='text' name='location' value='[IMG]".$script_location."/".$write_image."[/IMG]' class='location corners' />
<script language='javascript' type='text/javascript'>window.top.window.formEnable();</script>
<div class='clear'></div>";
	  }
	}
  }
}
	// database connection
	include('inc/db.inc.php');

			// insert into mysql database and show success message
			mysql_query("INSERT INTO `image_upload` (`id`, `image`, `thumbnail`, `thumbnail2`, `thumbnail3` ) VALUES (NULL, '". $image ."', 'thumb-". $image ."', 'thumb2-". $image ."', 'thumb3-". $image ."')");
	  }
		// error all fileds must be filled
	} else {
		echo '<div class="wrong">You must to fill all fields!</div>'; }
?>
Image uploader Ajax图片上传生成缩略图,原版是国外一个有名的图片上传程序,采用无刷新的Ajax上传方式,上传成功后,可生成四张不同大小的缩略图,参数可以自己调整,演示效果可看如本站首页。同时它还可实现与数据库对接,将图片信息保存至数据库。
在本站上传图片只需要点击浏览选择图片后就会自动上传
在本站上传图片后会在首页显示最新上传的9张
上传图片根据图片大小时间不等.请耐心等待.
如等待几分钟后还处于上传状态.请点击本站logo刷新本页.如果您所上传的图片了出现了首页下部.证明上传成功.可以根据下面所说的规则得出其它图片地址.
本站为测试用,不做商业用途.所以带宽有限.如遇上传失败,请重新上传.
首页展示图片为第一张缩略图 格式为:http://up.online.cm/images/thumb-原图地址
根据上一条的说明四张图片分别为 1.thumb-原图地址;2.thumb2-原图地址;3.thumb3-原图地址;4.原图地址
论据以上说明.如果知道一张图片的地址了,就可以知道其它图片的地址了.
本站四张图片分别规格为 限制宽: 80px 125px 250px 原图
本站上传图片支持格式为:gif, jpg, png
本站上传图片尺寸限制为:最小不能小于 100px x100px 最大不能大于 2560px x 1920px
如果本站配置不能满足您的要求的话.请您自行下载源码安装到空间中.

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
推荐一大波让你直呼哇塞的Canvas库【值得收藏】
推荐一大波让你直呼哇塞的Canvas库【值得收藏】
3 0
Vite2 + Vue3 + TypeScript + Pinia 搭建一套企业级的开发脚手架【值得收藏】
Vite2 + Vue3 + TypeScript + Pinia 搭建一套企业级的开发脚手架【值得收藏】
2 0
Vue3.x <script setup> 语法糖详解,助力快速上手Vue3.x【值得收藏】
Vue3.x <script setup> 语法糖详解,助力快速上手Vue3.x【值得收藏】
6 0
基于 H5 Canvas "指纹识别" 技术 【浏览器指纹 VS Canvas指纹】
基于 H5 Canvas "指纹识别" 技术 【浏览器指纹 VS Canvas指纹】
5 0
高性能渲染十万条数据不卡顿【虚拟滚动】
高性能渲染十万条数据不卡顿【虚拟滚动】
5 0
5款非常优秀的基于 vue3.x 和 Native UI 的中后台管理系统模板
5款非常优秀的基于 vue3.x 和 Native UI 的中后台管理系统模板
5 0
数据库中间件 MyCAT源码分析 —— PreparedStatement 重新入门
数据库中间件 MyCAT源码分析 —— PreparedStatement 重新入门
4 0
2022年的Flag从搭建博客开始,设计模式,数据结构,算法汇总【手摸手系列】
2022年的Flag从搭建博客开始,设计模式,数据结构,算法汇总【手摸手系列】
2 0
为什么下载小电影时,经常会卡在99%?
为什么下载小电影时,经常会卡在99%?
5 0
user-modify VS contenteditable 【实现掘金沸点话题标签】
user-modify VS contenteditable 【实现掘金沸点话题标签】
6 0
+关注
y0umer
高级网络安全技术员
906
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载