How to copy a remote watermark image to folder?

Discussion in 'PHP' started by 123GoToAndPlay, Apr 14, 2009.

  1. #1
    Hi all,

    For copying an image i use this
    
    function copyImg($url) {
    $file=file_get_contents($url);
    
    $local_name=basename($url);
    
    if ($file) {
    
        $handle=fopen('test/'.$local_name, 'a');
    
        //writes to current directory (must be chmod'd to 777)
    
        if (fwrite($handle,$file) === false) {
    
             echo "Cannot write file";
    
        } else {
    
             echo 'File successfully written. thanks!';
    
        }
    
        fclose($handle);
    
    } else {
    
        echo '<a href="'.$url.'">'.$url.'</a> cannot be read';
    
    }
    
    }
    
    PHP:
    but this doesn't work for
    watermark.php?folder=dat&amp;width=300&amp;article=663

    How can i fix this??
     
    123GoToAndPlay, Apr 14, 2009 IP
  2. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    what do you mean? Is watermark.php using GD to generate an image? If so, when you copy just save the file as a .gif or .jpg whatever it is creating the image as.

    Regards,
    Dennis M.
     
    Dennis M., Apr 14, 2009 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi dennis,

    Indeed that is exactly what i meant. Somehow it doesn't save the watermark as an image i get an unexpected format/error. If i browse to watermark.php with the variables it does show a .jpg`.

    Any suggestions?
     
    123GoToAndPlay, Apr 14, 2009 IP
  4. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    think i got it to work by adding
    
    ob_start();
    include($url);
    $file = ob_get_contents();
    ob_get_clean();
    
    PHP:
     
    123GoToAndPlay, Apr 15, 2009 IP
  5. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    AARRRHGG and now it doesn't work?
     
    123GoToAndPlay, Apr 15, 2009 IP
  6. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hmm, i am now trying with a class i found. Which works very fine........except with
    
    function endElement($parser, $tagName) { 
    	
            if (($tagName == "ITEM") or ($tagName == "GENERAL"))  {
    //here save the rss elements
    
    //but now for the image
    $image = new GetImage;
    // just an image URL for testing
    $image->source =  'http://img.dailymail.co.uk/i/pix/2007/12_01/17banksyES_468x606.jpg';
    //give it a name base on merchant name
    			$test = $merchantName.'-'.$this->id;
    			$image->givenName = $test;//doesn't work
    //$image->givenName = "this-one-worked";
    			$image->save_to = 'testfolder/'; // with trailing slash at the end
    			$image->quality = '75';
    			$get = $image->download('gd'); // using GD
    
    //here the other rss elements
    
    			$this->id = "";
                $this->title = "";
    			
                $this->description = ""; 
    			$this->img_large = "";
    			 $this->link = ""; 
                $this->insideitem = false; 
            } 
    
        }
    
    Code (markup):
    The code works when i hardcode the $givenName. But i like it to be a combo of merchant name + id. What happens is the extension isn't being saved.

    When i set $givenName = "testing"; It will save testing.jpg

    Do i need the "" for the class???
     
    123GoToAndPlay, Apr 15, 2009 IP