Javascript PHP上载文件在上载后将动画gif转换为常规图像
我在网上找到了这个上传脚本,一切都很完美 只有一件事我自己搞不清楚,那个就是每当我上传一个动画GIF图像时,它就会停止动画,并变成一个带有扩展GIF的常规图像 这是上传文件Javascript PHP上载文件在上载后将动画gif转换为常规图像,javascript,php,jquery,image,Javascript,Php,Jquery,Image,我在网上找到了这个上传脚本,一切都很完美 只有一件事我自己搞不清楚,那个就是每当我上传一个动画GIF图像时,它就会停止动画,并变成一个带有扩展GIF的常规图像 这是上传文件 if(isset($_POST)) { ############ Edit settings ############## $ThumbSquareSize = 150; //Thumbnail will be 200x200 $BigImageMaxSize = 800; //Image Max
if(isset($_POST))
{
############ Edit settings ##############
$ThumbSquareSize = 150; //Thumbnail will be 200x200
$BigImageMaxSize = 800; //Image Maximum height or width
$ThumbPrefix = "thumb_"; //Normal thumb Prefix
$DestinationDirectory = '../images/uploads/';
//specify upload directory ends with / (slash)
$Quality = 90; //jpeg quality
##########################################
//check if this is an ajax request
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
die();
}
// check $_FILES['ImageFile'] not empty
if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name']))
{
die('Something wrong with uploaded file, something missing!'); // output error when above checks fail.
}
// Get the $_POST image tag
if(isset($_POST['tag']))
{
$tt = secure($_POST['tag']);
$tag = str_replace(' ','',$tt);
}else{
$tag = 'no-tag';
}
// Get the $_POST image title
if(isset($_POST['img_title']))
{
$img_title = secure($_POST['img_title']);
}else{
$img_title = NULL;
}
// Random number will be added after image name
$RandomNumber = random_str(35);
$ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name'])); //get image name
$ImageSize = $_FILES['ImageFile']['size']; // get original image size
$TempSrc = $_FILES['ImageFile']['tmp_name']; // Temp name of image file stored in PHP tmp folder
$ImageType = $_FILES['ImageFile']['type']; //get file type, returns "image/png", image/jpeg, text/plain etc.
//Let's check allowed $ImageType, we use PHP SWITCH statement here
switch(strtolower($ImageType))
{
case 'image/png':
//Create a new image from file
$CreatedImage = imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
break;
case 'image/gif':
$CreatedImage = imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
break;
case 'image/jpeg':
case 'image/pjpeg':
$CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
break;
default:
die('Unsupported File!'); //output error and exit
}
//PHP getimagesize() function returns height/width from image file stored in PHP tmp folder.
//Get first two values from image, width and height.
//list assign svalues to $CurWidth,$CurHeight
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
//Get file extension from Image name, this will be added after random name
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
//remove extension from filename
$ImageName = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName);
//Construct a new name with random number and extension.
$NewImageName = $RandomNumber.'.'.$ImageExt;
//set the Destination Image
$thumb_DestRandImageName = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumbnail name with destination directory
$DestRandImageName = $DestinationDirectory.$NewImageName; // Image with destination directory
//Resize image to Specified Size by calling resizeImage function.
if(resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType))
{
//Create a square Thumbnail right after, this time we are using cropImage() function
if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType))
{
echo 'Error Creating thumbnail';
}
/*
We have succesfully resized and created thumbnail image
We can now output image to user's browser or store information in the database
*/
echo '<table width="100%" border="0" cellpadding="4" cellspacing="0">';
echo '<tr>';
echo '<td align="center"><img src="/images/uploads/'.$ThumbPrefix.$NewImageName.'" alt="Thumbnail"></td>';
echo '</tr>';
echo '</table>';
mysqli_query($con,"INSERT INTO pics (user_id,img_id,img_url, img_url_s, img_tag, img_title, date) VALUES ('".$user['id']."','".$RandomNumber."','/images/uploads/".$NewImageName."','".str_replace('..','',$thumb_DestRandImageName)."', '".secure($tag)."','".secure($img_title)."',CURRENT_TIMESTAMP)");
}else{
die('Resize Error'); //output error
}
}
// This function will proportionally resize image
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
//Construct a proportional size of new image
$ImageScale = min($MaxSize/$CurWidth, $MaxSize/$CurHeight);
$NewWidth = ceil($ImageScale*$CurWidth);
$NewHeight = ceil($ImageScale*$CurHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
// Resize Image
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//Destroy image, frees memory
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
//This function corps image to create exact square images, no matter what its original size!
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
//Check Image size is not 0
if($CurWidth <= 0 || $CurHeight <= 0)
{
return false;
}
//abeautifulsite.net has excellent article about "Cropping an Image to Make Square bit.ly/1gTwXW9
if($CurWidth>$CurHeight)
{
$y_offset = 0;
$x_offset = ($CurWidth - $CurHeight) / 2;
$square_size = $CurWidth - ($x_offset * 2);
}else{
$x_offset = 0;
$y_offset = ($CurHeight - $CurWidth) / 2;
$square_size = $CurHeight - ($y_offset * 2);
}
$NewCanves = imagecreatetruecolor($iSize, $iSize);
if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
{
switch(strtolower($ImageType))
{
case 'image/png':
imagepng($NewCanves,$DestFolder);
break;
case 'image/gif':
imagegif($NewCanves,$DestFolder);
break;
case 'image/jpeg':
case 'image/pjpeg':
imagejpeg($NewCanves,$DestFolder,$Quality);
break;
default:
return false;
}
//Destroy image, frees memory
if(is_resource($NewCanves)) {imagedestroy($NewCanves);}
return true;
}
}
if(isset($\u POST))
{
############编辑设置##############
$ThumbSquareSize=150;//缩略图将为200x200
$BigImageMaxSize=800;//图像最大高度或宽度
$ThumbPrefix=“thumb\uz”;//普通thumb前缀
$DestinationDirectory='../images/uploads/';
//指定上载目录以/(斜杠)结尾
$Quality=90;//jpeg质量
##########################################
//检查这是否是一个ajax请求
如果(!isset($\u服务器['HTTP\u X\u请求的\u带有'])){
模具();
}
//检查$\u文件['ImageFile']是否不为空
如果(!isset($_FILES['ImageFile'])| |!是否上载了文件($_FILES['ImageFile']['tmp_name']))
{
die('上传的文件有问题,丢失了!');//当上述检查失败时,输出错误。
}
//获取$\u POST图像标记
如果(isset($_POST['tag']))
{
$tt=安全($_POST['tag']);
$tag=str_替换('',$tt);
}否则{
$tag='无标签';
}
//获取$\u POST图像标题
如果(isset($\u POST['img\u title']))
{
$img_title=安全($_POST['img_title']);
}否则{
$img_title=NULL;
}
//随机数将添加在图像名称之后
$RandomNumber=random_str(35);
$ImageName=str_replace(“”,“-”,strtolower($_FILES['ImageFile']['name']);//获取图像名称
$ImageSize=$\u文件['ImageFile']['size'];//获取原始图像大小
$TempSrc=$\u FILES['ImageFile']['tmp\u name'];//存储在PHP tmp文件夹中的图像文件的临时名称
$ImageType=$_FILES['ImageFile']['type'];//获取文件类型,返回“image/png”、image/jpeg、text/plain等。
//让我们检查允许的$ImageType,我们在这里使用PHP开关语句
开关(strtolower($ImageType))
{
案例“image/png”:
//从文件创建新图像
$CreatedImage=imagecreatefrompng($_文件['ImageFile']['tmp_名称]]);
打破
案例“image/gif”:
$CreatedImage=imagecreatefromgif($_文件['ImageFile']['tmp_名称]]);
打破
案例“图像/jpeg”:
案例“image/pjpeg”:
$CreatedImage=imagecreatefromjpeg($_文件['ImageFile']['tmp_名称]]);
打破
违约:
die('Unsupported File!');//输出错误并退出
}
//函数的作用是:返回存储在PHP tmp文件夹中的图像文件的高度/宽度。
//从图像中获取前两个值:宽度和高度。
//列表将S值分配给$CurWidth、$CurHeight
列表($CurWidth,$CurHeight)=getimagesize($TempSrc);
//从图像名称获取文件扩展名,该扩展名将添加到随机名称之后
$ImageExt=substr($ImageName,strrpos($ImageName,'.');
$ImageExt=str_replace('.',''.$ImageExt);
//从文件名中删除扩展名
$ImageName=preg\u replace(“/\\..^\\s]{3,4}$/”,“”,$ImageName);
//用随机数和扩展名构造一个新名称。
$NewImageName=$RandomNumber....$ImageExt;
//设置目标映像
$thumb_DestRandImageName=$DestinationDirectory.$ThumbPrefix.$NewImageName;//带有目标目录的缩略图名称
$DestRandImageName=$DestinationDirectory.$NewImageName;//具有目标目录的图像
//通过调用resizeImage函数将图像调整为指定大小。
如果(调整图像大小($CurWidth、$CurHeight、$BigImageMaxSize、$DestRandImageName、$CreatedImage、$Quality、$ImageType))
{
//之后立即创建一个方形缩略图,这次我们使用cropImage()函数
如果(!cropImage($CurWidth、$CurHeight、$ThumbSquareSize、$thumb_DestRandImageName、$CreatedImage、$Quality、$ImageType))
{
回显“创建缩略图时出错”;
}
/*
我们已经成功地调整了大小并创建了缩略图
我们现在可以将图像输出到用户的浏览器或将信息存储在数据库中
*/
回声';
回声';
回声';
回声';
回声';
mysqli_query($con,“插入pics(用户id、img_id、img_url、img_url、img_标记、img_标题、日期)值(“$user['id']。”、“$RandomNumber.”、“/images/uploads/”$NewImageName.”、“.str_替换(“…”、“$thumb_-DestRandImageName”)、“.secure($tag)。”、“.secure($img_标题)。”、“、当前时间戳)”);
}否则{
模具('Resize Error');//输出错误
}
}
//此函数将按比例调整图像大小
函数resizeImage($CurWidth、$CurHeight、$MaxSize、$DestFolder、$SrcImage、$Quality、$ImageType)
{
//检查图像大小是否为0
如果($CurWidthGD无法处理动画GIF。您需要使用不同的库,如ImageMagick,不幸的是,它比GD更难使用…GD无法处理动画GIF。您需要使用不同的库,如ImageMagick,不幸的是,它比GD更难使用…GD无法处理动画GIFdle动画GIF。您将需要使用不同的库,如ImageMagick,不幸的是,它比GD更难使用…GD无法处理动画GIF。您将需要使用不同的库,如ImageMagick,不幸的是,它比GD更难使用…GD默认情况下无法处理GIF。我建议结束使用WideImage,它在OO方式下使用非常简单。GD默认情况下无法处理GIF。我建议使用WideImage,它在OO方式下使用非常简单。GD默认情况下无法处理GIF。我建议使用WideImage,它在OO方式下使用非常简单。GD无法通过defau处理GIFlt.我建议使用WideImage,它的使用非常简单,并且以面向对象的方式使用。糟糕,无论如何谢谢你,也感谢你的快速回放。糟糕,无论如何谢谢你,也感谢你的快速回放。糟糕,无论如何谢谢你,也感谢你的快速回放。糟糕,无论如何谢谢你,也感谢你的快速回放。