note 110130 added to function.imagecopymerge

From: Date: Sat, 22 Sep 2012 17:54:26 +0000
Subject: note 110130 added to function.imagecopymerge
Groups: php.notes 
Request: Send a blank email to php-notes+get-190961@lists.php.net to get a copy of this message
(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. ---- Server IP: 69.147.83.199 Probable Submitter: 62.56.51.123 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopymerge.php Edit -- https://master.php.net/note/edit/110130 Del: integrated -- https://master.php.net/note/delete/110130/integrated Del: useless -- https://master.php.net/note/delete/110130/useless Del: bad code -- https://master.php.net/note/delete/110130/bad+code Del: spam -- https://master.php.net/note/delete/110130/spam Del: non-english -- https://master.php.net/note/delete/110130/non-english Del: in docs -- https://master.php.net/note/delete/110130/in+docs Del: other reasons-- https://master.php.net/note/delete/110130 Reject -- https://master.php.net/note/reject/110130 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#190961) next »