webForumDet fria alternativet

Skapa Thumbnails, förminska bilder

PHP

0 svar · 3 086 visningar · startad av Duffer

Medlem sedan mars 2003429 inlägg
Frågan#1

Hej :)

Vet att många söker script eller funktioner att skapa så kallade thumbnails med php, så därför har jag försökt skapa en funktion som liknar något som skulle kunna vara en phpstandard =)

Systemkrav:

  • GD 2.0.1
  • PHP 4 eller senare

imagecreatethumb -- Copy and resize an image

Description

bol imagecreatethumb ( [dst_filename], src_filename, [src_path], [dst_width], [dst_height], [dst_quality] )

dst_filename includes destiny path and filename, can be replaced width NULL. src_filename tells the filename of the image source. src_path is the path to the image source and can be replaced with NULL. dst_width and dst_height tells the size of the destiny image, if one of the is replaced width NULL the function will automaticly scale the image. If both are NULL the function will return a thumbnail 120px * 90px. dst_quality tells the quality for the destiny image should be a int between 1-100, where 100 is highest. If it is replaced with NULL DEFAULT is 100.

return true on success else false

function imagecreatethumb($dst_file, $src_filename, $src_path, $dst_width, $dst_height, $dst_quality) {
	// Written by: Tobias Engström
	
	// Kontrollerar källfilen
	if(strlen($src_path) == 0 || $src_path == NULL) {
		$file = $src_filename;
	} else {
		$file = $src_path."/".$src_filename;
	}
	if(!file_exists($file)) {
		echo "Could not find image source";
		return false;
		die();
	}
	// Ordnar med storleken på bilden
	list($width, $height) = getimagesize($file);
	
	if(is_numeric($dst_width) && $dst_width > 0 || $dst_width <> NULL) {
		if($dst_height <= 0 || $dst_height == NULL) {
			$dst_height = ($dst_width / $width) * $height;
		} 
	} elseif(is_numeric($dst_height) && $dst_height > 0 || $dst_height <> NULL) {
		if($dst_width <= 0 || $dst_width == NULL) {
			$dst_width = ($dst_height / $height) * $width;
		}
	} else {
		$dst_width = 120;
		$dst_height = 90;
	}

	// Ordnar med ifall bilden ska sparas eller inte och hur
	if(strlen($dst_file) == 0 && $dst_file <> NULL) {
		$dst_file = NULL;
	}

	if(!is_numeric($dst_quality) || $dst_quality < 0 || $dst_quality == NULL) {
		$dst_quality = 100;
	}

	
	// Utför önskade åtgärder
	$image_p = imagecreatetruecolor($dst_width, $dst_height);
	$image = imagecreatefromjpeg($file);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width, $height);

	// Sparar / utdata
	imagejpeg($image_p, $dst_file, $dst_quality);

	return true;
}

Example 1

This will return a image (t_test.jpg) with the size: 120px * 90px and the quality "78"

imagecreatethumb("t_test.jpg", "test.jpg", NULL, NULL, NULL, 78);

Hoppas någon får användning för denna lilla sida och funktion som jag skapat =)

Kommentera gärna mitt jobb och kom med förslag på ändringar=)

250 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
116 ms — deklarationer (db)
0 ms — hämta statistik (cache)
129 ms — hämta tråd, inlägg och bilagor (db)
116 ms — ändringar (db)