SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error with watermarking

    Hi,

    I use the following script to watermark some images. It works just fine, however today I've come accross a couple of images that just wouldnt work. They are both .jpg images of around 350k in size.

    Can anyone see a particular reason for this? I know that it's something to do with the watermarking that causes the file to not save as if I comment out the watermark line of code then the image saves to the server just fine.

    Cheers!

    Uploader.php
    PHP Code:
    <?php

    include_once("../dbconnect.php");
    include_once(
    "../config.php");

    function 
    random()
    {
        
    $r array_merge(range('A''Z'), range('a''z'));
        
    $tmp = array();
        for (
    $i 0$i 5$i++)
        {
            
    $tmp[] = $r[array_rand($r)];
            
    $tmp[] = mt_rand(09);
        }
        
    shuffle($tmp);
        return 
    join($tmp);
    }
    $x random();
    $t date('YmdHis');
    $t $site_img_name."-".$t;
    $t $t.'-'.$x;
    if (((
    $_FILES["uploadedfile"]["type"] == "image/gif")
    || (
    $_FILES["uploadedfile"]["type"] == "image/jpeg")
    || (
    $_FILES["uploadedfile"]["type"] == "image/png")
    || (
    $_FILES["uploadedfile"]["type"] == "image/pjpeg"))
    && (
    $_FILES["uploadedfile"]["size"] <= 10000000))
        {
              
    $img basename($_FILES['uploadedfile']['name']);
             
    $filename "$tstrstr($img".");
             
    $img "pics/$tstrstr($img".");
              
              if (
    is_uploaded_file($_FILES['uploadedfile']['tmp_name']))
              {
                
    //good
                
    $image_location $_FILES['uploadedfile']['tmp_name'];

                
    // Locate the watermark file wherever you choose (remember PNG format)
                
    $watermark_location '../watermarking/watermark.png';

                
    // Location where you want to save the created watermarked file
                
    $save_watermarked_file_to $_FILES['uploadedfile']['tmp_name'];

                
    // Include the watermarking function file
                
    require_once('../watermarking/watermark.php');

                
    // Watermark the image and save it to file
                
    watermark($_FILES['uploadedfile']['tmp_name'], $watermark_location$save_watermarked_file_to);
        
                if (
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $img))
                {
      
                    
    //worked! process any database stuff here. use the $img variable to store the file location (including images/) in the db
                    
    $result mysql_query("INSERT INTO images (image_name, image_url) VALUES ('$filename','$img')");
                    
    $image_id mysql_insert_id(); //get the image id
                    
                    //site URL
                    
    if($localOrLive == 'local')
                    {
                        
    header("Location: $pathToSite/image.php?id=$image_id");
                    }
                    else
                    {
                        
    header("Location: $domainName/image.php?id=$image_id");
                    }
                    
                    
                    
                }
                   else
                {
                       
    //echo "There was an error uploading the file, please try again!";

                
    }

            }
            else
            {
                
    //invalid file type error
            
    }
        }
        else
        {
            
    //security (is_uploaded_file) error
        
    }
    ?>
    watermark.php

    PHP Code:
    <?php

    function watermark($SourceFile$WatermarkFile$SaveToFile NULL)
    {
        
        print(
    'hi');
        
    $watermark = @imagecreatefrompng($WatermarkFile)
        or exit(
    'Cannot open the watermark file.');
        
    imageAlphaBlending($watermarkfalse);
        
    imageSaveAlpha($watermarktrue);
        
    $image_string = @file_get_contents($SourceFile)
        or exit(
    'Cannot open image file.');
        
    $image = @imagecreatefromstring($image_string)
        or exit(
    'Not a valid image format.');
        
    $imageWidth=imageSX($image);
        
    $imageHeight=imageSY($image);
        
    $watermarkWidth=imageSX($watermark);
        
    $watermarkHeight=imageSY($watermark);
        
    $coordinate_X = ( $imageWidth 5) - ( $watermarkWidth);
        
    $coordinate_Y = ( $imageHeight 5) - ( $watermarkHeight);
        
    imagecopy($image$watermark$coordinate_X$coordinate_Y00$watermarkWidth$watermarkHeight);
        if(!(
    $SaveToFile)) header('Content-Type: image/jpeg');
        
    imagejpeg ($image$SaveToFile100);
        
    imagedestroy($image);
        
    imagedestroy($watermark);
        if(!(
    $SaveToFile)) exit;
        
    }
    ?>

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    Remove all occurances of the error suppression operator @.

    You probably run out of memory.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I switched on error reporting but don't get anything at all, it just doesn't upload the image or redirect to the page containing the image.

    Commenting out the line below then the image uploads correctly so PHP itself seems happy enough...

    PHP Code:
    watermark($_FILES['uploadedfile']['tmp_name'], $watermark_location$save_watermarked_file_to); 
    FWIW, I also looked in my PHP.ini file and found the line 'memory_limit = 8M'. Is there somewhere else I should also be changing the memory limit?

    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •