Hi Lee,
You could do this with the following php code on your sever. Which I've written most of it. It needs to GD library to be installed on the server.
Store the code below in a php file:
PHP Code:
<?php
/**
@description places a overlay on top of an image
@param resource $image the image resource
@param resource $overlay the overlay resource
@param integer $x x position of the overlay
@param integer $y y position of the overlay
@param integer $opacity the amount of opacity of the over lay
@return resource the image resource with the overlay on top
**/
function placeOverLay($image, $overlay, $x, $y, $opacity) {
// getting the overlay width
$w = imagesx($overlay);
// getting the overlay height
$h = imagesy($overlay);
// creating a cut resource
$cut = imagecreatetruecolor($w, $h);
// copying that section of the background to the cut
imagecopy($cut, $image, 0, 0, $x, $y, $w, $h);
// inverting the opacity
$opacity = 100 - $opacity;
// placing the watermark now
imagecopy($image, $overlay, $x, $y, 0, 0, $w, $h);
imagecopymerge($image, $cut, $x, $y, 0, 0, $w, $h, $opacity);
return $image;
}
/**
@description gets the content insisde a directory
@param string $path the directory were the source images are located
@param string $savePath the directory were the image with the overlay need to be stored
@param string $overLayImage the image that needs to be laid over the source image
@param string $x the coordinate x of the overlay image seen from the source image
@param string $y the coordinate y of the overlay image seen from the source image
@param string $opacity the tranparancy of the overlaid image
@param string $preFix the prefix that's added on the end of the source image $filenameSourceImage[$prefix].extension
when you don't want it give '' or just leave this parameter out
@return string the error messages are given as error message on the screen output
when done a summarry will be given
*/
function batchPlaceOverLay($path, $savePath, $overLayImage, $x, $y, $opacity, $preFix='') {
$savePath = (substr($savePath, -1) == "/") ? substr($savePath, 0, -1) : $savePath;
$path = (substr($path, -1) == "/") ? substr($path, 0, -1) : $path;
if(!is_dir($path)) exit('$path['.$path.'] is not a directory<br>');
if(!is_readable($path)) exit('$path['.$path.'] is not readable<br>');
if(!is_dir($savePath)) exit('$savePath['.$savePath.'] is not a directory<br>');
if(!is_readable($savePath)) exit('$savePath['.$savePath.'] is not readable<br>');
if(!is_writable($savePath)) exit('$savePath['.$savePath.'] is not writeable<br>');
if(!file_exists($overLayImage)) exit('$overLayImage['.$overLayImage.'] does not exists<br>');
if(!is_readable($overLayImage)) exit('$overLayImage['.$overLayImage.'] is not readable<br>');
$path .= "/";
$savePath .= "/";
$imageResourceOverLay = getImageResource($overLayImage);
if(!$imageResourceOverLay) exit('could not create an image identifier of $overLayImage['.$overLayImage.']<br>');
if($handle=opendir($path)) {
$countSouceImages = 0;
$countCreatedImages = 0;
while(false!==($entry=readdir($handle))) {
if(!ereg("^([.]{1,2})$", $entry)
&& is_file($path.$entry)
&& $path.$entry!=$overLayImage) {
$imageResourceSource = getImageResource($path . $entry);
if($imageResourceSource!==false) {
$countSouceImages++;
$extension = getExtensionFile($path . $entry);
$imageResourceFinal = placeOverLay($imageResourceSource, $imageResourceOverLay, $x, $y, $opacity);
$entry = str_replace('.'.$extension, $preFix.'.'.$extension, $entry);
switch($extension) {
case 'gif':
$writeFile = @imagegif($imageResourceFinal, $savePath . $entry);
break;
case 'jpg':
$writeFile = @imagejpeg($imageResourceFinal, $savePath . $entry);
break;
case 'png':
$writeFile = @imagepng($imageResourceFinal, $savePath . $entry);
break;
}
@imagedestroy($imageResourceFinal);
if($writeFile===false)
echo 'Failed to create new image['.$path.$entry.']<br>';
else $countCreatedImages++;
}
}
}
echo "total source images in " . $path . ":" . $countSouceImages . "<br>";
echo "total created images in " . $savePath . ":" . $countSouceImages . "<br>";
}
}
/**
@description gets the image identifier of $image
@param $string $image (absolute/relative)path to the image with the filename
@return boolean,resource false on failure, the image resource identifier on success
@note can only handle a jpg, gif or png image
*/
function getImageResource($image) {
$extension = getExtensionFile($image);
if(!$extension) return false;
$imageResource = false;
switch($extension) {
case 'gif':
$imageResource = @imagecreatefromgif($image);
break;
case 'jpg':
$imageResource = @imagecreatefromjpeg($image);
break;
case 'png':
$imageResource = @imagecreatefrompng($image);
break;
}
return $imageResource;
}
/**
@description gets the file extension form $filename
@param string $filename name of the file
@return string, boolean the extension without the . in front, false on failure
*/
function getExtensionFile($filename) {
if(ereg('.+\.(.+)', trim($filename), $matches)) return $matches[1];
return false;
}
?>
At the bottom of this file you can give the following command
PHP Code:
batchPlaceOverLay($path, $savePath, $overLayImage, $x, $y, $opacity, $preFix='')
At $path you fill in the path to the images related or absolute like
'images/' or '/home/www/sitename/htdocs/images/'
At $savePath you fill in the path to were the new images should be saved, same way as done at $path.
At $overLayImage you give the image you want to put on top of the source image. Something like '/home/www/sitename/htdocs/images/overlay.jpg' or
'overlay/overlay1.jpg'
At $x and $y you give the coordinates of the position the overlayed image should start. $x and $y are coordinates related to the source image. 0, 0 will put the overlayed at the left top of the source image.
At $opicty you give the amount of transparancy that should be given to the
overlayed images. For example when given 100 the overlayed image will not have any transparancy, when 0 you won't see the overlayed image because its complete tranparent.
At $prefix you give a possible prefix to the filename of the newly created image. If the source is called 'image.jpg' and the prefix is '_new' then the new image is called image_new.jpg. Leave this out when you don't want it or
put in ''
An example:
PHP Code:
batchPlaceOverLay('images/', 'result/', 'overlay/video2.jpg', 0, 0, 50, '_new');
The script can handle jpg, gif and png. I hope it will help you out and I wasn't to technical. Working with this script is ofcourse at own risk
Best Wishes,
Arian
Bookmarks