---
title: "Skala bild"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/131061-skala-bild"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "Flow"
published: "2005-07-07T06:46:53.000Z"
updated: "2005-07-08T07:10:55.000Z"
replies: 7
views: 452
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/131061-skala-bild"
---

# Skala bild

## #1 — Flow, 2005-07-07T06:46Z

Någon som kan förklara varför denna funktion inte fungerar på bilder som är större än 1500-2000 pixlar i bredd?

```php
	function scaleImage($userfile_name, $width_size, $userfile_type)
	{		

		///////// Start the thumbnail generation//////////////
				$image_stats = GetImageSize("$userfile_name");
				$original_height = $image_stats[1];
				$original_width = $image_stats[0];
				
				echo "original width: " . $image_stats[0] . " height: " . $image_stats[1];
				
				//if($original_width>=430) {
				
				if($original_width>=$width_size) {
					
				$n_width=$width_size;          // Fix the width of the thumb nail images
				$ratio = ($original_width / $original_height);

				//$n_height=100;         // Fix the height of the thumb nail imaage
				$n_height = round($n_width / $ratio);
				
				/*
				echo "Original height: $original_height<br>";
				echo "Ratio: $ratio<br>";
				echo "N_width: $n_width<br>";
				echo "N_height: $n_height<br>";
				*/
				$tsrc="$userfile_name";   // Path where thumb nail image will be stored
				//echo $tsrc;

				if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif")){echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed!<BR>";
				//exit;
				}
				/////////////////////////////////////////////// Starting of GIF thumb nail creation///////////
				if (@$userfile_type=="image/gif")
				{
				$im=ImageCreateFromGIF($userfile_name);
				$width=ImageSx($im);              // Original picture width is stored
				$height=ImageSy($im);             // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				if (function_exists("imagegif")) {
				Header("Content-type: image/gif");
				ImageGIF($newimage,$tsrc);
				}
				elseif (function_exists("imagejpeg")) {
				Header("Content-type: image/jpeg");
				ImageJPEG($newimage,$tsrc);
				}
				chmod("$tsrc",0777);
				}////////// end of gif file thumb nail creation//////////
				
				////////////// starting of JPG thumb nail creation//////////
				if($userfile_type=="image/pjpeg"){
				$im=ImageCreateFromJPEG($userfile_name); 
				$width=ImageSx($im);              // Original picture width is stored
				$height=ImageSy($im);             // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);                 
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImageJpeg($newimage,$tsrc);
				chmod("$tsrc",0777);
				}
				////////////////  End of JPG thumb nail creation //////////			
				}	
		}
```

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

## #2 — Gislaved, 2005-07-07T17:51Z

Har du provat att byta ut  imageCopyResized mot imagecopyresampled?

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

## #3 — tydal, 2005-07-07T18:01Z

Om du berättade vad det blev för fel så skulle vi kunna ha en möjlighet att förklara. Nu kan vi bara gissa.

Jag gissar dock på att minnesgränsen för skript överskrids. (Sätts i php.ini memory_limit)

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

## #4 — Flow, 2005-07-08T06:31Z

Hej!, Får inget felmeddelande, skriptet stoppar sig själv och lämnar inget meddelande alls, precis som om man hade skrivit exit(); 

Pröva imagecopyresampled men det fungerade inte heller... Om jag tvingar och sätter bredden och höjden till 100x100 så fungererar det, men inte när scriptet läser av höjden och bredden och gör en "ratio" på det.

Har även kollat så att den kan läsa bildbredd och höjd och det gör den...

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

## #5 — pettsson, 2005-07-08T06:57Z

Laddar du upp en bild med typ image/pjpeg då? Har för mig att det betyder progressiv jpeg, pröva att lägga till typen image/jpeg i if-satsen.
/r:
pjpeg var inte progressiv jpeg, men lägg ändå till image/jpeg i listan, verkar vara lite beroende på vilket program som sparade bilden vilken typ den får

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

## #6 — Flow, 2005-07-08T07:00Z

Har lagt in en echo rad efter varje kommando som utförs och den stannar på följande rad:

```php
				$newimage=imagecreatetruecolor($n_width,$n_height);
```

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

## #7 — Flow, 2005-07-08T07:09Z

Lade till detta:

	ini_set('memory_limit', '20M');

och nu fungerar det!

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

## #8 — pettsson, 2005-07-08T07:10Z

/r:
Bleh. Läste fel. Glöm det.

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

---

Tråden på webben: https://www.webforum.nu/amne/php/131061-skala-bild
