2013-02-28 71 views
1

我想保存的文件夾中不同類別的圖像,並檢索them..and當我保存的圖像保存,水印被自動應用使圖像.. 此我使用。圖像不是在載文件夾

<?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); 
     } 
?> 

一些圖像保存在文件夾上傳和臨時文件夾和數據庫也和水印是在them..but應用現在,當我試圖上傳圖片..its雲只有在上傳文件夾和一個致命錯誤顯示: -

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

變化在php.ini – KAsh 2013-02-28 12:05:54

+0

文件上傳大小限制,你應該修改你的php.ini來解決這個 – 2013-02-28 12:05:57

+0

可能dublicate http://stackoverflow.com/questions/415801/allowed-memory-size-of- 33554432字節枯竭,試圖對分配-43148176字節 – chriz 2013-02-28 12:07:16

回答

1

它可能是創建問題,當上傳在Server.you可以使用

<?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,$黑,$字體,$ 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); 
?> 
1

這意味着,當前內存的限制是不夠的,你的PHP腳本,生成水印。

轉到您的php.ini,找到 「memory_limit的」,改變該行類似的東西:

memory_limit = 256M; 

之後,重新啓動你的Apache。

+0

我在xampp中找到了php.ini,當我在服務器上傳這個項目時,我在cpanel中做了什麼更改。 – omnath 2013-02-28 12:16:41

+0

@omnath,創建一些空的PHP文件,寫在那裏「echo phpinfo();」。尋找php.ini,你會看到確切的位置。 – 2013-02-28 12:18:35

+0

但是,如果您使用的是共享主機,那麼更改php配置可能會有問題。聯繫您的託管支持關於這個問題。 – 2013-02-28 12:19:37

相關問題