1

i am Trying to save images of different categories in a folder and retrieve them..and when i save an image, a watermark is automatically applied on image .. for this i am using.

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

and watermark.php is

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

some images are save in upload folder and temp folder and database also and watermark is applied on them..but now when i am trying to upload a image..its goes only in upload folder and a fatal Error is show:-

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

how can i solve this.. and .PNG image is not saving also

omnath
  • 87
  • 9
  • change the file upload size limit in php.ini – KAsh Feb 28 '13 at 12:05
  • you should modify your php.ini to fix this – Silent Byte Feb 28 '13 at 12:05
  • possible dublicate http://stackoverflow.com/questions/415801/allowed-memory-size-of-33554432-bytes-exhausted-tried-to-allocate-43148176-byte – chriz Feb 28 '13 at 12:07
  • where i found the php.ini in my xampp and what changes i do in cpanel when i upload this project on server. – omnath Feb 28 '13 at 12:15
  • go to your icon tray , click on xampp icon then move to php..there u can see the php.ini option or go to the c:/xampp and in php folder search php.ini – KAsh Feb 28 '13 at 12:17
  • i am using ini_set('memory_limit', '-1'); is it the correct way? and error is not show but i am not able to save png file.what changes i do for this. – omnath Feb 28 '13 at 12:20
  • i am also hosting provider of this site..what changes i will do on cpanel of this website? – omnath Feb 28 '13 at 12:25

2 Answers2

1

That means that current RAM limit isn't enough for your PHP script, that generate watermark.

Go to your php.ini, find "memory_limit" and change that line to something like that:

memory_limit = 256M;

After that, restart your Apache.

Serge Kuharev
  • 1,012
  • 6
  • 15
  • where i found the php.ini in my xampp and what changes i do in cpanel when i upload this project on server. – omnath Feb 28 '13 at 12:16
  • @omnath, create some empty php file, write there "echo phpinfo();". Look for php.ini, you will see the exact location. – Serge Kuharev Feb 28 '13 at 12:18
  • But if you are using shared hosting, it might be a problem to change php configuration. Contact your hosting support about this issue. – Serge Kuharev Feb 28 '13 at 12:19
  • i am using ini_set('memory_limit', '-1'); is it the correct way? and error is not show but i am not able to save png file.what changes i do for this. – omnath Feb 28 '13 at 12:22
  • It's correct until ini_set is allowed in your environment. If error is gone, then it is working. If you can't save png file, maybe you have no permissions for that? Does it work on your local server? – Serge Kuharev Feb 28 '13 at 12:29
  • no,i am not able to save .png file on my local xampp,and i set the memory_limit=256M also. – omnath Feb 28 '13 at 12:33
  • Are you under windows or linux? – Serge Kuharev Feb 28 '13 at 12:34
  • i am using Linux hosting – omnath Feb 28 '13 at 12:37
  • Well, check then if watermark is called at least once. By using echo(), locate place, where script ends. Locate the line of code, that is causing the problem. – Serge Kuharev Feb 28 '13 at 12:41
  • Also, consider that imagejpeg is only working with JPG files, not png. Maybe this is a problem. – Serge Kuharev Feb 28 '13 at 12:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25304/discussion-between-omnath-and-megakuh) – omnath Feb 28 '13 at 17:01
1

Its May be create Problem when Upload in Server.you can Use

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

You need to download the arial.ttf file and upload it on your server. Then create a new PHP file and copy and paste the above function in it. Next 4 lines are used to set the Source file, Watermark text message and Destination file. If you want to just display the watermarked image you need to leave the $DestinationFile variable empty ($DestinationFile=''; ). Also please make sure that for source file and destination file you include the full server path and the image file name. If you want to change the position of the watermark message on your images you can chage that line 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);
?>
omnath
  • 509
  • 6
  • 25