Mam o to skrypt uploadu zdjęć który ma mozliwość zmniejszania zdjęć ale zmniejsza je o ileś procent no tyle ile ustalimy a chcialbym aby zmniejszał do danej wielkości czy mgłby mnie ktos nakierowac jak to mogę zrobic ?
<?php
# Variable
$new_height = 0.5; // new height(1 is 100%, 0.1 is 10%)
$new_width = 0.5; // new width
$quality = 50; // quality(100 = 100%)
$folder = 'zdjecia/'; // folder with images
# Functions
//function give new random name for file
function unid(){
while(substr($result, 0
, 1
) == 0
){ $vowels = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "o", "u", "A", "E", "I", "O", "U"); $result = substr($onlyconsonants, 0
, 7
); }
return $result;
}
function LoadJpeg($imgname, $new_height, $new_width)
{
/* Attempt to open */
$img = @imagecreatefromjpeg($imgname);
/* See if it failed */
if(!$img)
{
/* Create a black image */
$img_mini = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($img_mini, 255, 255, 255);
$tc = imagecolorallocate($img_mini, 0, 0, 0);
imagefilledrectangle($img_mini, 0, 0, 150, 30, $bgc);
/* Output an error message */
imagestring($img_mini, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}
else{
$width = imagesx($img);
$height = imagesy($img);
$width_mini = $width * $new_width;
$height_mini = $height * $new_height;
$img_mini = imagecreatetruecolor($width_mini, $height_mini);
Imagecopyresampled($img_mini, $img, 0, 0, 0, 0, $width_mini , $height_mini, $width , $height);
}
return $img_mini;
}
# MAIN
if($_FILES['photo']){
# uploading file to server
echo "Plik ". $_FILES['photo']['name'] ." zaladowany Poprawnie.\n"; } else {
echo "Upload Niewykonany Error: "; echo "Plik '". $_FILES['photo']['tmp_name'] . "'."; }
# resizing image and saving new file on server
$new_name = $folder.unid().'.jpg';
$new_name = $folder.unid().'.jpg';
}
$img = LoadJpeg($folder.$_FILES['photo']['name'], $new_height, $new_width);
imagejpeg($img, $new_name, $quality);
# deleting old file
imagedestroy($img);
unlink($folder.$_FILES['photo']['name']);
}
else{
?>
Wgraj zdjecia:
<form action="index.php?admin=upload" method="post" enctype="multipart/form-data">
<input type="file" name="photo" />
<input type="submit" value="wgraj" />
</form>
<?php
}
?>