1

我正在尝试将不同类别的图像保存在一个文件夹中并检索它们..当我保存图像时,水印会自动应用于图像..为此我正在使用。

  <?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图像也没有保存

4

2 回答 2

1

这意味着当前的 RAM 限制对于生成水印的 PHP 脚本来说是不够的。

转到您的 php.ini,找到“memory_limit”并将该行更改为类似内容:

memory_limit = 256M;

之后,重新启动您的 Apache。

于 2013-02-28T12:06:25.203 回答
1

在服务器中上传时可能会产生问题。您可以使用

    <?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);
?>
于 2013-02-28T17:08:11.870 回答