GD Library Help ... placing an Image ontop of an image?

Discussion in 'PHP' started by jpinheiro, Nov 28, 2009.

  1. #1
    Hello,

    I am using to external images to place one on top of the other but its not working correctly

    This image is to be on top: http://www.everyday-taichi.com/image-files/youtube-play.jpg
    This Image is to be on the bottom: http://img.youtube.com/vi/7Lfyk9yQPYk/default.jpg

    This is what I have so far but its like cutting away at the images: http://JneroCorp.com/test.php

    Here is the code I have:
    <?php
    	$src = imagecreatefromjpeg( 'http://img.youtube.com/vi/7Lfyk9yQPYk/default.jpg' );
    	$dst = imagecreatefromjpeg( 'http://www.everyday-taichi.com/image-files/youtube-play.jpg' );
    	
    	imagecopymerge( $dst, $src, 1, 1, 0, 0, 200, 200, 80 );
    	header( 'Content-type: image/jpeg' );
    	imagejpeg( $dst );
    	imagedestroy( $src );
    	imagedestroy( $dst );
    ?>
    
    Code (markup):
     
    jpinheiro, Nov 28, 2009 IP
  2. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Hi there!
    Try this, it's not fancy, not perfect, but it should do the job, maybe you can atleast use it as a starting point.
    <?php
    /**
     * Just a stupid class made while waiting for my pizza
     */
    class myImageWorker {
    
    	/**
    	 * Creates a watermarked image resource
    	 * 
    	 * @param resource $p_rDest Used as background
    	 * @param resource $p_rSrc Used as watermark
    	 * @param int $p_iDestX  Background X offset
    	 * @param int $p_iDestY Background Y offset
    	 * @param int $p_iSrcX Watermark  X offset
    	 * @param int $p_iSrcY  Watermark  Y offset
    	 * @param int $p_iSrcWidth Width of watermark
    	 * @param int $p_iSrcHeight height  of watermark
    	 * @param int $p_iOpacity OPacity of  of watermark
    	 * @param string $p_sType Imagetype to create, jpeg, gif, png
    	 * @return resource An image resource sent to the browser
    	 */
    	public function makeWaterMarkedImage ($p_rDest, $p_rSrc, $p_iDestX, $p_iDestY, $p_iSrcX, $p_iSrcY, $p_iSrcWidth, $p_iSrcHeight, $p_iOpacity, $p_sType) {
    		
    		// create an image resource to fill with something
    		$rImage = imagecreatetruecolor ($p_iSrcWidth, $p_iSrcHeight);
    		
    		// copy parts of dest and source and merge
    		imagecopy ($rImage, $p_rDest, 0, 0, $p_iDestX, $p_iDestY, $p_iSrcWidth, $p_iSrcHeight);
    		imagecopy ($rImage, $p_rSrc, 0, 0, $p_iSrcX, $p_iSrcY, $p_iSrcWidth, $p_iSrcHeight);
    		imagecopymerge ($p_rDest, $rImage, $p_iDestX, $p_iDestY, $p_iSrcX, $p_iSrcY, $p_iSrcWidth, $p_iSrcHeight, $p_iOpacity);
    		
    		return  $this->outputResource($p_rDest, $p_rSrc, $p_sType);
    	}
    	
    	/**
    	 * outputs the image
    	 * 
    	 * @param resource $p_rDest
    	 * @param resource $p_rSrc
    	 * @param string $p_sType
    	 * @return void
    	 */
    	private function outputResource($p_rDest, $p_rSrc, $p_sType) {
    		header ('Content-type: image/' . $p_sType);
    		switch ($p_sType) {
    			case 'jpeg':
    				imagejpeg($p_rDest);
    			break;
    			case 'png':
    				imagepng($p_rDest);
    			break;
    			case 'gif':
    				imagegif($p_rDest);
    			break;
    		}
    		
    		$this->destroyResource($p_rDest, $p_rSrc);
    	}
    	
    	/**
    	 * Destroys the image resource
    	 * 
    	 * @param resource $p_rDest
    	 * @param resource $p_rSrc
    	 * @return void
    	 */
    	private function destroyResource ($p_rDest, $p_rSrc) {
    		imagedestroy($p_rDest);
    		imagedestroy($p_rSrc);
    	}
    	
    }
    
    // usage
    $oImageWorker = new myImageWorker();
    
    $rDest = imagecreatefromjpeg ('http://img.youtube.com/vi/7Lfyk9yQPYk/default.jpg');
    $rSource = imagecreatefromjpeg ('http://www.everyday-taichi.com/image-files/youtube-play.jpg');
    
    $oImageWorker->makeWaterMarkedImage($rDest, $rSource, 28, 25, 0, 0, 61, 44, 60, 'jpeg'); // you can change jpeg to some other type 
    PHP:
    :)
     
    wing, Nov 28, 2009 IP