watermerk over image
foutmelding: The image “http://localhost/showpic.php” cannot be displayed because it contains errors.
[script]
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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
$imagesource = @MYSQL_RESULT($result,0,"data");
//get the picture type. It will change according to file extension it may be either gif or jpg
$type = @MYSQL_RESULT($result,0,"mime");
//send the header of the picture we are going to send
Header( "Content-type: $type");
//send the binary data
//Add watermark to image
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (empty($image)) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
echo $imagesource
?>
$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
$imagesource = @MYSQL_RESULT($result,0,"data");
//get the picture type. It will change according to file extension it may be either gif or jpg
$type = @MYSQL_RESULT($result,0,"mime");
//send the header of the picture we are going to send
Header( "Content-type: $type");
//send the binary data
//Add watermark to image
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (empty($image)) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
echo $imagesource
?>
[/script]
haal tijdelijk eens de header weg, en kijk eens welke foutmeldingen je krijgt?
Dit script heb ik later erbij geplakt:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path from the document root - such as subdirectory/image.jpg
$imagesource = $_SERVER['DOCUMENT_ROOT']."/".$_GET['path'];
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (empty($image)) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
// this script creates a watermarked image from an image file - can be a .jpg .gif or .png file
// where watermark.gif is a mostly transparent gif image with the watermark - goes in the same directory as this script
// where this script is named watermark.php
// call this script with an image tag
// <img src="watermark.php?path=imagepath"> where path is a relative path from the document root - such as subdirectory/image.jpg
$imagesource = $_SERVER['DOCUMENT_ROOT']."/".$_GET['path'];
if (!file_exists($imagesource)) die();
$filetype = strtolower(substr($imagesource,strlen($imagesource)-4,4));
if($filetype == ".gif") $image = @imagecreatefromgif($imagesource);
if($filetype == ".jpg") $image = @imagecreatefromjpeg($imagesource);
if($filetype == ".png") $image = @imagecreatefrompng($imagesource);
if (empty($image)) die();
$watermark = @imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
header("Content-type: image/jpeg");
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
Maar ipv <img src="watermark.php?path=imagepath"> dit te gebruiken probeer ik het resultaat van mn query ($imagesource = @MYSQL_RESULT($result,0,"data");) te gebruiken.
Zie mijn vorige reactie.
Open het bestand, en kijk of de eerste regels kloppen. Haal ook de @'s weg.
Ik heb er nu dit van gemaakt:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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");
//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();
$watermark = imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
$type = mysql_result($result,0,"mime");
//send the header of the picture we are going to send
//Header( "Content-type: $type");
//send the binary data
?>
$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");
//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();
$watermark = imagecreatefromgif('watermark.gif');
$imagewidth = imagesx($image);
$imageheight = imagesy($image);
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
$type = mysql_result($result,0,"mime");
//send the header of the picture we are going to send
//Header( "Content-type: $type");
//send the binary data
?>
<html>
<body>
<img src="showpic.php">
</body>
</html>
Maar met deze code krijg ik vervolgens deze erros:
Warning: imagesx(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 23
Warning: imagesy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 24
Warning: imagecopy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 29
Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 30
Warning: imagedestroy(): supplied argument is not a valid Image resource in C:\xampp\htdocs\showpic.php on line 31
Is het mogelijk om op deze manier het watermerk toe te voegen? Of kan ik beter de url van het plaatje opslaan in de database en vandaaruit verder werken?
http://nl.php.net/imagesx, bijv. http://nl.php.net/manual/en/function.imagecreatetruecolor.php
Je geeft een verkeerde resource op, zie ook : Gewijzigd op 14/09/2011 17:33:58 door - Ariën -
Gewijzigd op 14/09/2011 22:54:14 door Daan Dutilh