Update of multiple Images

Asked

Viewed 89 times

2

My code is currently doing its role well by uploading and putting the watermark on the upload images, however I would like to be storing the names of all images in the database.

$idnoticia = $_GET['idnoticia'];

$watermark = imagecreatefrompng('watermark.png');
$watermark_x = imagesx($watermark);
$watermark_y = imagesy($watermark);
$dir = "../img/$idnoticia/";

foreach($_FILES['files']['tmp_name'] as $index => $file) {
   $image = imagecreatefromjpeg($file);
   imagecopy($image, $watermark, round(imagesx($image) / 1) - round($watermark_x / 1),    round(imagesy($image) / 1) - round($watermark_y / 1), 0, 0, $watermark_x,    $watermark_y);
   imagejpeg($image, $dir . preg_replace('/.jpeg|.jpg/i', '', $_FILES['files']['name'][$index]) . '_wm.jpg');
   imagedestroy($image);
}

imagedestroy($watermark);
echo "Envio Completado!";

$sqlInsert = 'UPDATE noticias SET galeria = "$image" WHERE idnoticia = "$idnoticia';
$stmt = DB::prepare($sqlInsert2);
$stmt->bindParam("galeria", $image);
$stmt->execute();

I would like to store the image names in the field galeria like (img1.jpg, img2.jpg, img3.jpg), placing a delimiter for when to display facilitate.

It is possible?

1 answer

1

First store the names in an array while running your foreach:

$nomes = array();
foreach($_FILES['files']['tmp_name'] as $index => $file) {
    (...)
    $nomes[] = $_FILES['files']['name'][$index]) . '_wm.jpg';
}

When saving, concatenate using the implode function, in the example having as separator a comma and a space:

http://php.net/manual/en/function.implode.php

$sqlInsert = 'UPDATE noticias SET galeria = "'.implode(', ', $nomes).'"  WHERE idnoticia = "$idnoticia';

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.