function upload_image_array()
{
$res = array();
if(isset($_FILES['user_image']))
{
// Count total files
$countfiles = count($_FILES['user_image']['name']);
for($i=0;$i<$countfiles;$i++){
$extension = explode('.', $_FILES['user_image']['name'][$i]);
$new_name = rand() . '.' . $extension[1];
$destination = '../portfolio/upload/' . $new_name;
$thumbDestination = '../portfolio/upload/thumb/' . $new_name;
move_uploaded_file($_FILES['user_image']['tmp_name'][$i], $destination);
resize($destination, $thumbDestination, 300);
$res[] = $new_name;
}
return $res;
}
}
if($_POST["operation"] == "Add")
{
$images = array();
if($_FILES["user_image"]["name"] != '')
{
$images = upload_image_array();
}
foreach ($images as $image){
$statement = $connection->prepare("
INSERT INTO portfolio (image, image_mini)
VALUES (:image, :image_mini)
");
$result = $statement->execute(
array(
':image' => $image,
':image_mini' => $image
)
);
}
if(!empty($result))
{
echo 'Фото Добавлено';
}
}
function upload_image()
{
if(isset($_FILES['user_image']))
{
// Count total files
$countfiles = count($_FILES['user_image']['name']);
for($i=0;$i<$countfiles;$i++){
$extension = explode('.', $_FILES['user_image']['name'][$i]);
$new_name = rand() . '.' . $extension[1];
$destination = '../portfolio/upload/' . $new_name;
$thumbDestination = '../portfolio/upload/thumb/' . $new_name;
move_uploaded_file($_FILES['user_image']['tmp_name'][$i], $destination);
resize($destination, $thumbDestination, 300);
}
return $new_name;
}
}
function resize($source, $destination, $width){
$image = new Imagick($source);
// Получаем соотношение сторон для пропорционального ресайза.
$d = $image->getImageGeometry();
$w = $d['width'];
$h = $d['height'];
$aspectratio = $h / $w;
// Ресайзим и сохраняем.
$image->resizeImage($width, $width*$aspectratio, Imagick::FILTER_LANCZOS, 1);
$image->writeImage($destination);
$image->destroy();
}