简体   繁体   English

运行php脚本在图像上添加水印时发生内部服务器错误

[英]Internal server error when running php script to add watermark on images

Am developing new photography website in which images are uploaded frequently.... Now I want to add watermark on uploaded images before it goes to client side, I have used the following code for watermark 正在开发一个新的摄影网站,在该网站上经常要上传图像。...现在,我想在上传的图像添加到客户端之前在其上添加水印,我已将以下代码用于水印

$SourceFile = "sys\img\image1.jpg";
$DestinationFile = "sys\img\image1-wm.jpg"; 
$WaterMarkText = 'Copyright sys.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);

function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) { 
 list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height); 
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = 'arial.ttf';
$font_size = 10; 
imagettftext($image_p, $font_size, 0, 10, 20, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
   imagejpeg ($image_p, $DestinationFile, 100); 
} else {
   header('Content-Type: image/jpeg');
  imagejpeg($image_p, null, 100);
};
imagedestroy($image); 
imagedestroy($image_p); 
};

But it showing the following error 但是它显示以下错误

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your 
request.
Please contact the server administrator to inform of the time the error occurred and of anything
you might have done that may have caused the error.

More information about this error may be available in the server error log.

What is the mistake I have done...? 我犯了什么错误...?

Finally I found an another way to add watermark to my images... 终于,我找到了另一种在图像上添加水印的方法。

Here is the code for watermark.... 这是水印的代码。

$filename=$_REQUEST['filename'];
$imgpath="img/"; // Folder of your images in the server
$imgpath = $imgpath.$filename;
header('content-type: image/jpeg'); //HTTP header - assumes your images in the gallery are JPGs
$watermarkfile="img/watermark.png";
$watermark = imagecreatefrompng($watermarkfile);
list($watermark_width,$watermark_height) = getimagesize($watermarkfile);
$image = imagecreatefromjpeg($imgpath);
$size = getimagesize($imgpath);
$dest_x = $size[0] - $watermark_width - 15;
$dest_y = $size[1] - $watermark_height - 15;
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);

Save this code as watermark.php and then use it in the img tag as follows... 将此代码另存为watermark.php,然后按如下所示在img标签中使用它...

<img src="watermark.php?filename=image1.jpg">

Thats it... Working fine... 就是这样...工作正常...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM