Jump to content

greyscale thumbnails


Woodburn2006

Recommended Posts

is there anyway of changing this function so that it creates greyscale thumbnails rather than coloured images?

 

<?
$db_path = "/customers/ontherocks.me.uk/ontherocks.me.uk/httpd.www/cp/db.inc";

function connect_to_sql($host,$usr,$pwd,$db)
{
	$connection = mysql_connect($host,$usr,$pwd);
    	mysql_select_db($db);
	if (mysql_error()) { print "Database ERROR: " . mysql_error(); }

	return $connection;
}

################################################################################

function createthumb($name,$filename,$new_w,$new_h)
{
	$system=explode('.',$name); // split filename either side of the '.'
	if (preg_match('/jpg|jpeg|JPG/',$system[1])){ // if extension is jpg or jpeg
		$src_img=imagecreatefromjpeg($name); // create a copy of the image in jpg
	}
	if (preg_match('/png/',$system[1])){ // if extension is png
		$src_img=imagecreatefrompng($name); // create a copy of the image in png
	}
	$old_x=imageSX($src_img); // gets width of original image
	$old_y=imageSY($src_img); // gets height of original image
	if ($old_x > $old_y) { // if img is wider than high
		$thumb_w=$new_w; // sets width variable of new img
		$thumb_h=$old_y*($new_h/$old_x); // sets height variable by doing: height = original width * (100 / original height)
	}
	if ($old_x < $old_y) { // other way around from setting wider than high
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
	}
	if ($old_x == $old_y) { // if img is square sets variabls straight forwardly
		$thumb_w=$new_w;
		$thumb_h=$new_h;
	}
	$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); // creates image
	imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // copies into new image
	if (preg_match("/png/",$system[1])) // if old file extension is png
	{
		imagepng($dst_img,$filename); // extension of new image is png
	} else {
		imagejpeg($dst_img,$filename); // else extension of new image is jpeg or jpg
	}
	imagedestroy($dst_img); //destroys variable
	imagedestroy($src_img); 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/154100-greyscale-thumbnails/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.