Hi, I’m trying to add a watermark to images loaded from a database. The images are loading fine, but when i try to add the watermark, i got the following errors:
Warning: imagesx(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 20
Warning: imagesy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 21
Warning: imagecopy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 34
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\showpic.php:20) in C:\xampp\htdocs\showpic.php on line 37
Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 38
Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 39
I understand I’m using an invalid image resource, but i don’t know how to fix it. Need help!!
This is the script.
<?php
$id = 10;
$mysql_server="localhost";
$mysql_username="root";
$mysql_password="";
$mysql_database="db_image";
//connect to database using above settings
mysql_connect("localhost",$mysql_username,$mysql_password);
mysql_select_db("db_image");
//select the picture using the id
$query = "select data, mime from file where id=$id";
//execute the query
$result = mysql_query($query);
//get the picture data which will be binary
$image = mysql_result($result,0,"data");
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$type = mysql_result($result,0,"mime");
//get the picture type. It will change according to file extension it may be either gif or jpg
//Add watermark to image
if (empty($image)) die();
// Load the watermark image
$watermark = imagecreatefromgif('watermark.gif');
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
//send the header of the picture we are going to send
header( "Content-type: $type");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>