$array = [1,2,3];
$in = str_repeat('?,', count($array) - 1) . '?';
$sql = "SELECT * FROM `tovar` WHERE `id` = IN ($in)";
$stmt = $pdo->prepare($sql);
$stmt->execute($array);
$data = $stmt->fetchAll(PDO::FETCH_UNIQUE);
// Смысл кода - вставить в IN столько знаков вопроса сколько элементов в массиве, потом заменяем их значениями из $array подставляя его в execute().<?php
function upload_image()
{
if(isset($_FILES['user_image']))
{
// Count total files
$countfiles = count($_FILES['user_image']['name']);
$bigImages = []; // Создаем переменную-массив, туда будем складывать большие картинки.
$thumbImages = []; // Тоже самое по превьюшкам.
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;
$bigImages = $destination; // Добавляем в массив имя картинки (заполняем массив).
$thumbImages = $thumbDestination; // // Добавляем в массив имя превьюшки (заполняем массив).
move_uploaded_file($_FILES['user_image']['tmp_name'][$i], $destination);
resize($destination, $thumbDestination, 300);
}
// А вот здесь "снаружи" цикла for пишем скрипт вставки в БД.
$in = str_repeat('?,', count($bigImages) - 1) . '?';
$sql = "INSERT INTO `images` (image_path) VALUES ($in)";
$stmt = $pdo->prepare($sql);
$stmt->execute($bigImages);
$in2 = str_repeat('?,', count($thumbImages) - 1) . '?';
$sql = "INSERT INTO `small_images` (image_path) VALUES ($in2)";
$stmt = $pdo->prepare($sql);
$stmt->execute($thumbImages);
return $new_name;
}
};
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();
}
<?php
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();
}
resize('magic.jpg', 'magic_new.jpg', 800);
imagejpeg($thumb, $_FILES['user_image']['tmp_name']);
return $_FILES['user_image']['tmp_name'];