问题
I am trying to create multiple images while uploading,
I found a solution from This site this is exactly what I want.
But sadly it only creating jpeg thumbnails.
I have tried many variations to turn function for multiple image extention but keep geting errors.
I realy need help.
This main function which is uploading image it call createThumbnails()
from functions file:
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, $fileType, 300, 185);
$thumb3 = createThumbnail($temp_path, $large_thumbnail,$fileType, 550, 340);
$Name = htmlspecialchars($_POST['sname']);
$sql = "INSERT INTO tbl_images(image, sname) VALUES (:image, :sname)";
$stmt =$pdo->prepare($sql);
$stmt->bindParam(":image", $large_thumbnail, PDO::PARAM_STR);
$stmt->bindParam(":sname", $Name, 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);
}
<from action="upload.php">
<input type="file" name="fupload"/>
<button type="submit">submit</button>
</form><from action="upload.php">
<input type="file" name="fupload"/>
<button type="submit">submit</button>
</form>
This is the function creating folders for thumbnails if not exist its working fine:
function createFolder($path)
{
if (!file_exists($path)) {
mkdir($path, 0755, TRUE);
}
}
This is the original function
which is working fine only for jpeg and I want to turn it to support multiple extentions:
function createThumbnail($sourcePath, $targetPath, $file_type, $thumbWidth, $thumbHeight){
$source = imagecreatefromjpeg($sourcePath);
$width = imagesx($source);
$height = imagesy($source);
$tnumbImage = imagecreatetruecolor($thumbWidth, $thumbHeight);
imagecopyresampled($tnumbImage, $source, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
if (imagejpeg($tnumbImage, $targetPath, 90)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
} else {
return FALSE;
}
}
And this is my latest try its resizing and uploading file same as I want, But now I have problem with png background transparent I get png images with black backgorund :
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, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
if(imagegif($tnumbImage, $targetPath, 80)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
}if(imagepng($tnumbImage, $targetPath, 80)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
}if(imagejpeg($tnumbImage, $targetPath, 80)) {
imagedestroy($tnumbImage);
imagedestroy($source);
return TRUE;
} else {
return FALSE;
}
}
But I am having tons of errors and not uploading images thanks to all qho can help.
来源:https://stackoverflow.com/questions/58792156/how-to-create-multiple-thumbnails-while-uploading-image-without-libraries