Page 1 of 1

Add watermark only if image is larger than ...

Posted: Tue Aug 24, 2010 2:07 pm
by seezee
I'm using a modified version of this script. I'd like it to process only images that are larger than a specified minimum width & height, say, 225 x 225. How would I do that?

Code: Select all

// watermark_wrapper.php

// Path the the requested file
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

// Load the requested image
$image = imagecreatefromstring(file_get_contents($path));

$w = imagesx($image);
$h = imagesy($image);

// Load the watermark image
$watermark = imagecreatefrompng('watermark.png');
$ww = imagesx($watermark);
$wh = imagesy($watermark);

// Merge watermark upon the original image
imagecopy($image, $watermark, (($w/96)-($ww/96)), (($h/96)-($wh/96)), 0, 0, $ww, $wh);// place watermark in upper left corner

// Send the image
header('Content-type: image/jpeg');
imagejpeg($image,null,95);
exit();

Re: Add watermark only if image is larger than ...

Posted: Tue Aug 24, 2010 2:30 pm
by McInfo

Code: Select all

define('MIN_WIDTH', 225);
define('MIN_HEIGHT', 225);

// Get image dimensions with imagesx() and imagesy()

if ($imageWidth >= MIN_WIDTH && $imageHeight >= MIN_HEIGHT) {
    // Process image
} else {
    // Do something else
}

Re: Add watermark only if image is larger than ...

Posted: Thu Aug 26, 2010 1:05 pm
by seezee
OK, that works:

Code: Select all

// watermark_wrapper.php

define('MIN_WIDTH', 225);
define('MIN_HEIGHT', 225);

// Path the the requested file
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

// Load the requested image
$image = imagecreatefromstring(file_get_contents($path));

$w = imagesx($image);
$h = imagesy($image);

if ($w >= MIN_WIDTH && $h >= MIN_HEIGHT) {
	// Load the watermark image
	$watermark = imagecreatefrompng('watermark.png');
	$ww = imagesx($watermark);
	$wh = imagesy($watermark);
	
	// Merge watermark upon the original image
	//imagecopy($image, $watermark, $w-$ww, $h-$wh, 0, 0, $ww, $wh);// place watermark in lower right corner
	//imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);// center watermark
	imagecopy($image, $watermark, (($w/96)-($ww/96)), (($h/96)-($wh/96)), 0, 0, $ww, $wh);// place watermark in upper left corner
	
	// Send the image
	header('Content-type: image/jpeg');
	imagejpeg($image,null,95);
	exit();
} else {
	imagecopy($image);// no watermark
	
	// Send the image
	header('Content-type: image/jpeg');
	imagejpeg($image,null,95);
	exit();
}

Re: Add watermark only if image is larger than ...

Posted: Thu Aug 26, 2010 1:20 pm
by seezee
Snippet for outputting a PNG:

Code: Select all

	// Send the image
	header('Content-type: image/jpeg');
	imagepng($image,null,0,PNG_NO_FILTER);
	exit();
See the PHP manual for syntax.

Re: Add watermark only if image is larger than ...

Posted: Fri Oct 21, 2011 2:26 pm
by seezee
Final version (preserves full alpha transparency):

Code: Select all

<?php
// watermark_wrapper.php

define('MIN_WIDTH', 225);
define('MIN_HEIGHT', 225);

// Path to the requested file
$path = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];

// Load the requested image
$image = imagecreatefromstring(file_get_contents($path));

$w = imagesx($image);
$h = imagesy($image);

if ($w >= MIN_WIDTH && $h >= MIN_HEIGHT) {
	// Load the watermark image
	$watermark = imagecreatefrompng('watermark.png');
	$ww = imagesx($watermark);
	$wh = imagesy($watermark);
	
	// Merge watermark upon the original image
	//imagecopy($image, $watermark, $w-$ww, $h-$wh, 0, 0, $ww, $wh);// place watermark in lower right corner
	//imagecopy($image, $watermark, (($w/2)-($ww/2)), (($h/2)-($wh/2)), 0, 0, $ww, $wh);// center watermark
	imagecopy($image, $watermark, (($w/96)-($ww/96)), (($h/96)-($wh/96)), 0, 0, $ww, $wh);// place watermark in upper left corner
	imagealphablending($image, false);
	imagesavealpha($image, true);		

	// Send the image
	header('Content-Type: ' . $image_data['mime']);
	imagepng($image,null,0,PNG_NO_FILTER);
	imagedestroy($image);
	imagedestroy($watermark);
	exit();

} else {
	imagecopy($image);// no watermark
	imagealphablending($image, false);
	imagesavealpha($image, true);		

	// Send the image
	header('Content-Type: ' . $image_data['mime']);
	imagepng($image,null,0,PNG_NO_FILTER);
	imagedestroy($image);
	imagedestroy($watermark);
	exit();
}
?>