Avatar billede dero Nybegynder
09. januar 2004 - 15:39 Der er 11 kommentarer og
1 løsning

resing et billede med imagecopyresized

Hej.

Jeg skal bruge et php script, som tager et billede, og kopier det, og ligger det ind i mappen "billeder/small/" hvor at billedet selvfølgelig skal være blevet mindre. Billedet skal være 77 * 103

Jeg vil godt have det til at blive gjordt med imagecopyresized, hvis det er muligt!

p.s. Jeg er newbie, så jeg er nød til at have et HELT script, som jeg bare skal kopier og gemme.

p.p.s Det er ikke min egen server, så jeg kan ikke installere noget på den.

Mange tak..

Mvh
Anders
Avatar billede sylvesternielsen Nybegynder
09. januar 2004 - 15:46 #1
Avatar billede sylvesternielsen Nybegynder
09. januar 2004 - 15:46 #2
<?php
    # Classes by Thomas Daugaard 24/01-2002.

    # Set script time limit to 120 seconds.
    set_time_limit(120);

    /*
        Class    : imageresize
        Purpose  : Re- size/sample an image (JPG or PNG)

        Variables :
                    new_width        - New width
                    new_height        - New height
                    resize_ax        - If 1 = auto. adjust is done to height, 2 = .. to width.
                    maintain_aspect    - If true, image resizing will maintain aspect ratio of image.
                                      If true, only new_width or new_height should be set.
                                      Whichever is set last is effective.

                    output            - Output filename. If emtpy "" output will be to stdout.
                    jpeg_quality    - Quality of JPEG image

        Functions :
                    bool source(string image_file)
                        Loads image_file as the active source image

                    bool resize()
                        Performs a pixel resize on the source image

                    bool resample()
                        Performs a resample on the source image


            All returns true on success, otherwise false.
    */
    class imageresize {
        var $new_width;            # Positive value: width in px; Negative: width in % of org. width
        var $new_height;        # Same as above for height.
        var $resize_ax;            # 1 = Resize width proportionally to width; 2 = Resize height ...
                                # if maintain_aspect is true.
                                # Default: 1

        var $maintain_aspect;    # Maintain aspect ratio. True/False.
                                # Default: True

        var $output;            # Filename to output to. If emtpy, outputs image directly to the browser.
                                # $output is reset after a call to set_source();
       
        var $jpeg_quality;        # Quality of JPEG output (0 - 100).
                                # Default: 90

        var $org_width;            # If set_source() was called, and succeeded, returns the original width.
        var $org_height;        # Same as above for height

        # internal variables, not to be altered!
        var $i_image;
        var $i_src;
        var $i_isGD2;
       
        # Main function
        function imageresize() {
            # Initialize variables with default values
            $this->maintain_aspect = 1;
            $this->jpeg_quality = 90;
            $this->resize_ax = 1;

            $this->new_height = 0;
            $this->new_width = 0;

            $this->org_height = 0;
            $this->org_width = 0;

            $this->i_src = 0;
            $this->i_isGD2 = true;
        }

        # Returns true/false depending on success.
        function source($image_file) {
            # if file does not exist, return false
            if (!file_exists($image_file)) return false;
            # set image as active image
            $this->i_image = $image_file;

            # load the image set in i_image, and assign image resource to i_src.
            if ($this->int_loadimage()) {
                $this->org_width = imagesx($this->i_src);
                $this->org_height = imagesy($this->i_src);
                $this->output = "";

                return true;
            } else {
                # clear image on load failure
                $this->i_image = "";
                return false;
            }
        }

        # Resample loaded image. (requires GD 2.0.1 or later)
        function resample() {
            if (!$this->i_image) return false;
           
            # get the resource to the destination image
            if ($dest = $this->int_get_destimage()) {
                # copy the image from i_src and resample it, then place it on $dest.
                imagecopyresampled($dest, $this->i_src, 0, 0, 0, 0, $this->new_width, $this->new_height, $this->org_width, $this->org_height);
                # output the image
                $this->int_outputimage($dest);

                return true;
            } else {
                return false;
            }
        }

        # Pixel reesize loaded image. (requires GD 2.0.1 or later)
        function resize() {
            if (!$this->i_image) return false;

            # get the resource to the destination image
            if ($dest = $this->int_get_destimage()) {
                # copy the image from i_src and resize it, then place it on $dest.
                imagecopyresized($dest, $this->i_src, 0, 0, 0, 0, $this->new_width, $this->new_height, $this->org_width, $this->org_height);
                # output the image
                $this->int_outputimage($dest);

                return true;
            } else {
                return false;
            }
        }

        # Returns image resource on succes, otherwise false.
        function int_loadimage() {
            if (!$this->i_image) return false;
            # determine file type and load image
            switch ($this->int_imagetype()) {
                case ".jpg": $this->i_src = @imagecreatefromjpeg($this->i_image); break;
                case ".png": $this->i_src = @imagecreatefrompng($this->i_image); break;
            }

            return $this->i_src;
        }

        # Returns file extension of loaded image
        function int_imagetype() {
            if (!$this->i_image) return false;
            return strtolower(strrchr($this->i_image, "."));
        }

        # Returns image resource to destination image on success, otherwise false.
        function int_get_destimage() {
            if ($this->maintain_aspect) {
                if ($this->new_width > 0 || $this->new_height > 0) {
                    if ($this->resize_ax == 1) {
                        $ratio = ($this->new_width * 100) / $this->org_width;
                        $this->new_height =  floor($this->org_height * ($ratio / 100));
                    } else if ($this->resize_ax == 2) {
                        $ratio = ($this->new_height * 100) / $this->org_height;
                        $this->new_width =  floor($this->org_width * ($ratio / 100));
                    }
                } else if ($this->new_width < 0 || $this->new_height < 0) {
                    if ($this->new_width < 0) {
                        $width = abs($this->new_width) / 100;
                        $this->new_width = $this->org_width * $width;
                        $this->new_height = $this->org_height * $width;
                    }
                    if ($this->new_height < 0) {
                        $height = abs($this->new_height) / 100;
                        $this->new_height = $this->org_height * $height;
                        $this->new_width = $this->org_width * $height;
                    }
                }
            } else {
                if ($this->new_width < 0) $this->new_width = $this->org_width * (abs($this->new_width) / 100);
                if ($this->new_height < 0) $this->new_height = $this->org_height * (abs($this->new_height) / 100);
            }

            $dest = (!$this->i_isGD2) ? @imagecreate($this->new_width, $this->new_height):@imagecreatetruecolor($this->new_width, $this->new_height);
            return $dest;
        }

        # Outputs image from $dest
        function int_outputimage($dest) {
            # array with Content-Type's
            $mimes = array(".jpg" => "image/jpeg", ".png" => "image/png");
            # if image should be send directly to stdout, then send the appropriate header
            if (!$this->output) header("Content-Type: " . $mimes[$this->int_imagetype()]);

            # output the image
            switch ($this->int_imagetype()) {
                case ".jpg": ($this->output) ? imagejpeg($dest, $this->output, $this->jpeg_quality):imagejpeg($dest, "", $this->jpeg_quality); break;
                case ".png": ($this->output) ? imagepng($dest, $this->output):imagepng($dest); break;
            }

            # clear up resources
            imagedestroy($dest);
        }
    }

    /*
        Class    : imagethumbnail; extends imageresize
        Purpose  : Generate thumbnails of pictures and save thumnail for later and faster retreival.

        Functions :
                    get_thumbnail(string image, int width, int height[, bool resize_type = 0 [, bool regenerate = false]])
                        Get the filename of a thumbnail og "image".
                        Either width or height must be set, but not both. One *has* to be zero.
                        Whichever is set will have its opposite proportionally adjusted.
                        i.e.: if width = 100, the thumbnail will be 100 px wide and n px high (adjusted)
                       
                        If resize_type is 0, pixel resizing will be used, if 1 resampling will be used.
                        If regenerate is true the thumbnail will be generated even if it already exists.

                        Returns: filename of thumbnail on success, otherwise false.
    */
    class imagethumbnail extends imageresize {
        function get_thumbnail($image, $width, $height, $resize_type = 0, $regenerate = false) {
            if (!file_exists($image) || ($width > 0 && $height > 0)) return false;

            # make thumbnail filename
            $thumbnail_file = dirname($image) . "/tmb_" . basename($image);

            # if thumbnail does not exists or regenerate is true, then generate the thumbnail
            if (!@file_exists($thumbnail_file) || $regenerate) {
                # load image
                $this->source($image);
                # set output filename
                $this->output = $thumbnail_file;
                if ($width > 0) {
                    $this->new_width = $width;
                } else {
                    $this->new_height = $height;
                }

                # re -size/-sample image and save the image
                ($resize_type == 1) ? $this->resample():$this->resize();
            }

            # return the filename
            return $thumbnail_file;
        }
    }
?>
Avatar billede dero Nybegynder
09. januar 2004 - 16:51 #3
Når jeg gemmer den, hvad skal jeg så gemme den som, og hvilken adresse skal jeg så gå ind på, skal variablerne stå i adresselinjen??

Mvh
Anders
Avatar billede sylvesternielsen Nybegynder
09. januar 2004 - 17:02 #4
Du gemmer den som en hvilken som helst php fil..

Det du gør er at lave en
include("php filen");
i toppen af det dokument hvor du skal bruge det her..

For at bruge scriptet bør du kigge i de eksempler som medfølger.
Det er let at gå til..
Avatar billede sylvesternielsen Nybegynder
09. januar 2004 - 17:10 #5
Her er et eksempel på hvordan du laver en tumbnail
<?
//henter filen
include("class.imageresize.php");
//Indlæser klassen
$rz = new imageresize();
// sætter den til IKKE at have en proportioner
$rz->maintain_aspect = false;
//Kildefilen
$rz->source("test.jpg");
//sætter breden
$rz->new_width = 77;
//sætter højden
$rz->new_height = 103;
//vælg output fil
$rz->output("billeder/small/test.jpg");
//laver billedet
$rz->resample();
?>
Avatar billede dero Nybegynder
16. januar 2004 - 15:33 #6
Men jeg skal da også have angivet hvilket sted jeg vil have lortet uploadet og sådan??
Avatar billede sylvesternielsen Nybegynder
16. januar 2004 - 19:26 #7
Det nævnte du jo ikke noget om..
Den her kode skal ligges ind på billedet efter det er uploadet til serveren..

Men kan da lige smide et lille upload script her også..
Avatar billede dero Nybegynder
16. januar 2004 - 20:04 #8
nej nej.. Det var ikke sådan ment.. Jeg mente, at når man har uploadet det store billede, så skal man så køre det her script, som så danner et nyt billede (det lille billede), og uploader til en bestemt url... Men jeg fatter ikke helt hvordan??

Mvh
Anders
Avatar billede sylvesternielsen Nybegynder
16. januar 2004 - 21:36 #9
ok.. du uploader dit billede..

Billedet test.jpg har du lige uploadet til mappen pic.
så kører du den kode jeg har skrevet som så vil se sådan er ud hvis filen ligger i roden af dit bibliotek.

<?
//henter filen
include("class.imageresize.php");
//Indlæser klassen
$rz = new imageresize();
// sætter den til IKKE at have en proportioner
$rz->maintain_aspect = false;
//Kildefilen
$rz->source("./pic/test.jpg");
//sætter breden
$rz->new_width = 77;
//sætter højden
$rz->new_height = 103;
//vælg output fil
$rz->output("pic/small/test.jpg");
//laver billedet
$rz->resample();
?>

så laver den en lille kopi i mappen pic/small/
Avatar billede dero Nybegynder
23. januar 2004 - 16:11 #10
Jeg har prøvet.. Det virker ikke.. Kan det være fordi den skal have stien til min hjemmeside??

Mv
Anders
Avatar billede sylvesternielsen Nybegynder
23. januar 2004 - 16:13 #11
ja den skal have stien.
Avatar billede dero Nybegynder
23. januar 2004 - 16:45 #12
Hvor skal jeg angive den??
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester