Newsgroup: it.comp.www.php
Titolo: [lungo] Help con elaborazione immagini

Mi aiutate a controllare questo script?
           // upload del file ricevuto nella cartella 'immagini/'
    move_uploaded_file($_FILES['immagine']['tmp_name'], 'immagini/' .
$_FILES['immagine']['name']);
          // rinomino il file sostituendo gli spazi con '_'
          // utilizzando il titolo inserito nel form
          // qui non sono sicuro di aver fatto bene
    $titolo = ereg_replace ([[:space:]], '_', $_POST["titolo"]);
    $titolo.='.jpg';         // aggiungo l'estensione
    rename ($_FILES['immagine']['name'], $titolo);
            // controllo la dimensione dell'immagine principale
            // per ridimensionarla nel caso fosse troppo grande
            // qui i primi dubbi, va bene l'indirizzo dell'immagine?
     $immagine = 'immagini/'.$titolo;
     list($width, $height) = getimagesize($immagine);
            // controllo
    if ($width>800) || ($heigt>600) {
        $w=800;  // altezza
        $h=600;  // lunghezza
        $img = imagecreatetruecolor($w, $h);
        $image = ImageCreateFromJpeg($immagine);
        $imagedata = getimagesize($immagine);
        imagecopyresized ($big, $image, 0, 0, 0, 0, $w, $h, $imagedata[0],
$imagedata[1]);
            // in questo modo sovrascrive l'immagine?
        imagejpeg($big, "immagini/".$titolo);
        imagedestroy($big);
    }
           // adesso creo la thumbnail.
           // posso tranquillamente riutilizzare gli stessi nomi
           // per le variabili?
        $w=150;  // altezza
        $h=150;  // lunghezza
        $img = imagecreatetruecolor($w, $h);
        $image = ImageCreateFromJpeg($immagine);
        $imagedata = getimagesize($immagine);
        imagecopyresized ($small, $image, 0, 0, 0, 0, $w, $h, $imagedata[0],
$imagedata[1]);
            // basta cambiare il nome al file?
        imagejpeg($small, "immagini/thumb_".$titolo);
        imagedestroy($small);
questo è quanto ma sto studiando ancora un controllo che rispetti le
proporzioni delle immagini...
ogni suggerimento è gradito
Luciano Amodio

 
come vedi è molto + semplice .... poi la qualità delle immagini è 
incredibilmente superiore alle gd e viene creata in una frazione di tempo
per maggiori info 
http://www.imagemagick.org/www/utilities.html
negli ultimi server redhat i file di iMagemagick potrebbere essere in 
/usr/local   invece di /usr/local/bin
ciao
Andrea

Il web server è IIS, ho chiesto e dice che non può installarle...

 
lol
oggi mi sento buono anche con i winari
beccati sta classe(la uso anchio quando non posso con imagemagick =)
è un pò modifcato rispetto all'originale cmq vedi tu
file imageResize.class.php
<?php
/* php image resizing class
About:
This is a php image resizing class. I wrote this class to make it easier to
properly resize images for my website. Before I wrote this class I manually
resized and compressed all images I put on my website. Right now I can 
make php
do it and still maintain proper proportions etc.
Right now it only works with the Jpeg image format, but I might make it 
work with
more formats soon (Jpeg is all I really need).
HowTo:
Using this image class is simple and straightforward, there are 3 steps 
which
have to take place for the class to work:
-  Load a image with the loadImage($path) function
-  Resize the image using any of the resize*(*) functions
-  Save a image with the saveImage($path, $quality=75)
-  Clear the memory of the image by using clearImage()
I have included a small example at the bottem of this file
License:
Do whatever you want with it aslong as you leave this statement intact
and in the same file as the class definition.
Contact:
Got any questions, requests, changes or whatever, please contact me.
Greetings,
Timothy Kanters
< 
tph.k...@wanadoo.nl
 >
< 
http://www.TimothyKanters.com
 >
*/
class Image
{
     var $image = FALSE;
     function loadImage($path)
     /*
     This function loads the image you want to resize.
     The function will return TRUE on a succes and FALSE on failure
     */
     {
         if ( $this->image ) {
             imagedestroy($this->image);
         }
         if ( !($this->image = imageCreateFromJpeg($path)) ) {
             return FALSE;
         } else {
             return TRUE;
         }
     }
     function saveImage($path, $quality=75)
     /*
     Save the image, to either a new or an already existing file.
     The quality setting determines the quality of the image (0 to 100).
     The function will return TRUE on a succes and FALSE on failure
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $fp = fopen($path, "w");
         if (!$fp) {
             return FALSE;
         } else {
             fclose($fp);
             if (!imageJpeg($this->image, $path, $quality)) {
                 return FALSE;
             } else {
                 return TRUE;
             }
         }
     }
	function saveImage_anteprima($path, $quality=75,$string)
     /*
     Save the image, to either a new or an already existing file.
     The quality setting determines the quality of the image (0 to 100).
     The function will return TRUE on a succes and FALSE on failure
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $fp = fopen($path, "w");
         if (!$fp) {
             return FALSE;
         } else {
             fclose($fp);
				$txt_c = ImageColorAllocate ($this->image, 0, 0, 255);
				$txt_c1 = ImageColorAllocate ($this->image, 255, 255, 255);
				imagefilledrectangle ($this->image, 0, 0, 50, 12, $txt_c);
				ImageString($this->image, 3, 10, 0, $string, $txt_c1);
             if (!imageJpeg($this->image, $path, $quality)) {
                 return FALSE;
             } else {
                 return TRUE;
             }
         }
     }
     function saveImagePNG($path)
     /*
     Save the image, to either a new or an already existing file.
     The quality setting determines the quality of the image (0 to 100).
     The function will return TRUE on a succes and FALSE on failure
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $fp = fopen($path, "w");
         if (!$fp) {
             return FALSE;
         } else {
             fclose($fp);
             if (!imagepng($this->image, str_replace(".jpg", "", 
$path).".png")) {
                 return FALSE;
             } else {
                 return TRUE;
             }
         }
     }
 
     function clearImage()
     /*
     Clears the memory used for the image (if it is loaded)
     This function is required to ensure that there are no memory leaks!
     you have to run this function after your resizing is complete.
     The function always returns TRUE
     */
     {
         if ($this->image) {
             imagedestroy($this->image);
             $this->image = FALSE;
             return TRUE;
         } else {
             return TRUE;
         }
     }
     function getWidth()
     /*
     returns the width of the loaded image
     */
     {
         if (!$this->image) {
             return 0;
         }
         return imageSX($this->image);
     }
     function getHeight()
     /*
     returns the Height of the loaded image
     */
     {
         if (!$this->image) {
             return 0;
         }
         return imageSY($this->image);
     }
     function resizeW($newWidth)
     /*
     resizes an image accordingly to fit the newWidth and remain proportions
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $oldWidth = imageSX($this->image);
         $oldHeight = imageSY($this->image);
         $newHeight = ($oldHeight / ($oldWidth / $newWidth));
         $imageNew = ImageCreateTrueColor($newWidth, $newHeight);
         imageCopyResampled($imageNew, $this->image, 0, 0, 0, 0, 
$newWidth, $newHeight, $oldWidth, $oldHeight);
         imageDestroy($this->image);
         $this->image = $imageNew;
         return TRUE;
     }
     function resizeH($newHeight)
     /*
     resizes an image accordingly to fit the newHeight and remain 
proportions
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $oldWidth = imageSX($this->image);
         $oldHeight = imageSY($this->image);
         $newWidth = ($oldWidth / ($oldHeight / $newHeight));
         $imageNew = ImageCreateTrueColor($newWidth, $newHeight);
         imageCopyResampled($imageNew, $this->image, 0, 0, 0, 0, 
$newWidth, $newHeight, $oldWidth, $oldHeight);
         imageDestroy($this->image);
         $this->image = $imageNew;
         return TRUE;
     }
     function resizeWH_palette($maxWidth, $maxHeight, $stretch=FALSE)
     /*
     resizes an image accordingly to fit the maxWidth and maxHeight.
     By default it retains proportions. If however stretch is set to TRUE it
     will stretch the image to the maxWidth and maxHeight.
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $oldWidth = imageSX($this->image);
         $oldHeight = imageSY($this->image);
         $newWidth= $maxWidth;
         $newHeight= $maxHeight;
         if (!$stretch) {
             $ratio = $oldWidth / $oldHeight;
             if (($maxWidth  / $maxHeight) < $ratio) {
                 $newHeight = ($oldHeight / ($oldWidth / $maxWidth));
             } else {
                 $newWidth = ($oldWidth / ($oldHeight / $maxHeight));
             }
         }
         $imageNew = ImageCreateTrueColor($newWidth, $newHeight);
         imageCopyResampled($imageNew, $this->image, 0, 0, 0, 0, 
$newWidth, $newHeight, $oldWidth, $oldHeight);
         imageDestroy($this->image);
         $this->image = $imageNew;
         return TRUE;
     }
function resizeWH($maxWidth, $maxHeight, $stretch=FALSE)
     /*
     resizes an image accordingly to fit the maxWidth and maxHeight.
     By default it retains proportions. If however stretch is set to TRUE it
     will stretch the image to the maxWidth and maxHeight.
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $oldWidth = imageSX($this->image);
         $oldHeight = imageSY($this->image);
         $newWidth= $maxWidth;
         $newHeight= $maxHeight;
         if (!$stretch) {
             $ratio = $oldWidth / $oldHeight;
             if (($maxWidth  / $maxHeight) < $ratio) {
                 $newHeight = ($oldHeight / ($oldWidth / $maxWidth));
             } else {
                 $newWidth = ($oldWidth / ($oldHeight / $maxHeight));
             }
         }
         $imageNew = ImageCreateTrueColor($newWidth, $newHeight);
         imageCopyResampled($imageNew, $this->image, 0, 0, 0, 0, 
$newWidth, $newHeight, $oldWidth, $oldHeight);
         imageDestroy($this->image);
         $this->image = $imageNew;
         return TRUE;
     }
     function resizeCropped($width, $height)
     /*
     resizes an image to the width and height arguments.
     If needed parts of the image will be cropped to remain proportions
     ** note this is a function I quickly hacked in, I hope it works ok,
        but I haven't had a chance to properly test it, or to review the 
code I wrote here yet
     */
     {
         if (!$this->image) {
             return FALSE;
         }
         $oldWidth = imageSX($this->image);
         $oldHeight = imageSY($this->image);
         $ratioW = $oldWidth / $width;
         $ratioH = $oldHeight / $height;
         if ($ratioH > $ratioW) {
             // some parts from the height will have to be cut off
             $newWidth = $oldWidth;
             $newHeight = $height * $ratioW;
             $srcX = 0;
             $srcY = +($oldHeight - $newHeight) / 2;
         } else {
             // some parts from the width will have to be cut off
             $newWidth = $width * $ratioH;
             $newHeight = $oldHeight;
             $srcX = +($oldWidth - $newWidth) / 2;
             $srcY = 0;
         }
         $imageNew = ImageCreateTrueColor($newWidth, $newHeight);
         imageCopyResized($imageNew, $this->image, 0, 0, $srcX, $srcY, 
$oldWidth, $oldHeight, $oldWidth, $oldHeight);
         imageDestroy($this->image);
         $this->image = $imageNew;
         // Now we are actually going to resample the image to the 
correct size
         $oldWidth = $newWidth;
         $oldHeight = $newHeight;
         $newWidth = $width;
         $newHeight = $height;
         $imageNew = ImageCreateTrueColor($newWidth, $newHeight);
         imageCopyResampled($imageNew, $this->image, 0, 0, 0, 0, 
$newWidth, $newHeight, $oldWidth, $oldHeight);
         imageDestroy($this->image);
         $this->image = $imageNew;
         return TRUE;
     }
}
?>
PER UTILIZZARLA
include ("imageResize.class.php");
$picture = new Image;
$picture->loadImage("percorso_file_iniziale");
$picture->resizeW(larghezza_finale)
$picture->saveImage("percorso_file_finale", compressione_jpg);
NB
compressione_jpg= valore 1-100 che identifica la qualità dell'immagine 
..... consigliato 75/80

 è un pò modifcato rispetto all'originale cmq vedi tu
ti ringrazio per il post.
sicuramente sarà utile.
il punto è che volevo imparare a farlo da me...
capisco però che controllare il mio codice sia una grossa perdita di
tempo...
studierei allora il tuo ma le classi non le capisco ancora...
ti chiedo dunque se puoi darmi qualche link dove si parli di classi per
newbbie
grazie
Lux