Thanks for the tips on this page guys. This version does (should do) the following:
1. Load the image and scale it to a given maximum dimension.
2. Load the watermark file
3. Work out the width of the image and the watermark and apply the watermark centrally
4. Create a thumbnail without a watermark but scaled to a given maximum dimension.
5. Save out both thumbnail and watermarked images to file.
6. Delete the orginal file.
I did it for me and my needs but it should be pretty easy to tweak.
Might help anyone putting up an image library ...
<?php
function waterMarkImage($catalogueRef, $imageName = '') {
$watermarkSrc = dirname(__FILE__) . '/spc_watermark.png';
$sourceDir = dirname(__FILE__) . '/tmp/';
$thumbnailDir = dirname(__FILE__) . '/../images/thumbs/';
$outputDir = dirname(__FILE__) . '/../images/large/';
$maximumDimension = 500; // max image dimension
$maximumThumbnail = 200; // max thumbnail dimension
if ($imageName == '') {
$imageName = $catalogueRef . ".jpg";
}
$rawImage = imagecreatefromjpeg($sourceDir . $imageName);
// ======================================================================== Find base image size & scale down to a max of 500px
$rawWidth = imagesx($rawImage);
$rawHeight = imagesy($rawImage);
$imageScaleTo = 1;
if ($rawWidth >= $rawHeight) { // if a landsscape or square image
if ($rawWidth > $maximumDimension) {
$imageScaleTo = $maximumDimension/$rawWidth;
}
// scale thumbnail
$thumbNailScale = $maximumThumbnail/$rawWidth;
$thumbNailWidth = $maximumThumbnail;
$thumbNailHeight = floor($rawHeight * $thumbNailScale);
$thumbNailImage = imagecreatetruecolor($thumbNailWidth, $thumbNailHeight);
imagecopyresampled($thumbNailImage, $rawImage, 0, 0, 0, 0, $thumbNailWidth, $thumbNailHeight, $rawWidth, $rawHeight);
} else { // if a portrait image
if ($rawHeight > $maximumDimension) {
$imageScaleTo = $maximumDimension/$rawHeight;
}
// scale thumbnail
$thumbNailScale = $maximumThumbnail/$rawHeight;
$thumbNailWidth = floor($rawWidth * $thumbNailScale);
$thumbNailHeight = $maximumThumbnail;
$thumbNailImage = imagecreatetruecolor($thumbNailWidth, $thumbNailHeight);
imagecopyresampled($thumbNailImage, $rawImage, 0, 0, 0, 0, $thumbNailWidth, $thumbNailHeight, $rawWidth, $rawHeight);
}
// ======================================================================== Create Output Image
$outputWidth = floor($rawWidth * $imageScaleTo);
$outputHeight = floor($rawHeight * $imageScaleTo);
$outputImage = Imagecreatetruecolor($outputWidth, $outputHeight);
imagecopyresampled($outputImage, $rawImage, 0, 0, 0, 0, $outputWidth, $outputHeight, $rawWidth, $rawHeight);
// Turn on alpha blending
imagealphablending($outputImage, true);
// Create overlay image
$watermark = imagecreatefrompng($watermarkSrc);
// Get the offset centring for the overlay
$offsetWidth = floor(($outputWidth - imagesx($watermark))/2);
$offsetHeight = floor(($outputHeight - imagesy($watermark))/2);
// Overlay watermark
imagecopy($outputImage, $watermark, $offsetWidth, $offsetHeight, 0, 0, imagesx($watermark), imagesy($watermark));
// Output header and final images to file
imagejpeg($outputImage, $outputDir . $catalogueRef . ".jpg");
imagejpeg($thumbNailImage, $thumbnailDir . $catalogueRef . ".jpg");
// Destroy the images
imagedestroy($outputImage);
imagedestroy($thumbNailImage);
imagedestroy($rawImage);
imagedestroy($watermark);
// clean up temp
unlink($sourceDir . $imageName);
}
?>