Я загружаю несколько изображений, но у меня возникает одна проблема. Я получаю имя папки в URL вместо имени нового изображения. Я ожидаю получить 888539185.jpg, 1644594769.png.
Не могли бы вы помочь мне с этой проблемой?
Логика работы нескольких слайдеров:
$gallery_images = $_FILES['slider'];
$file_post = $_FILES['slider'];
if($file_post['name'][0]!=''){
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
$location='assets/images/uploads/sliders/';
if(!$r_value =resizeImage($file_ary[$i], $location, 800)){
$status = 0;
}else{
$gallery_images[] = $r_value;
}
}
}
echo "<pre>";
print_r($gallery_images);
$allsliders = implode(",",$gallery_images);
Я загрузил только два изображения и получил $allsliders вывод:
[slider] => Array,Array,Array,Array,Array,assets/images/uploads/sliders/888539185.jpg,assets/images/uploads/sliders/1644594769.png
Я же ожидаю вот такого вывода слайдера [slider] =>888539185.jpg, 1644594769.png
$gallery_images вывод:
Array (
[name] => Array (
[0] => 871430578.jpg
[1] => download1.png
)
[type] => Array (
[0] => image/jpeg
[1] => image/png
)
[tmp_name] => Array (
[0] => /opt/lampp/temp/php0Giji9
[1] => /opt/lampp/temp/phpNC4udL
)
[error] => Array (
[0] => 0
[1] => 0
)
[size] => Array (
[0] => 344471
[1] => 2806
)
[0] => assets/images/uploads/sliders/888539185.jpg
[1] => assets/images/uploads/sliders/1644594769.png
)
Код изменения размера изображения:
function imageResize($imageSrc,$imageWidth,$imageHeight, $newWidth, $newHeight) {
$newImageWidth =$newWidth;
$newImageHeight =$newHeight;
$newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);
imageAlphaBlending($newImageLayer, false);
imageSaveAlpha($newImageLayer, true);
// $color = imagecolorallocate($newImageLayer$newImageLayer, 255, 255, 255);
// заполнить изображение
//imagefill($newImageLayer, 0, 0, $color);
imagecopyresampled($newImageLayer,$imageSrc,0,0,0,0,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);
return $newImageLayer;
}
function resizeImage($file, $path, $width){
if($file['name'] != ""){
$uploadedFile = $file['tmp_name'];
$sourceProperties = getimagesize($uploadedFile);
//$newFileName = time().$file['name'];
$filename = basename($file['name']);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$new1 = mt_rand();// случайный номер
$new = $new1.'.'.$extension;
$dirPath = $path;
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$imageType = $sourceProperties[2];
$height = number_format(($sourceProperties[1] / $sourceProperties[0]) * $width, 0, ',', '');
switch ($imageType) {
case IMAGETYPE_PNG:
$imageSrc = imagecreatefrompng($uploadedFile);
$tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1], $width, $height);
$new_one = imagepng($tmp,$dirPath. $new);
break;
case IMAGETYPE_JPEG:
$imageSrc = imagecreatefromjpeg($uploadedFile);
$tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1], $width, $height);
$new_one = imagejpeg($tmp,$dirPath. $new);
break;
case IMAGETYPE_GIF:
$imageSrc = imagecreatefromgif($uploadedFile);
$tmp = imageResize($imageSrc,$sourceProperties[0],$sourceProperties[1], $width, $height);
$new_one = imagegif($tmp,$dirPath. $new);
break;
default:
echo "Неизвестный формат изображения.";
exit;
break;
}
if($new_one == 1){
return $dirPath. $new;
}else{
return 0;
}
}
}
Ответ 1
Вам нужно изменить имя переменной
if(!$r_value =resizeImage($file_ary[$i], $location, 800)){
$status = 0;
}else{
$result_gallery_images[] = $r_value;
}
Перед изменением необходимо установить ее как пустой массив
$result_gallery_images= []
Перед добавлением цикла for выполнить следующее:
$gallery_images = $_FILES['slider'];
$file_post = $_FILES['slider'];
$result_gallery_images= [];
if($file_post['name'][0]!=''){
$file_ary = array();
$file_count = count($file_post['name']);
$file_keys = array_keys($file_post);
for ($i=0; $i<$file_count; $i++) {
foreach ($file_keys as $key) {
$file_ary[$i][$key] = $file_post[$key][$i];
}
$location='assets/images/uploads/sliders/';
if(!$r_value =resizeImage($file_ary[$i], $location, 800)){
$status = 0;
}else{
$result_gallery_images[] = $r_value;
}
}
}
echo "<pre>";
print_r($result_gallery_images);
$allsliders = implode(",",$result_gallery_images);
Ответ 2
Итак, во-первых надо удалить $dirPath .$new из своей функции изменения размера изображения. Во-вторых, вам нужно удалить все массивы элементов из $gallery_images, кроме последних, которые вы помещаете в массив с помощью unset() функции.
Итак, перед отображением переменной $gallery_images:unset($gallery_images['name'])
unset($gallery_images['size])
unset($gallery_images['error'])
unset($gallery_images['tmp_name'])
unset($gallery_images['type'])И после этого ошибка будет решена.
Web