3

I want to add watermark on multiple images and display them on browser. Here is my piece of code:

<?php
include('admin/connect.php');
$q = mysql_query("select * from ring");
while($row=mysql_fetch_array($q)){  
    $img = 'admin/'.$row['Image'];
    $stamp = imagecreatefrompng('admin/image/watermark.png');
    $im = imagecreatefromjpeg($img);
    $marge_right= 20;
    $marge_bottom = 50;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);
    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
    header('Content-type: image/png');
    imagepng($im);
    imagedestroy($im);
}
?>

I have used header('Content-type: image/png'); in my code due to which I am not able to display all the images. Here I am fetching images from database and then adding watermark on it but it will be better if anyone of you can help me to add watermark first and then save it in database. Thanks in advance.

Dinesh Patil
  • 615
  • 2
  • 15
  • 37

5 Answers5

0

You can't have multiples headers as header('Content-type: image/png') in one page (you can only have one Content-Type per page) but you can have multiples tags. If you want modify the original image, you don't need make an update in the database, in this case this is the code.

<?php
include('admin/connect.php');
$q = mysql_query("select * from ring");
while($row=mysql_fetch_array($q)){  
    $img = 'admin/'.$row['Image'];
    $stamp = imagecreatefrompng('admin/image/watermark.png');
    $im = imagecreatefromjpeg($img);
    $marge_right= 20;
    $marge_bottom = 50;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);
    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
    imagejpeg($im, 'admin/' . $row['Image']); //This overwrite your original image

    echo "<img src='admin/" . $row['Image'] . "' /><br/>";
    </div>
    }
    ?>

If you want preserve your original image you must save your image watermarked with another name and then update the name in the database, in this case you can do something like this:

<?php
include('admin/connect.php');
$q = mysql_query("select * from ring");
while($row=mysql_fetch_array($q)){  
    $img = 'admin/'.$row['Image'];
    $stamp = imagecreatefrompng('admin/image/watermark.png');
    $im = imagecreatefromjpeg($img);
    $marge_right= 20;
    $marge_bottom = 50;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);
    imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
    imagejpeg($im, 'admin/' . 'wm-' . $row['Image']); //Saving the watermarked with the prefix wm

    echo "<img src='admin/" . 'wm-' . $row['Image'] . "' /><br/>";
    </div>
    }

mysql_query("UPDATE ring SET Image = CONCAT('wm-', Image)"); //Updating all your image with the prefix wm-
    ?>
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

IMO, you should never(almost never) save images to a database. Save the paths to a database and store them locally or on a Cloud solution like Amazon S3

Nihal Sahu
  • 56
  • 7
0

You can not send multiple images in one Request. In my option you have three options.

  1. Concatenate all images to one big image and display the images using a "css map" (Jquery UI icons working this way). But you will have Problems with different dimensions of the single images, and the concatenated image could get really large and so on.

  2. You generate only one image by request and use use the id of the image to identify the image. But this solution is mentioned already.

  3. Use base64 encode images like this:

(To get the image data in a variable you have to use output buffering)

<?php
include('admin/connect.php');
$q = mysql_query("select * from ring");
while ($row = mysql_fetch_array($q)) {
    $img = 'admin/' . $row['Image'];
    $stamp = imagecreatefrompng('admin/image/watermark.png');
    $im = imagecreatefromjpeg($img);
    $marge_right = 20;
    $marge_bottom = 50;
    $sx = imagesx($stamp);
    $sy = imagesy($stamp);
    imagecopy(
        $im,
        $stamp,
        imagesx($im) - $sx - $marge_right,
        imagesy($im) - $sy - $marge_bottom,
        0,
        0,
        imagesx($stamp),
        imagesy($stamp)
    );
    ob_start();
    imagepng($im);
    $image_data = ob_get_clean();
    echo '<img src="data:image/png;base64,' . base64_encode($image_data) . '">';
   imagedestroy($im);
}

And with some js magic you are able to include the html content to your file (like https://stackoverflow.com/a/9003363/3972213)

Community
  • 1
  • 1
skroczek
  • 2,289
  • 2
  • 16
  • 23
0

Use the Imagick for watermarking the picture. You can use a batch process to watermark the images you stored in the database regularly. In that case, use crontab.

zawhtut
  • 8,335
  • 5
  • 52
  • 76
0

I agree with Nihal Sahu that you should never(almost never) save images to a database. Save the paths to a database and store them locally. It will be less time consuming for you to process, as if images are in database then the time consumption is more.

amishmhatre
  • 38
  • 1
  • 4