首先,我在google上搜索并阅读了所有所有帖子,但大多数都是使用类的。
我需要裁剪(调整大小)图像,然后再上传到服务器,而不是创建缩略图, 我有一个从重命名和上传的图片创建缩略图的功能, 效果很好。
但是我想在没有任何类别的情况下上传之前调整原始图像的大小 我不想共享全部代码。.lol
函数generatenewstring();
正在为新名称创建随机令牌。
这是我的文件上传代码,它通过示例插入到mysql中上传原始图像:
if(isset($_POST['btnSubmit']) && isset($_FILES['fupload'])) {
$Name = htmlspecialchars($_POST['sname']);
if(preg_match('/[.](jpg)|(gif)|(png)$/',$_FILES['fupload']['name'])){
$filename = generatenewstring(10).$_FILES['fupload']['name'];
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory . $filename;
move_uploaded_file($source,$target);
$sql = "INSERT INTO tbl_images(image,sname) VALUES (:image,:sname)";
$stmt =$pdo->prepare($sql);
$stmt->bindParam(":image",$filename,PDO::PARAM_STR);
$stmt->bindParam(":sname",$Name,PDO::PARAM_STR);
if($stmt->execute()){
echo "All successfully.";
} else {
echo "All Not Success.";
}
}
}
从表单发布图像值到upload.php的变量:
<from action="upload.php">
<input type="file" name="fupload"/>
<button type="submit">submit</button>
</form>
这是upload.php
require_once('function.php');
if(isset($_POST['btnSubmit']) && isset($_FILES['fupload'])) {
$output['status'] = FALSE;
set_time_limit(0);
$allowedImageType = array("image/gif","image/jpeg","image/pjpeg","image/png","image/x-png" );
if ($_FILES['fupload']["error"] > 0) {
$output['error']= "File Error";
}
elseif (!in_array($_FILES['fupload']["type"],$allowedImageType)) {
$output['error']= "Invalid image format";
}
elseif (round($_FILES['fupload']["size"] / 1024) > 4096) {
$output['error']= "Maximum file upload size is exceeded";
} else {
$temp_path = $_FILES['fupload']['tmp_name'];
$selam = $_FILES['fupload']['name'];
$file = pathinfo($_FILES['fupload']['name']);
$fileType = $file["extension"];
$fileName = rand(222,888) . $selam;
$small_thumbnail_path = "uploads/small/";
createFolder($small_thumbnail_path);
$small_thumbnail = $small_thumbnail_path . $fileName;
$medium_thumbnail_path = "uploads/medium/";
createFolder($medium_thumbnail_path);
$medium_thumbnail = $medium_thumbnail_path . $fileName;
$large_thumbnail_path = "uploads/large/";
createFolder($large_thumbnail_path);
$large_thumbnail = $large_thumbnail_path . $fileName;
$thumb1 = createThumbnail($temp_path,$small_thumbnail,$fileType,150,93);
$thumb2 = createThumbnail($temp_path,$medium_thumbnail,300,185);
$thumb3 = createThumbnail($temp_path,$large_thumbnail,550,340);
$Name = htmlspecialchars($_POST['sname']);
$sql = "INSERT INTO tbl_images(image,:sname)";
$stmt =$pdo->prepare($sql);
$stmt->bindParam(":image",PDO::PARAM_STR);
$stmt->bindParam(":sname",PDO::PARAM_STR);
if($stmt->execute()){
echo "All successfully.";
} else {
echo "All Not Success.";
}
if($thumb1 && $thumb2 && $thumb3) {
$output['status']=TRUE;
$output['small']= $small_thumbnail;
$output['medium']= $medium_thumbnail;
$output['large']= $large_thumbnail;
}
}
echo json_encode($output);
}
这是upload.php的精度为createThumbnails();
我的缩略图功能:
function createFolder($path)
{
if (!file_exists($path)) {
mkdir($path,0755,TRUE);
}
}
function createThumbnail($sourcePath,$targetPath,$file_type,$thumbWidth,$thumbHeight){
switch (strtoupper($file_type)):
case 'jpg':
$source = imagecreatefromjpeg($sourcePath);
break;
case 'JPG':
$source = imagecreatefromjpeg($sourcePath);
break;
case 'png':
$source = imagecreatefrompng($sourcePath);
break;
case 'PNG':
$source = imagecreatefrompng($sourcePath);
break;
case 'gif':
$source = imagecreatefromgif($sourcePath);
break;
case 'GIF':
$source = imagecreatefromgif($sourcePath);
break;
default:
echo "Invalid File Type";
endswitch;
$width = imagesx($source);
$height = imagesy($source);
$tnumbImage = imagecreatetruecolor($thumbWidth,$thumbHeight);
imagecopyresampled($tnumbImage,$source,$thumbHeight,$width,$height);
if(imagegif($tnumbImage,80)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
}if(imagepng($tnumbImage,80)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
}if(imagejpeg($tnumbImage,80)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
} else {
return FALSE;
}
}
我尝试了几种变体,但无法使其正常工作。
最后我尝试了PHP.NET中的image();
他们没有工作,或者我无法使它们工作。
感谢您的帮助