Return to Snippet

Revision: 10809
at November 8, 2010 08:56 by shahways


Updated Code
function set_thumb($file, $photos_dir=�uploads/photos�,$thumbs_dir=�uploads/photos/thumbs�, $square_size=167, $quality=100) {
	//check if thumb exists
	if (!file_exists($thumbs_dir."/".$file)) {
		//get image info
		list($width, $height, $type, $attr) = getimagesize($photos_dir."/".$file);
		//set dimensions
		if($width> $height) {
			$width_t=$square_size;
			//respect the ratio
			$height_t=round($height/$width*$square_size);
			//set the offset
			$off_y=ceil(($width_t-$height_t)/2);
			$off_x=0;
		} elseif($height> $width) {
			$height_t=$square_size;
			$width_t=round($width/$height*$square_size);
			$off_x=ceil(($height_t-$width_t)/2);
			$off_y=0;
		} else {
			$width_t=$height_t=$square_size;
			$off_x=$off_y=0;
		}
		$thumb=imagecreatefromjpeg($photos_dir."/".$file);
		$thumb_p = imagecreatetruecolor($square_size, $square_size);
		//default background is black
		$bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
		imagefill ( $thumb_p, 0, 0, $bg );
		imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
		imagejpeg($thumb_p,$thumbs_dir."/".$file,$quality);
	}
}

Revision: 10808
at January 14, 2009 08:13 by shahways


Initial Code
function set_thumb($file, $photos_dir=‘uploads/photos’,$thumbs_dir=‘uploads/photos/thumbs’, $square_size=167, $quality=100) {
	//check if thumb exists
	if (!file_exists($thumbs_dir."/".$file)) {
		//get image info
		list($width, $height, $type, $attr) = getimagesize($photos_dir."/".$file);
		//set dimensions
		if($width> $height) {
			$width_t=$square_size;
			//respect the ratio
			$height_t=round($height/$width*$square_size);
			//set the offset
			$off_y=ceil(($width_t-$height_t)/2);
			$off_x=0;
		} elseif($height> $width) {
			$height_t=$square_size;
			$width_t=round($width/$height*$square_size);
			$off_x=ceil(($height_t-$width_t)/2);
			$off_y=0;
		} else {
			$width_t=$height_t=$square_size;
			$off_x=$off_y=0;
		}
		$thumb=imagecreatefromjpeg($photos_dir."/".$file);
		$thumb_p = imagecreatetruecolor($square_size, $square_size);
		//default background is black
		$bg = imagecolorallocate ( $thumb_p, 255, 255, 255 );
		imagefill ( $thumb_p, 0, 0, $bg );
		imagecopyresampled($thumb_p, $thumb, $off_x, $off_y, 0, 0, $width_t, $height_t, $width, $height);
		imagejpeg($thumb_p,$thumbs_dir."/".$file,$quality);
	}
}

Initial URL

                                

Initial Description

                                

Initial Title
PHP Thumbnail function

Initial Tags
images

Initial Language
PHP