Avatar billede webco_dk Nybegynder
13. april 2009 - 13:53 Der er 3 kommentarer

filnavn ved resize

Hej jeg har dette script der uploader og gemmer et givent billede på
serveren, hvorefter den resizer billedet og gemmer det med et 10 ciffert
filnavn. dette filnavn vil jeg meget gerne gemme i en database men hvordan
for jeg dette ud i en variael $bileldenavn så jeg kan gemme det i
databasen????

de to filer er her:






imageresize.inc:

_____________________________

<?php
/**
* @author Dodit Suprianto
* Email: d0dit@yahoo.com
* Website: http://doditsuprianto.com
*
* Nama file: ImageResize.inc
*
* This class to resize an image from bigger to smaller size.
* Supporting PNG, JPG, and GIF image format.
*
* if you chose $proportional=true, it means that width or height image is
proportional, example:
* $r = new Resize("test.jpg", 0, 500, true); height image will be 500
pixel and width will be proportional
* $r = new Resize("test.jpg", 500, 0, true); width image will be 500
pixel and height will be proportional
*
* if you chose $proportional=false, it means the width and height image
will be customizable, example:
* $r = new Resize("test.jpg", 500, 500, true); it forces the image size
will be 500 pixel width and 500 pixel height
*
*/

        class Resize
        {
                private $file_source;
                private $width_resize;
                private $height_resize;
                private $proportional;

                public function __construct($file_source, $width_resize, $height_resize,
$proportional)
                {
                        $this->file_source = $file_source;
                        $this->width_resize = $width_resize;
                        $this->height_resize = $height_resize;
                        $this->proportional = $proportional;
                }

                public function setProportional($proportional)
                {
                        $this->proportional = $proportional;
                }

                public function setFileSource($file_source)
                {
                        $this->file_source = $file_source;
                }

                public function setHeightResize($height_resize)
                {
                        $this->height_resize = $height_resize;
                }

                public function setWidthResize($width_resize)
                {
                        $this->width_resize = $width_resize;
                }

                private function MemoryUsage()
                {
                        $imageInfo    = getimagesize($this->file_source);
                        $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] *
$imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);

                        $memoryLimit = (int) ini_get('memory_limit')*1048576;
                        if ((memory_get_usage() + $memoryNeeded) > $memoryLimit)
                        ini_set('memory_limit', ceil((memory_get_usage() + $memoryNeeded +
$memoryLimit)/1048576).'M');
                }

                public function ImageResize()
                {
                        $this->MemoryUsage();
                        if ( $this->height_resize <= 0 && $this->width_resize <= 0 ) return false;

                        $info = getimagesize($this->file_source);
                    $image = '';

                        $final_width = 0;
                        $final_height = 0;
                        list($width_old, $height_old) = $info;

                        if ($this->proportional)
                        {
                                $proportion = $width_old / $height_old;

                                if ( $this->width_resize > $this->height_resize &&
$this->height_resize != 0)
                                {
                                        $final_height = $this->height_resize;
                                        $final_width = $final_height * $proportion;
                                }
                                elseif ( $this->width_resize < $this->height_resize &&
$this->width_resize != 0)
                                {
                                        $final_width = $this->width_resize;
                                        $final_height = $final_width / $proportion;
                                }
                                elseif ( $this->width_resize == 0 )
                                {
                                        $final_height = $this->height_resize;
                                        $final_width = $final_height * $proportion;
                                }
                                elseif ( $this->height_resize == 0)
                        {
                                        $final_width = $this->width_resize;
                                        $final_height = $final_width / $proportion;
                                }
                                else
                                {
                                        $final_width = $this->width_resize;
                                        $final_height = $this->height_resize;
                                }
                        }
                        else
                        {
                                $final_width = ($this->width_resize <= 0) ? $this->width_resize_old :
$this->width_resize;
                                $final_height = ($this->height_resize <= 0) ? $this->height_resize_old
: $this->height_resize;
                        }

                        switch ( $info[2] )
                        {
                                case IMAGETYPE_GIF:
                                        $image = imagecreatefromgif($this->file_source);
                                        echo $image;
                                break;
                                case IMAGETYPE_JPEG:
                                        $image = imagecreatefromjpeg($this->file_source);
                                        echo $image;
                                break;
                                case IMAGETYPE_PNG:
                                        $image = imagecreatefrompng($this->file_source);
                                        echo $image;
                                break;
                                default:
                                        return false;
                        }

                        $image_resized = imagecreatetruecolor( $final_width, $final_height );

                        imagecolortransparent($image_resized,
imagecolorallocate($image_resized, 0, 0, 0) );
                        imagealphablending($image_resized, false);
                        imagesavealpha($image_resized, true);

                        imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width,
$final_height, $width_old, $height_old);

                        switch ( $info[2] )
                        {
                                case IMAGETYPE_GIF:
                                        imagegif($image_resized, time().".gif", 100);
                                break;
                                case IMAGETYPE_JPEG:
                                        imagejpeg($image_resized, time().".jpg", 100);
                                break;
                                case IMAGETYPE_PNG:
                                        imagepng($image_resized, time().".png", 100);
                                break;
                                default:
                                        return false;
                        }
                        return true;
                }
        }


?>


__________________________________________



test.php
__________________________________________

if ($_POST['submit'])

        {
                require_once("ImageResize.inc");
                $propo = "yes";
                if ($propo == "yes")
                {
                        // proportional
                        $r = new Resize($_FILES['photo']['tmp_name'], $_POST['width'], 0, true);
                        // or $r = new Resize($_FILE['photo']['tmp_name'], 0, $_POST['height'],
true);
                } else
                {
                        // Force the image size
                        $r = new Resize($_FILES[photo][tmp_name], $_POST[width],
$_POST[height], false);
                }

                $r->ImageResize();



        }


_______________________________________________
Avatar billede olebole Juniormester
13. april 2009 - 16:48 #1
<ole>

- og hvor får du det 10-ciffrede navn fra?

/mvh
</bole>
Avatar billede jakobdo Ekspert
14. april 2009 - 14:54 #2
Det kommer herfra ole:

case IMAGETYPE_GIF:
                                        imagegif($image_resized, time().".gif", 100);
                                break;
                                case IMAGETYPE_JPEG:
                                        imagejpeg($image_resized, time().".jpg", 100);
                                break;
                                case IMAGETYPE_PNG:
                                        imagepng($image_resized, time().".png", 100);
                                break;

Og svaret er altså: time()
Avatar billede olebole Juniormester
14. april 2009 - 21:46 #3
Dooohhhh ... sæ'følsomt! den havde jeg da helt overset! Tak, Jakob  ;o)

Så er det jo bare i switch'en, du skal indsætte i MySQL  =)
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