---
title: "Problem med beskärning vid uppladdning"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/169605-problem-med-beskärning-vid-uppladdning"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "Asa"
published: "2008-03-04T15:15:55.000Z"
updated: "2008-03-09T19:16:28.000Z"
replies: 1
views: 423
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/169605-problem-med-beskärning-vid-uppladdning"
---

# Problem med beskärning vid uppladdning

## #1 — Asa, 2008-03-04T15:15Z

Kan någon hjälpa mig med denna koden så den laddar upp och förminskar bilden. jpg eller gif ska vara tillåtet. Jag vill att den skapar 4 olika storlekar vid uppladdning. De ska heta bildnamn_80.jpg, bildnamn_100.jpg, bildnamn_200.jpg, bildnamn_300.jpg

Denna koden funkar alltså inte att beskära bilden utan den blir orginalstorlek. Varför vet jag inte. FÅr inga fel eller så. Vill att den ska skapa 4 olika filer med namnen ovan. bildnamn kan vara **$artnr**

Sen vill jag kunna sätta bredden och inte höjden som står i koden..

```
if (is_uploaded_file($_FILES['bild']['tmp_name'])) {
      $tempname = $_FILES['bild']['tmp_name'];
      $filnamn  = $_FILES['bild']['name'];
      $filtyp   = $_FILES['bild']['type'];
 
      $imgfulldir  = "../gfx/produkt/";
 
      move_uploaded_file($tempname, $imgfulldir.$artnr."_".$filnamn);
 
      $src_filename = $imgfulldir.$artnr."_".$filnamn;
      $dst_filename = $imgfulldir.$artnr."_".$filnamn;
 
      $src_size = getimagesize ("$src_filename");
 
      $src_width   = $src_size[0];
      $src_height  = $src_size[1];
      $src_dim     = $src_size[3];
 
      $dst_height = 200;
      $dst_scaling = 100 / $src_height;
      $dst_scaling2 = round($dst_scaling * 100);
      $dst_width = round($dst_scaling * $src_width);
 
      $dst_dim = "width=$dst_width height=$dst_height";
 
      $src_img = imagecreatefromjpeg("$src_filename");
      $dst_img = imagecreatetruecolor($dst_width,$dst_height);
 
      imagecopyresampled($dst_img,$src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
      imagejpeg($dst_img, "$dst_filename", 75);
}
```

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

## #2 — tha_neo, 2008-03-09T19:16Z

Låter som mycket och skriva ärligt talat. men skapa filen
resize_thumb.php
\-----------------
\<?php
\##############################################
\# Shiege Iseng Resize Class
\# 11 March 2003
\# shiegege_at_yahoo.com
\# View Demo :
\#   <http://shiege.com/scripts/thumbnail/>
/\*############################################
include 'utf8/utf8.php';

Sample :
$thumb=new thumbnail("./shiegege.jpg");            // generate image_file, set filename to resize
$thumb-\>size_width(100);                // set width for thumbnail, or
$thumb-\>size_height(300);                // set height for thumbnail, or
$thumb-\>size_auto(200);                    // set the biggest width or height for thumbnail
$thumb-\>jpeg_quality(75);                // \[OPTIONAL\] set quality for jpeg only (0 - 100) (worst - best), default = 75
$thumb-\>show();                        // show your thumbnail
$thumb-\>save("./huhu.jpg");                // save your thumbnail to file
\----------------------------------------------
Note :
\- GD must Enabled
\- Autodetect file extension (.jpg/jpeg, .png, .gif, .wbmp)
  but some server can't generate .gif / .wbmp file types
\- If your GD not support 'ImageCreateTrueColor' function,
  change one line from 'ImageCreateTrueColor' to 'ImageCreate'
  (the position in 'show' and 'save' function)
\*/############################################

class thumbnail
{
    var $img;

    function thumbnail($imgfile)
    {
        //detect image format
        $this-\>img\["format"\]=ereg_replace(".\*\\.(.\*)$","\\\\1",$imgfile);
        $this-\>img\["format"\]=strtoupper($this-\>img\["format"\]);
        if ($this-\>img\["format"\]=="JPG" \|\| $this-\>img\["format"\]=="JPEG") {
            //JPEG
            $this-\>img\["format"\]="JPEG";
            $this-\>img\["src"\] = ImageCreateFromJPEG ($imgfile);
        } elseif ($this-\>img\["format"\]=="PNG") {
            //PNG
            $this-\>img\["format"\]="PNG";
            $this-\>img\["src"\] = ImageCreateFromPNG ($imgfile);
        } elseif ($this-\>img\["format"\]=="GIF") {
            //GIF
            $this-\>img\["format"\]="GIF";
            $this-\>img\["src"\] = ImageCreateFromGIF ($imgfile);
        } elseif ($this-\>img\["format"\]=="WBMP") {
            //WBMP
            $this-\>img\["format"\]="WBMP";
            $this-\>img\["src"\] = ImageCreateFromWBMP ($imgfile);
        } else {
            //DEFAULT
            echo "Not Supported File";
            exit();
        }
        @$this-\>img\["lebar"\] = imagesx($this-\>img\["src"\]);
        @$this-\>img\["tinggi"\] = imagesy($this-\>img\["src"\]);
        //default quality jpeg
        $this-\>img\["quality"\]=75;
    }

    function size_height($size=100)
    {
        //height
        $this-\>img\["tinggi_thumb"\]=$size;
        @$this-\>img\["lebar_thumb"\] = ($this-\>img\["tinggi_thumb"\]/$this-\>img\["tinggi"\])\*$this-\>img\["lebar"\];
    }

    function size_width($size=100)
    {
        //width
        $this-\>img\["lebar_thumb"\]=$size;
        @$this-\>img\["tinggi_thumb"\] = ($this-\>img\["lebar_thumb"\]/$this-\>img\["lebar"\])\*$this-\>img\["tinggi"\];
    }

    function size_auto($size=100)
    {
        //size
        if ($this-\>img\["lebar"\]\>=$this-\>img\["tinggi"\]) {
            $this-\>img\["lebar_thumb"\]=$size;
            @$this-\>img\["tinggi_thumb"\] = ($this-\>img\["lebar_thumb"\]/$this-\>img\["lebar"\])\*$this-\>img\["tinggi"\];
        } else {
            $this-\>img\["tinggi_thumb"\]=$size;
            @$this-\>img\["lebar_thumb"\] = ($this-\>img\["tinggi_thumb"\]/$this-\>img\["tinggi"\])\*$this-\>img\["lebar"\];
        }
    }

    function jpeg_quality($quality=75)
    {
        //jpeg quality
        $this-\>img\["quality"\]=$quality;
    }

    function show()
    {
        //show thumb
        @Header("Content-Type: image/".$this-\>img\["format"\]);

        /\* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function\*/
        $this-\>img\["des"\] = ImageCreateTrueColor($this-\>img\["lebar_thumb"\],$this-\>img\["tinggi_thumb"\]);
            @imagecopyresized ($this-\>img\["des"\], $this-\>img\["src"\], 0, 0, 0, 0, $this-\>img\["lebar_thumb"\], $this-\>img\["tinggi_thumb"\], $this-\>img\["lebar"\], $this-\>img\["tinggi"\]);

        if ($this-\>img\["format"\]=="JPG" \|\| $this-\>img\["format"\]=="JPEG") {
            //JPEG
            imageJPEG($this-\>img\["des"\],"",$this-\>img\["quality"\]);
        } elseif ($this-\>img\["format"\]=="PNG") {
            //PNG
            imagePNG($this-\>img\["des"\]);
        } elseif ($this-\>img\["format"\]=="GIF") {
            //GIF
            imageGIF($this-\>img\["des"\]);
        } elseif ($this-\>img\["format"\]=="WBMP") {
            //WBMP
            imageWBMP($this-\>img\["des"\]);
        }
    }

    function save($save="")
    {
        //save thumb
        if (empty($save)) $save=utf8_strtolower("./thumb.".$this-\>img\["format"\]);
        /\* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function\*/
        $this-\>img\["des"\] = ImageCreateTrueColor($this-\>img\["lebar_thumb"\],$this-\>img\["tinggi_thumb"\]);
            @imagecopyresized ($this-\>img\["des"\], $this-\>img\["src"\], 0, 0, 0, 0, $this-\>img\["lebar_thumb"\], $this-\>img\["tinggi_thumb"\], $this-\>img\["lebar"\], $this-\>img\["tinggi"\]);

        if ($this-\>img\["format"\]=="JPG" \|\| $this-\>img\["format"\]=="JPEG") {
            //JPEG
            imageJPEG($this-\>img\["des"\],"$save",$this-\>img\["quality"\]);
        } elseif ($this-\>img\["format"\]=="PNG") {
            //PNG
            imagePNG($this-\>img\["des"\],"$save");
        } elseif ($this-\>img\["format"\]=="GIF") {
            //GIF
            imageGIF($this-\>img\["des"\],"$save");
        } elseif ($this-\>img\["format"\]=="WBMP") {
            //WBMP
            imageWBMP($this-\>img\["des"\],"$save");
        }
    }
}
?\>
\----------------------
Sen när du ska resiza bilden :

\----------------------
include_once("resize_thumb.php");
$thumb=new thumbnail("../images/members/presentations/$userid/$newfile"); //prepare to generate "shiegege.jpg" in directory "/www"
				$thumb-\>size_width(75);           // sätter bildens bredd till 75px
			$thumb-\>jpeg_quality(80);
			$thumb-\>save("images/");       // sparar bilden i mappen images

sen kan du få leka själv med att göra if satser o while satser för att få ut fyra olika mått genom att ändra size_width(75) till önskad bredd. den generar alltså automatiskt ut höjden för att bilden ska bli bra.
du kan även ändra size_width till size_height om du vill det

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

---

Tråden på webben: https://www.webforum.nu/amne/php/169605-problem-med-beskärning-vid-uppladdning
