---
title: "Skapa Thumbnails, förminska bilder"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/122337-skapa-thumbnails-förminska-bilder"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "Duffer"
published: "2005-02-13T16:17:15.000Z"
updated: "2005-02-13T16:17:15.000Z"
replies: 0
views: 3092
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/122337-skapa-thumbnails-förminska-bilder"
---

# Skapa Thumbnails, förminska bilder

## #1 — Duffer, 2005-02-13T16:17Z

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***

```php
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"

```php
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=)

Permalänk: https://www.webforum.nu/p/122337

---

Tråden på webben: https://www.webforum.nu/amne/php/122337-skapa-thumbnails-förminska-bilder
