Add watermark only if image is larger than ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

Add watermark only if image is larger than ...

Post 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();
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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
}
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

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

Post 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();
}
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

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

Post 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.
User avatar
seezee
Forum Newbie
Posts: 17
Joined: Thu Aug 05, 2010 9:59 am
Location: Oklahoma

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

Post 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();
}
?>
Post Reply