Class dimiourgias thumbs

Μια περιοχή όπου τα μέλη μας μπορούν να βάζουν τα PHP scripts που έχουν φτιάξει και θέλουν να τα μοιραστούν με τα υπόλοιπα μέλη.

Συντονιστές: Super-Moderators, WebDev Moderators, PHP Moderators

Απάντηση
AlexanderTheGr
Δημοσιεύσεις: 153
Εγγραφή: 11 Ιούλ 2003 04:22
Επικοινωνία:

Class dimiourgias thumbs

Δημοσίευση από AlexanderTheGr » 21 Ιαν 2006 01:58

I class einai xrisimi gia thn dimiourgia thumbs. Dil kanei resize analogika.
Emeis aplos thetoume 2 oria (megisto mikos kai megisto ypsos) kai to teliko proorismo tis eikonas


The class

Κώδικας: Επιλογή όλων

<?
class CreateThumps &#123; 

        var $final_width; // megisto platos tis telikis eikonas 
        var $final_height; // megisto yspos tis telikis eikonas 
        // ypologismos diastaseon tis telikis eikonas 
        // den prepei na kseperna ta oria  final_width kai final_height 
       
        function create_size&#40;$width, $height, $max_width, $max_height&#41; &#123; 

                //ypologismos analogion telikwn diastaseon me tin arxiki eikona 
                $width_ratio  = $max_width / $width; 
                $height_ratio = $max_height / $height; 

                // Exoume tessereis periptoseis                  
                  
                if &#40;&#40;$height > $max_height&#41; && &#40;$width > $max_width&#41; && &#40;$height < $width&#41;&#41; &#123; 
                       // Proti periptosi&#58; 
                       // Kai oi 2 diastaseis tis eikonas einai megalyteres apo ta iria pou thesame 
                       // Alla to mikos>ypsos 
                        $this->final_height = $max_height; 
                        $this->final_width  = ceil&#40;$height_ratio * $width&#41;; 
                        if &#40;$this->final_width > $max_width&#41; &#123; 
                                $widthratio = $max_width/$this->final_width; 
                                $this->final_width = $max_width; 
                                $this->final_height  = ceil&#40;$widthratio * $this->final_height&#41;; 
                        &#125; 
               &#125; elseif &#40;&#40;$height > $max_height&#41; && &#40;$width > $max_width&#41; && &#40;$height > $width&#41;&#41; &#123; 
                       // Deteyri periptosi&#58; 
                       // Kai oi 2 diastaseis tis eikonas einai megalyteres apo ta iria pou thesame 
                       // Alla to mikos<ypsos 
                        $this->final_width = $max_width; 
                        $this->final_height  = ceil&#40;$width_ratio * $height&#41;; 
                        if &#40;$this->final_height > $max_height&#41; &#123; 
                                $heightratio = $max_height/$this->final_height; 
                                $this->final_height = $max_height; 
                                $this->final_width  = ceil&#40;$heightratio * $this->final_width&#41;; 
                        &#125; 
                &#125; elseif &#40;&#40;$height > $max_height&#41; && &#40;$width <= $max_width&#41;&#41; &#123; 
                       //Triti Periptosi&#58; 
                       //An to ypsos tis eikonas eimai megalyteri apo to irio 
                       // Kai to platos einai iso i mikrotero apo to irio  
                        $this->final_width  = ceil&#40;$height_ratio * $width&#41;; 
                        $this->final_height = $max_height; 
                &#125; elseif &#40;&#40;$height <= $max_height&#41; && &#40;$width > $max_width&#41;&#41; &#123; 
                       //Tetarti Periptosi&#58; 
                       //An to platos tis eikonas eimai megalyteri apo to irio 
                       // Kai to ypsos einai iso i mikrotero apo to irio 
                        $this->final_height = ceil&#40;$width_ratio * $height&#41;; 
                        $this->final_width  = $max_width; 
                &#125; 

                $this->width     = $width; 
                $this->max_width = $max_width; 
                $this->height    = $height; 
                $this->max_height= $max_height; 
        &#125; 
        function create_thump&#40;$filenamefullpath, $file_name, $temp_image&#41; &#123; // Apothikeysi telikis eikonas me tis telikes diastaseis 

                 // Elegxos an prepei na ginei resize 
                 // An mia ek ton diastaseon ksepernaei ta oria pou thesame                
                 if &#40;!&#40;&#40;$this->width <= $this->max_width&#41; && &#40;$this->height <= $this->max_height&#41;&#41;&#41; &#123; // Prepei na ginei resize 
                        $newim  = imagecreate&#40;$this->final_width, $this->final_height&#41;; 
                        $pinfo = pathinfo&#40;strtolower&#40;$file_name&#41;&#41;; 
                        if &#40;$pinfo&#91;"extension"&#93; == "jpg"&#41; &#123;   // An i eikona einai jpg  einai dynato to resize tis eikonas. 
                                $im = imagecreatefromjpeg&#40;$temp_image&#41;; 
                        &#125; 
                        elseif &#40;$pinfo&#91;"extension"&#93; == "gif"&#41; &#123;  // An i eikona einai gif den einai dynato to resize tis eikonas. 
                                header&#40;"Location&#58; error.php?error=gifimage"&#41;; 
                                exit; 
                                // $im = imagecreatefromgif&#40;$temp_image&#41;; 
                        &#125; 
                        elseif &#40;$pinfo&#91;"extension"&#93; == "bmp"&#41; &#123;  // An i eikona einai bmp den einai dynato to resize tis eikonas. 
                                header&#40;"Location&#58; error.php?error=gifimage"&#41;; 
                                exit; 
                        &#125; 
                        imagecopyresized&#40;$newim, $im, 0, 0, 0, 0, $this->final_width, $this->final_height, $this->width, $this->height&#41;; 
                        if &#40;$pinfo&#91;"extension"&#93; == "jpg"&#41; &#123;
                                if   &#40;file_exists&#40;$filenamefullpath.".jpg"&#41;&#41; unlink&#40;$filenamefullpath.".jpg"&#41;; 
                                imagejpeg&#40;$newim, $filenamefullpath.".jpg"&#41;; 
                        &#125; elseif &#40;$pinfo&#91;"extension"&#93; == "gif"&#41; &#123; 
                                if   &#40;file_exists&#40;$filenamefullpath.".gif"&#41;&#41; unlink&#40;$filenamefullpath.".gif"&#41;; 
                                imagegif&#40;$newim, $filenamefullpath.".gif"&#41;; 
                        &#125; 
                        imagedestroy&#40;$newim&#41;; 
                        imagedestroy&#40;$im&#41;; 
                &#125; else &#123; // den xriazete na ginei resize 

                        $pinfo = pathinfo&#40;strtolower&#40;$file_name&#41;&#41;; 
                        if &#40;file_exists&#40;$filenamefullpath.".gif"&#41;&#41; unlink&#40;$filenamefullpath.".gif"&#41;; 
                        if &#40;file_exists&#40;$filenamefullpath.".jpg"&#41;&#41; unlink&#40;$filenamefullpath.".jpg"&#41;; 
                        if &#40;file_exists&#40;$filenamefullpath.".bmp"&#41;&#41; unlink&#40;$filenamefullpath.".bmp"&#41;; 
                        copy&#40;$temp_image, $filenamefullpath.".".$pinfo&#91;"extension"&#93;&#41;; 
                &#125; 
        &#125; 
       
&#125;
?>
Ena paradeigma xrhsis tis class:

Stin arxi dimiourgoume tin forma gia to upload tis eikonas

Κώδικας: Επιλογή όλων

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>file.html</title>
</head>

<body>

<form method="POST" enctype="multipart/form-data" action="thumb.php">
	<input type="file" name="image" size="20">
	<input type="submit" value="Submit" name="submit">
	<input type="reset" value="Reset" name="reset"> <br>
	<input type="text" name="width" size="4"> Limit Width<br>
	<input type="text" name="height" size="4"> Limit Height</form>
</body>

</html>
Stin synexeia to script pou tha diaxeirizete tin class (thumb.php)

Κώδικας: Επιλογή όλων

<?
//thumb.php
include&#40;"inc/classes.php"&#41;;
$temp_image = $_FILES&#91;'image'&#93;&#91;'tmp_name'&#93;;
$file_name = $_FILES&#91;'image'&#93;&#91;'name'&#93;;
$file_type = $_FILES&#91;'image'&#93;&#91;'type'&#93;;
$file_size = $_FILES&#91;'image'&#93;&#91;'size'&#93;;
$result    = $_FILES&#91;'image'&#93;&#91;'error'&#93;;
$ext = strrchr&#40;$file_name,'.'&#41;;

if &#40;is_uploaded_file&#40;$_FILES&#91;'image'&#93;&#91;'tmp_name'&#93;&#41;&#41; &#123;
       if &#40;$file_size == "0"&#41; &#123;
               header&#40;"Location&#58; error.php?error=filenotexist"&#41;;
               exit;
       &#125;

       if &#40;!@getimagesize&#40;$temp_image&#41;&#41; &#123;
               header&#40;"Location&#58; error.php?error=filenotimage"&#41;;
              exit;
       &#125;

      $size = @getimagesize&#40;$temp_image&#41;;
      $thumbfilename = "thumb"; //filename tou thumb xoris to ext &#40;jpg, gif ktl&#41;
      $max_height  = $_POST&#91;"height"&#93;;  // Thetoume to megisto irio ypsous
      $max_width   = $_POST&#91;"width"&#93;; // Thetoume to megisto irio platous
      $height          = $size&#91;1&#93;;
      $width           = $size&#91;0&#93;;
      $create_size = new CreateThumps;
      $create_size->create_size&#40;$width, $height, $max_width, $max_height&#41;; // ypolofismos telikon diastaseon
      $create_size->create_thump&#40;"img/thumbs/thumb", $file_name, $temp_image&#41;; //dimiourgia kai apothykeysi tis telikis eikonas
      echo "<img border=\"0\" src=\"img/thumbs/".$thumbfilename.".jpg\" width=\"$max_width\" height=\"$max_height\">";
&#125;
?>
Stin class tin sozoume se ena arxeio me to onoma classes.php kai tin topothetoume se mia dir me to onoma inc

Episis tha prepei na diourgisoume tin directory "img/thumb/" kai na tis dosoume permitions 713.

Have a nice day

Άβαταρ μέλους
ThyClub
Honorary Member
Δημοσιεύσεις: 5312
Εγγραφή: 17 Νοέμ 2003 00:21
Τοποθεσία: Hell's Kitchen
Επικοινωνία:

Class dimiourgias thumbs

Δημοσίευση από ThyClub » 21 Ιαν 2006 03:15

I loved it :victory:

Άβαταρ μέλους
Rapid-eraser
WebDev Moderator
Δημοσιεύσεις: 6851
Εγγραφή: 05 Απρ 2003 17:50
Τοποθεσία: Πειραιάς
Επικοινωνία:

Class dimiourgias thumbs

Δημοσίευση από Rapid-eraser » 21 Ιαν 2006 10:01

Av kai exo mia mikroenstasi stov tropo pou exei graftei sivolika 8a sta8w movo se mia simbouli :)

avti gia

Κώδικας: Επιλογή όλων

$newim  = imagecreate&#40;$this->final_width, $this->final_height&#41;;
va givei mia xrisi tis fuctnion

Κώδικας: Επιλογή όλων

$newim  = imagecreatetruecolor&#40;$this->final_width, $this->final_height&#41;;
Movo pou xriazete GD 2.0.1 kai megaliteri
(episis dev 8a doulepsei me gifs) alla exei
poli kalitera apotelesmata xromatos sto veo canvas.

Episis ta simia me ta diadoxia if if if ... kai if elseif elseif isos va mporousav va graftouv ligo pio apla
me eva select case.

Telos dev eida compresion ratio sta jpeg :) isos 8a eprepe va iparxei
kai mia pros8iki gia afto, kamia fora simferei va simpiestei n eikova
mazi me to resize :)
kamia fora eivai pio apotelesmatiko/8emito afto :P

Isos mia orea pros8iki va ntav to fixed resize me basi mia diastasi.


**PS: ta permisions pou xriazete va divoume eivai movo sto user kai sto word to group mporei va paramivei afoba 0 :P
Avaloga me to pos eivai setarismevi n php prepei va divoume to chmod kai dev eivai pavakia n lisi.
px av n php trexei me isapi module tote avagastika 8elei word dikeomata, av briskete sav cgi 8elei sigoura user dikeomata aka ...

AlexanderTheGr keep up :)
Cu, Rapid-eraser, Tα αγαθά copies κτώνται.
Love is like oxygen, You get too much you get too high
Not enough and you're gonna die, Love gets you high

Άβαταρ μέλους
Basilakis
PHP Moderator
Δημοσιεύσεις: 8574
Εγγραφή: 17 Νοέμ 2003 13:03
Τοποθεσία: Womans' Brain
Επικοινωνία:

Class dimiourgias thumbs

Δημοσίευση από Basilakis » 23 Ιαν 2006 19:21

Very very nice... Kante to tutorial!

Απάντηση

Επιστροφή στο “PHP τα δικά μας scripts”

Μέλη σε σύνδεση

Μέλη σε αυτήν τη Δ. Συζήτηση: Δεν υπάρχουν εγγεγραμμένα μέλη και 0 επισκέπτες