#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Question A little help to imagecopy() / imagecopymerge()


    Hey guys.

    I have this script, that almost works as I want to. It resizes pictures and adds a logo (watermark) to them.

    The problem is, that the logo is a PNG with a transparent background - and it has to work with the original image as a PNG with transparent background too.

    I read that imagecopy() can't take two PNGs with alpha channels or something (transparency I guess). So I think that's not a possibility.

    Therefore I found imagecopymerge() - and it works, but it requires the logo to be PNG 8, and that's not good quality.

    --- So can anybody help me with a solution for this? You have to look at the end of the script at addLogo()

    PHP Code:
    class SimpleImage {
     
       var 
    $image;
       var 
    $image_type;
     
       function 
    load($filename) {
     
          
    $image_info getimagesize($filename);
          
    $this->image_type $image_info[2];
          if( 
    $this->image_type == IMAGETYPE_JPEG ) {
     
             
    $this->image imagecreatefromjpeg($filename);
          } elseif( 
    $this->image_type == IMAGETYPE_GIF ) {
     
             
    $this->image imagecreatefromgif($filename);
          } elseif( 
    $this->image_type == IMAGETYPE_PNG ) {
     
             
    $this->image imagecreatefrompng($filename);
          }
       }
       
       function 
    save($filename$image_type=IMAGETYPE_JPEG$compression=75$permissions=null) {
     
          if( 
    $image_type == IMAGETYPE_JPEG ) {
             
    imagejpeg($this->image,$filename,$compression);
          } elseif( 
    $image_type == IMAGETYPE_GIF ) {
               
    imagealphablending($this->imagefalse );
              
    imagesavealpha($this->imagetrue );
             
    imagegif($this->image,$filename);
          } elseif( 
    $image_type == IMAGETYPE_PNG ) {
             
    imagealphablending($this->imagefalse );
              
    imagesavealpha($this->imagetrue );
             
    imagepng($this->image,$filename);
          }
          if( 
    $permissions != null) {
     
             
    chmod($filename,$permissions);
          }
       }
       function 
    output($image_type=IMAGETYPE_JPEG) {
     
          if( 
    $image_type == IMAGETYPE_JPEG ) {
             
    imagejpeg($this->image);
          } elseif( 
    $image_type == IMAGETYPE_GIF ) {
             
    imagegif($this->image);
          } elseif( 
    $image_type == IMAGETYPE_PNG ) {
             
    imagepng($this->image);
          }
       }
       function 
    getWidth() {
     
          return 
    imagesx($this->image);
       }
       function 
    getHeight() {
     
          return 
    imagesy($this->image);
       }
       function 
    resizeToHeight($height) {
     
          
    $ratio $height $this->getHeight();
          
    $width $this->getWidth() * $ratio;
          
    $this->resize($width,$height);
       }
     
       function 
    resizeToWidth($width) {
          
    $ratio $width $this->getWidth();
          
    $height $this->getheight() * $ratio;
          
    $this->resize($width,$height);
       }
     
       function 
    scale($scale) {
          
    $width $this->getWidth() * $scale/100;
          
    $height $this->getheight() * $scale/100;
          
    $this->resize($width,$height);
       }
     
       function 
    resize($width,$height) {
          
    $new_image imagecreatetruecolor($width$height);
          
    imagealphablending($new_imagefalse );
          
    imagesavealpha($new_imagetrue );
          
    imagecopyresampled($new_image$this->image0000$width$height$this->getWidth(), $this->getHeight());
          
    $this->image $new_image;
       }      
       
       function 
    addLogo($x_cord$y_cord) {
           
    $overlayImg imagecreatefrompng("./img/overlayIMG8-2.png"); // SKAL VÆRE PNG-8
           
    imagealphablending($overlayImgfalse );
           
    imagesavealpha($overlayImgtrue );
           
    imagecopymerge($this->image$overlayImg$x_cord$y_cord0010027100);
       }
     

    Thank you!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,469
    Rep Power
    594
    Why not use a CSS and make your watermark the background of a class? Something like:
    Code:
    .watermark {
       background: url(path/to/watermark.png) no-repeat;
    }
    Code:
    <div class="watermark">
       <img src=...>
    </div>
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    It has to be a part of the images :-)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,469
    Rep Power
    594
    That means your objective is not to just render the image in a browser but to create a new image to store somewhere. PHP is probably not the best tool for that. Perl provides better and easier image management tools.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Exactly. The whole site is built in PHP. And this is so close to be perfect, so I believe it's possible.

IMN logo majestic logo threadwatch logo seochat tools logo