Jump to content

Amalgamate image resize script & watermark script.


Recommended Posts

Hi guys,

 

I really hope someone can help. I've got the following 2 scripts. The first handles my resizing and the second adds a watermark:

<?php
function saveThumb($gal,$fn) {
define("MAX_XY_SMALL", 120); // maximum width or height of thumbnail image
define("MAX_XY_LARGE", 800); // maximum width or height of thumbnail image
$fpath = "categories/$gal/$fn";
//print $fpath."\n";
/* GetImageSize returns an array:
 * $info[0] - horizontal size of image in pixels
 * $info[1] - vertical size of image in pixels
 * $info[2] - type of image (1=GIF, 2=JPEG, 3=PNG)
*/
$info = GetImageSize("$fpath");
//print_r($info);

// do we need to resize image?
if ($info[0] > MAX_XY_SMALL || $info[1] > MAX_XY_SMALL) {
	// is image landscape?
	if ($info[0] >= $info[1]) {
		$width = MAX_XY_SMALL;
		$height = $info[1]*MAX_XY_SMALL/$info[0];
	// or portrait?
	} else {
		$height = MAX_XY_SMALL;
		$width = $info[0]*MAX_XY_SMALL/$info[1];
	}
} else {
	// use original dimensions
	$width = $info[0];
	$height = $info[1];
}


if ($info[0] > MAX_XY_LARGE || $info[1] > MAX_XY_LARGE) {
	// is image landscape?
	if ($info[0] >= $info[1]) {
		$width_large = MAX_XY_LARGE;
		$height_large = $info[1]*MAX_XY_LARGE/$info[0];
	// or portrait?
	} else {
		$height_large = MAX_XY_LARGE;
		$width_large = $info[0]*MAX_XY_LARGE/$info[1];
	}
} else {
	// use original dimensions
	$width_large = $info[0];
	$height_large = $info[1];
}	

// create new thumbnail image
//echo "<br>$width - $height<br>";
$im = ImageCreateTrueColor($width, $height);
$im_large = ImageCreateTrueColor($width_large, $height_large);

/* determine image type and load original image. Notice new tests for image
 * types. The GD extension has changed a lot over the years, dropping GIFs
 * and adding PNG support. By testing, we avoid trying to process an image
 * PHP can't deal with
*/
$im_big = ""; // default is no image
switch ($info[2]) {
	case 1 :
		if (ImageTypes() & IMG_GIF) // test for GIF support 
			$im_big = ImageCreateFromGIF("$fpath");
		break;
	case 2 :
		if (ImageTypes() & IMG_JPG) // test for JPEG support 
			$im_big = ImageCreateFromJPEG("$fpath");
		break;
	case 3 :
		if (ImageTypes() & IMG_PNG) // test for PNG support  
			$im_big = ImageCreateFromPNG("$fpath");
		break;
	case 4 :
		if (ImageTypes() & IMG_BMP) // test for BMP support  
			$im_big = ImageCreateFromBMP("$fpath");
		break;			
}

if ($im_big) {
	/* resize original image into thumbnail - see PHP Manual for details on
	 * the arguements ImageCopyResized takes
	*/
	ImageCopyResized($im,$im_big,0,0,0,0,$width,$height,$info[0],$info[1]);
	ImageCopyResized($im_large,$im_big,0,0,0,0,$width_large,$height_large,$info[0],$info[1]);
} else {
	// couldn't load original image, so generate 'no thumbnail' image instead
	$width = 150;
	$height = 150;
      	$im = ImageCreate($width, $height); // create a blank image
      	$bgc = ImageColorAllocate($im, 0, 0, 0); // background color = black
      	$tc  = ImageColorAllocate($im, 255, 255, 255); // text color = white
      	ImageFilledRectangle($im, 0, 0, $width, $height, $bgc); // draw rectangle
      	ImageString($im, 3, 9, 36, "No thumbnail", $tc); // add text
		ImageString($im, 3, 17, 48, "available", $tc);  
  	}

/* save our image in thumb directory - if the second argument is left out,
 * the image gets sent to the browser, as in thumbnail.php
*/
ImageJPEG($im, "categories/".$gal."/t_".$fn);
ImageJPEG($im_large, "categories/".$gal."/".$fn);

// clean up our working images
ImageDestroy($im_big);
ImageDestroy($im);
ImageDestroy($im_large);
  }

?>

The below is my watermark script

$imagesource =  $_GET['path']; 
$filetype = substr($imagesource,strlen($imagesource)-4,4); 
$filetype = strtolower($filetype); 
if($filetype == ".gif")  $image = @imagecreatefromgif($imagesource);  
if($filetype == ".jpg")  $image = @imagecreatefromjpeg($imagesource);  
if($filetype == ".png")  $image = @imagecreatefrompng($imagesource);  
if (!$image) die(); 
$watermark = @imagecreatefromgif('watermark.gif'); 
$imagewidth = imagesx($image); 
$imageheight = imagesy($image);  
$watermarkwidth =  imagesx($watermark); 
$watermarkheight =  imagesy($watermark); 
$startwidth = (($imagewidth - $watermarkwidth)/2); 
$startheight = (($imageheight - $watermarkheight)/2); 
imagecopy($image, $watermark,  $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); 
imagejpeg($image); 
imagedestroy($image); 
imagedestroy($watermark); 

 

Is there a way to combine the 2 so that the watermark is added to the larger resized file. Also, I notice my watermark script isn't writing the watermark to the image, it simply adds it on the screen over the top, meaning you can download the image without problems. How can I sort this too?

 

Thanks sooo much!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.