我正在尝试将不同类别的图像保存在文件夹中并检索它们。当我保存图像时,水印会自动应用于图像。 为此我正在使用。
<?php
if(isset($_POST['submit'])){
$cat=$_POST['cat'];
$fname= $_FILES["file"]["name"];
"Type: " . $_FILES["file"]["type"] . "<br />";
"Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
"stored in: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
"Stored in: " . "upload/" . $_FILES["file"]["name"];
echo 'image Save Successfully' ;
require_once( "watermark.php" );
$original_directory = "upload/";
$watermarked_images = "temp/";
if ($handle = opendir($original_directory))
{
while (false !== ($file = readdir($handle)))
{
if(!is_file($original_directory.$file))
continue;
if(exif_imagetype($original_directory.$file)==2)
{
watermark($original_directory.$file,"watermark.png",$watermarked_images.$file);
//echo "Done watermarking <b>".$file."</b><br>\n";
}
}
closedir($handle);
}
}
}
?>
和watermark.php是
<?php
function watermark($original_image,$original_watermark,$destination="")
{
$image=imagecreatefromjpeg($original_image);
list($imagewidth,$imageheight)=getimagesize($original_image);
$watermark = imagecreatefrompng($original_watermark);
list($watermarkwidth,$watermarkheight)=getimagesize($original_watermark);
if($watermarkwidth>$imagewidth || $watermarkheight>$imageheight)
{
$water_resize_factor = $imagewidth / $watermarkwidth;
$new_watermarkwidth = $watermarkwidth * $water_resize_factor;
$new_watermarkheight = $watermarkheight * $water_resize_factor;
$new_watermark = imagecreatetruecolor($new_watermarkwidth , $new_watermarkheight);
imagealphablending($new_watermark , false);
imagecopyresampled($new_watermark , $watermark, 0, 0, 0, 0, $new_watermarkwidth, $new_watermarkheight, $watermarkwidth, $watermarkheight);
$watermarkwidth = $new_watermarkwidth;
$watermarkheight = $new_watermarkheight;
$watermark = $new_watermark;
}
$startwidth = ($imagewidth - $watermarkwidth) / 2;
$startheight = ($imageheight - $watermarkheight) / 2;
imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight);
if(!empty($destination))
imagejpeg($image,$destination);
else
imagejpeg($image);
}
?>
一些图像保存在上传文件夹和临时文件夹和数据库中,并且水印应用于它们..但是现在当我尝试上传图像时..仅在上传文件夹中显示并且显示致命错误: -
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 15000 bytes) in D:\xampp\htdocs\looks\admin\watermark.php on line 28
我该怎么解决这个问题.. 和.PNG图像也没有保存
答案 0 :(得分:1)
这意味着当前的RAM限制对于生成水印的PHP脚本是不够的。
转到你的php.ini,找到“memory_limit”并将该行更改为:
memory_limit = 256M;
之后,重新启动Apache。
答案 1 :(得分:1)
在Server中上传时可能会出现问题。您可以使用
<?php
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);
};
?>
您需要下载arial.ttf文件并将其上传到您的服务器上。然后创建一个新的PHP文件,并将上面的函数复制并粘贴到其中。接下来的4行用于设置源文件,水印文本消息和目标文件。如果您只想显示带水印的图像,则需要将$ DestinationFile变量留空($ DestinationFile ='';)。另外,请确保对于源文件和目标文件包含完整服务器路径和映像文件名。如果你想改变你的图像上水印消息的位置,你可以改变该行imagettftext($ image_p,$ font_size,0,10,20,$ black,$ font,$ WaterMarkText);
<?php
$SourceFile = '/home/user/www/images/image1.jpg';
$DestinationFile = '/home/user/www/images/image1-watermark.jpg';
$WaterMarkText = 'Copyright phpJabbers.com';
watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile);
?>