note 110130 deleted from function.imagecopymerge by googleguy

From: Date: Sat, 22 Sep 2012 23:47:35 +0000
Subject: note 110130 deleted from function.imagecopymerge by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190965@lists.php.net to get a copy of this message
Note Submitter: mikech at livinginthepast-audioweb dot co dot uk ---- (Part #2, watermarking 'X' number of images, by php replacing jpg files [see also part #1]) /images/overlay.php ~ <?php /** * This module does the watermarking for images. The image file (PHP with image file type extension) * must define '$uri' then include this as a require_once. To make the image executable as PHP its * file name must be explicitly declared in .htaccess to be handled as PHP. */ Header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0'); Header('Expires: Sun, 1 Jan 2012 00:00:01 GMT'); Header('Pragma: no-cache'); if ( ! defined( 'DIR_ROOT' ) ) { define('DIR_ROOT', str_replace( "\\", "/", dirname( __FILE__ ) ) . '/' ); } // ^ Includes images subpath as well if in images folder. if ( ! defined( 'FILE_LOCATION' ) ) { define('FILE_LOCATION', DIR_ROOT . 'source/'); } if ( ! defined( 'WATERMARK_FILE_LOCATION' ) ) { define('WATERMARK_FILE_LOCATION', DIR_ROOT . 'watermark.png'); } // Isolate file name only if images subpath included: $pos = strripos($uri, '/'); if ($pos !== false) $uri = substr($uri, $pos+1); $test = is_file(FILE_LOCATION . $uri); if ($test===false) throw new Exception('File not found: ' . $uri); $sourcefile = FILE_LOCATION . $uri; // Get extension/image type: $fileType = strtolower(substr($sourcefile, -3)); switch($fileType) { case('gif'): $sourcefile_id = imagecreatefromgif($sourcefile); break; case('png'): $sourcefile_id = imagecreatefrompng($sourcefile); break; case('jpg'): $sourcefile_id = imagecreatefromjpeg($sourcefile); break; default: throw new Exception('Invalid file type (not gif/png/jpg): ' . $uri); } $insert = imagecreatefrompng(WATERMARK_FILE_LOCATION); $insertWidth = imagesx($insert); $insertHeight = imagesy($insert); $imageWidth = imagesx($sourcefile_id); $imageHeight = imagesy($sourcefile_id); $overlapX = $imageWidth - $insertWidth - 5; $overlapY = $imageHeight - $insertHeight - 5; /** If source is GIF can't blend into, so just merge watermark insert with a set transparent background (This was the original code I started with). */ if ($fileType == 'gif') { imagecolortransparent($insert, imagecolorat($insert, 0, 0)); imagecopymerge($sourcefile_id, $insert, $overlapX, $overlapY, 0, 0, $insertWidth, $insertHeight, 90); } else { ImageAlphaBlending($sourcefile_id, true); imageCopy($sourcefile_id, $insert, $overlapX, $overlapY, 0, 0, $insertWidth, $insertHeight); } switch($fileType) { case('gif'): Header( "Content-type: image/gif"); ImageGIF($sourcefile_id); break; case('png'): Header( "Content-type: image/png"); ImagePNG($sourcefile_id); break; default: Header( "Content-type: image/jpeg"); ImageJPEG($sourcefile_id); } // destroy the memory ImageDestroy($sourcefile_id); ImageDestroy($insert); ?> This means the page HTML doesn't have to change, it fetches what it thinks are image files exactly as before, but now they come with watermarks.

« previous php.notes (#190965) next »