Created
September 9, 2017 10:56
-
-
Save Sanix-Darker/4972273cad4d65d847c7394c2fd417d7 to your computer and use it in GitHub Desktop.
Resize image to multiple dimensions (PHP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$valid_formats = array("jpg", "png", "gif", "bmp"); | |
$max_file_size = 1024*7500; //100 kb | |
$path = "assets/img/photo/"; // Upload directory | |
$count = 0; | |
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){ | |
foreach ($_FILES['files']['name'] as $f => $name) { | |
if ($_FILES['files']['error'][$f] == 4) { | |
continue; | |
} | |
if ($_FILES['files']['error'][$f] == 0) { | |
if ($_FILES['files']['size'][$f] > $max_file_size) { | |
$message[] = "$name est trop grand!"; | |
continue; | |
} | |
elseif( ! in_array(strtolower(pathinfo($name, PATHINFO_EXTENSION)), $valid_formats) ){ | |
$message[] = "$name n'a pas un bon format!"; | |
continue; | |
} | |
else{ | |
//On fait du renomage | |
$name = $titre.$_FILES['files']['name'][$f]; | |
$_FILES['files']['name'][$f] = $titre.$_FILES['files']['name'][$f]; | |
$idphoto = 0; | |
if($type == "image"){ | |
$size1=getimagesize($_FILES['files']['tmp_name'][$f]); | |
$width=$size1[0]; | |
$height=$size1[1]; | |
$idphoto = $BD->lastInsertId(); | |
//On a les differentes tailles en largeurs; | |
//On a le tableaus des largeurs et les decoupages en descendant des images | |
$nivo_image=0; | |
if($width>=320 AND $width<640){ | |
//Nivo 0 | |
$nivo_image=1; | |
}elseif($width>=640 AND $width<800){ | |
//nivo1 | |
$nivo_image=2; | |
}elseif($width>=800 AND $width<1024){ | |
//nivo2 | |
$nivo_image=3; | |
}elseif($width>=1024 AND $width<1280){ | |
//nivo3 | |
$nivo_image=4; | |
}elseif($width>=1280 AND $width<1536){ | |
//nivo4 | |
$nivo_image=5; | |
}elseif($width>=1536 AND $width<1600){ | |
//nivo5 | |
$nivo_image=6; | |
}elseif($width>=1600 AND $width<2048){ | |
//nivo6 | |
$nivo_image=7; | |
}elseif($width>=2048 AND $width<2240){ | |
//nivo7 | |
$nivo_image=8; | |
}elseif($width>=2240 AND $width<3032){ | |
//nivo8 | |
$nivo_image=9; | |
}elseif($width>=3032 AND $width<3072){ | |
//nivo9 | |
$nivo_image=10; | |
}elseif($width>=3072 AND $width<3264){ | |
//nivo10 | |
$nivo_image=11; | |
} | |
$i=0; | |
$pourcent = 100; | |
while($i<5){ | |
if($nivo_image==1){ | |
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name); | |
break; | |
}else if($nivo_image>1){ | |
$nivo_image--; | |
} | |
switch ($nivo_image) { | |
case 11: | |
$pourcent = (3072*100)/$size1[0]; | |
break; | |
case 10: | |
$pourcent = (3032*100)/$size1[0]; | |
break; | |
case 9: | |
$pourcent = (2240*100)/$size1[0]; | |
break; | |
case 8: | |
$pourcent = (2048*100)/$size1[0]; | |
break; | |
case 7: | |
$pourcent = (1600*100)/$size1[0]; | |
break; | |
case 6: | |
$pourcent = (1536*100)/$size1[0]; | |
break; | |
case 5: | |
$pourcent = (1280*100)/$size1[0]; | |
break; | |
case 4: | |
$pourcent = (1024*100)/$size1[0]; | |
break; | |
case 3: | |
$pourcent = (800*100)/$size1[0]; | |
break; | |
case 2: | |
$pourcent = (640*100)/$size1[0]; | |
break; | |
case 1: | |
$pourcent = (320*100)/$size1[0]; | |
break; | |
default: | |
$pourcent = (320*100)/$size1[0]; | |
break; | |
} | |
$larg1=$size1[0]*$pourcent/100; | |
$long1=$size1[1]*$pourcent/100; | |
switch ($_FILES['files']['type'][$f]) { | |
case 'image/jpeg': | |
$files=imagecreatefromjpeg($_FILES['files']['tmp_name'][$f]); | |
$img_dest1=imagecreatetruecolor($larg1,$long1); | |
$ici = imagecopyresampled($img_dest1,$files,0,0,0,0,$larg1,$long1,$size1[0],$size1[1]); | |
if($ecrire){ | |
$jpg_image = imagecreatefromjpeg($_FILES['files']['tmp_name'][$f]); | |
$white = imagecolorallocate($jpg_image, 255, 255, 255); | |
imagettftext($jpg_image, 25, 0, 75, 300, $white, $path, 'Shooter'); | |
} | |
//imagejpeg($img_dest1); | |
imagejpeg($img_dest1,$path.$i.$_FILES['files']['name'][$f]); | |
break; | |
case 'image/png': | |
$files=imagecreatefrompng($_FILES['files']['tmp_name'][$f]); | |
$img_dest1=imagecreatetruecolor($larg1,$long1); | |
$ici = imagecopyresampled($img_dest1,$files,0,0,0,0,$larg1,$long1,$size1[0],$size1[1]); | |
if($ecrire){ | |
$jpg_image = imagecreatefrompng($_FILES['files']['name'][$f]); | |
$white = imagecolorallocate($jpg_image, 255, 255, 255); | |
imagettftext($jpg_image, 25, 0, 75, 300, $white, $path, 'Shooter'); | |
} | |
//imagepng($img_dest1); | |
imagepng($img_dest1,$path.$i.$_FILES['files']['name'][$f]); | |
break; | |
case 'image/gif': | |
$files=imagecreatefromjgif($_FILES['files']['tmp_name'][$f]); | |
$img_dest1=imagecreatetruecolor($larg1,$long1); | |
$ici = imagecopyresampled($img_dest1,$files,0,0,0,0,$larg1,$long1,$size1[0],$size1[1]); | |
if($ecrire){ | |
$jpg_image = imagecreatefromgif($_FILES['files']['name'][$f]); | |
$white = imagecolorallocate($jpg_image, 255, 255, 255); | |
imagettftext($jpg_image, 25, 0, 75, 300, $white, $path, 'Shooter'); | |
} | |
//imagegif($img_dest1); | |
imagegif($img_dest1,$path.$i.$_FILES['files']['name'][$f]); | |
break; | |
default: | |
$files=imagecreatefromjpeg($_FILES['files']['tmp_name'][$f]); | |
$img_dest1=imagecreatetruecolor($larg1,$long1); | |
$ici = imagecopyresampled($img_dest1,$files,0,0,0,0,$larg1,$long1,$size1[0],$size1[1]); | |
if($ecrire){ | |
$jpg_image = imagecreatefromjpeg($_FILES['files']['name'][$f]); | |
$white = imagecolorallocate($jpg_image, 255, 255, 255); | |
imagettftext($jpg_image, 25, 0, 75, 300, $white, $path, 'Shooter'); | |
} | |
//imagejpeg($img_dest1); | |
imagejpeg($img_dest1,$path.$i.$_FILES['files']['name'][$f]); | |
break; | |
} | |
$i++; | |
} | |
imagedestroy($img_dest1); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!