画像アップロード フォームに問題があります。アニメーション GIF をアップロードすると、もはやアニメーション化されていないようです。この問題を解決する方法はありますか? または、.gif 拡張子のサイズ変更をスキップして、.jpg、png などの他のファイル タイプのみをサイズ変更できれば問題ありません。
upload.php コード
<?php
// replace with your mysql database details
$MySql_username = "root"; //mysql username
$MySql_password = ""; //mysql password
$MySql_hostname = "localhost"; //hostname
$MySql_databasename = 'upload'; //databasename
$UploadDirectory = 'uploads/'; //Upload Directory, ends with slash & make sure folder exist
$UploadThumbDirectory ='uploads/thumb/';//Upload thumb Directory, ends with slash & make sure folder exist
//Some Settings
$ThumbMaxWidth = 150; //Thumbnail width
$ThumbMaxHeight = 150; //Thumbnail Height
$BigImageMaxWidth = 700; //Resize Image width to
$BigImageMaxHeight = 700; //Resize Image height to
$ThumbPrefix = "thumb_"; //Normal thumb Prefix
if (!@file_exists($UploadDirectory)) {
//destination folder does not exist
die("Make sure Upload directory exist!");
}
if (!@file_exists($UploadThumbDirectory)) {
//destination folder does not exist
die("Make sure Upload Thumb directory exist!");
}
if($_POST)
{
if(!isset($_POST['mName']) || strlen($_POST['mName'])<1)
{
//required variables are empty
die("Title is empty!");
}
if(!isset($_FILES['mFile']))
{
//required variables are empty
die("File is empty!");
}
if($_FILES['mFile']['error'])
{
//File upload error encountered
die(upload_errors($_FILES['mFile']['error']));
}
$ImageName = strtolower($_FILES['mFile']['name']); //uploaded file name
$Imageitle = $_POST['mName']; // file title
$ImageExt = substr($ImageName, strrpos($ImageName, '.')); //file extension
$ImageType = $_FILES['mFile']['type']; //file type
$ImageSize = $_FILES['mFile']["size"]; //file size
$TempSrc = $_FILES['mFile']['tmp_name'];
$uploaded_date = date("Y-m-d H:i:s");
$process = true;
//Validate file + create image from uploaded file.
switch(strtolower($ImageType))
{
case 'image/png':
$CreatedImage = imagecreatefrompng($_FILES['mFile']['tmp_name']);
break;
case 'image/gif':
$CreatedImage = @imagecreatefromstring(file_get_contents(($_FILES['mFile']['tmp_name'])));
break;
case 'image/jpeg':
$CreatedImage = imagecreatefromjpeg($_FILES['mFile']['tmp_name']);
break;
case 'image/bmp':
$CreatedImage = imagecreatefromjpeg($_FILES['mFile']['tmp_name']);
break;
default:
die('Unsupported File!'); //output error
}
//get Image Size
list($CurWidth,$CurHeight)=getimagesize($TempSrc);
//File Title will be used as new File name
$NewName = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), strtolower($Imageitle));
$DestRandImageName = $NewName.'_'.date("YmdHis").$ImageExt;
$NewThumbFileName = $NewName.'_'.$ThumbPrefix.date("YmdHis").$ImageExt;
//Resize image to our Specified Size by calling our resizeImage function.
if(resizeImage($CurWidth,$CurHeight,$BigImageMaxWidth,$BigImageMaxHeight,$UploadDirectory .$DestRandImageName,$CreatedImage))
{
//Create Thumbnail for the Image
resizeImage($CurWidth,$CurHeight,$ThumbMaxWidth,$ThumbMaxHeight,$UploadThumbDirectory.$NewThumbFileName,$CreatedImage);
// Insert info into database table.. do w.e!
$dbconn = mysql_connect($MySql_hostname, $MySql_username, $MySql_password)or die("Unable to connect to MySQL");
mysql_select_db($MySql_databasename,$dbconn);
@mysql_query("INSERT INTO images (img_name, img_title, thumb_name, uploaded_date) VALUES ('$DestRandImageName', '$Imageitle','$NewThumbFileName','$uploaded_date')");
mysql_close($dbconn);
//respond with our images
echo '<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr><td align="center"><img src="'.$UploadThumbDirectory.$NewThumbFileName.'" alt="Thumbnail"></td></tr><tr>
<td align="center"><img src="'.$UploadDirectory .$DestRandImageName.'" alt="Resized Image"></td></tr></table>';
}else{
die('Upload Error'); //output error
}
}
//function outputs upload error messages, http://www.php.net/manual/en/features.file-upload.errors.php#90522
function upload_errors($err_code) {
switch ($err_code) {
case UPLOAD_ERR_INI_SIZE:
return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
case UPLOAD_ERR_FORM_SIZE:
return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
case UPLOAD_ERR_PARTIAL:
return 'The uploaded file was only partially uploaded';
case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';
case UPLOAD_ERR_NO_TMP_DIR:
return 'Missing a temporary folder';
case UPLOAD_ERR_CANT_WRITE:
return 'Failed to write file to disk';
case UPLOAD_ERR_EXTENSION:
return 'File upload stopped by extension';
default:
return 'Unknown upload error';
}
}
function resizeImage($CurWidth,$CurHeight,$MaxWidth,$MaxHeight,$DestFolder,$SrcImage)
{
$ImageScale = min($MaxWidth/$CurWidth, $MaxHeight/$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))
{
// copy file
if(imagejpeg($NewCanves,$DestFolder,100))
{
imagedestroy($NewCanves);
return true;
}
}
}
?>