Newsgroup: it.comp.www.php
Titolo: Ridimensionare un'immagine

Salve a tutti, ho un problema da cui non riesco a venire fuori.
Ho fatto uno script in php che sceglie casualmente un'immagine da una
determinata cartella e la fa visualizzare.
Ora, questo script lo posso anche utilizzare in un tag <img>. Vorrei ora
far si che passando allo script due variabili, $h e $w, esso mi
ridimensioni l'immagine all'interno del tag <img>.
Mi spiego meglio: se lo script viene richiamato in questo modo: <img
src="script.php?pic=random"> l'immagine viene visualizzata comunque. Per
far questo io uso questo comando:
Location: $slika
dove $slika è l'url dell'immagine.
Devo usare le librerire GD per far si che le immagini assumano le dim che
gli passo scrivendo <img src="script.php?pic=rand&w=100&h=100">?
Grazie,
Liuk
-- 
Message sent from: 
http://www.fanwars.it
 
IP ADDRESS: 80.182.250.34 
TIME: 18-04-2003 14:53

<?php
//////////////////////////////////////////////////////
// tnp.php           //
//////////////////////////////////////////////////////
// creates a thumbnail PNG of a given JPEG image //
// (c)2002 Bluegoldfish - All Rights reserved  //
// Author: Chris Morgan       //
// Version 1.01g         //
//////////////////////////////////////////////////////
// This program is designed to be called from an  //
// <img .. > tag in an HTML page. The parameters  //
// that must be passed are...      //
//  image  - the image to be processed   //
//  tnsize - the size of the thumbnail   //
//             //
// E.g.           //
//  <img src="tn.php?image=big.jpg&tnsize=150"> //
//             //
// This should produce a 150x150 thubnail from the //
// original image big.jpg       //
// If either of the parameters are missing, the //
// image file can't be located, or the thumbnail //
// size is invalid then an error image is   //
// generated.          //
//////////////////////////////////////////////////////
// A typical use of this program would be to  //
// generate thumbnails on-the-fly with a link to //
// the original image, e.g. ...     //
// <a href="big.jpg"><img src="tn.php?big.jpg  //
//  &tnsize=150"></a>       //
//////////////////////////////////////////////////////
// For further information, advice or constructive //
// comments please contact 
p...@bluegoldfish.com
  //
//////////////////////////////////////////////////////
// check if the 'image' parameter has been given
if (!isset($image)){ // if not generate an error image
 $img = ImageCreate(100, 100);
 $red = ImageColorAllocate($img, 255, 0, 0);
 $yellow = ImageColorAllocate($img, 255, 255, 0);
 ImageString($img, 5, 20, 20, "No image", $yellow);
 ImageString($img, 5, 20, 40, "name", $yellow);
 ImageString($img, 5, 20, 60, "given!", $yellow);
 ImagePNG($img);
 ImageDestroy($img);
 exit();
}
// check if the 'tnsize' parameter has been given
if (!isset($tnsize)) { // if not generate an error image
 $img = ImageCreate(100, 100);
 $red = ImageColorAllocate($img, 255, 0, 0);
 $yellow = ImageColorAllocate($img, 255, 255, 0);
 ImageString($img, 5, 20, 20, "No thumb", $yellow);
 ImageString($img, 5, 20, 40, "size", $yellow);
 ImageString($img, 5, 20, 60, "given!", $yellow);
 ImagePNG($img);
 ImageDestroy($img);
 exit();
}
// ensure that $tnsize is an integer
$tnsize = (integer) $tnsize;
// check that the size asked for is sensible
if (($tnsize<20) or ($tnsize>300)){ // if not generate an error image
 $img = ImageCreate(100, 100);
 $red = ImageColorAllocate($img, 255, 0, 0);
 $yellow = ImageColorAllocate($img, 255, 255, 0);
 ImageString($img, 5, 20, 20, "Bad", $yellow);
 ImageString($img, 5, 20, 40, "size", $yellow);
 ImageString($img, 5, 20, 60, "given!", $yellow);
 ImagePNG($img);
 ImageDestroy($img);
 exit();
}
// attempt to load the big image
if (!$bigimage = @ImageCreateFromJPEG($image)) { // if loading fails
 $img = ImageCreate(100, 100);     // generate an error
 $red = ImageColorAllocate($img, 255, 0, 0);  // image
 $yellow = ImageColorAllocate($img, 255, 255, 0);
 ImageString($img, 5, 20, 20, "Image", $yellow);
 ImageString($img, 5, 20, 40, "not", $yellow);
 ImageString($img, 5, 20, 60, "found!", $yellow);
 ImagePNG($img);
 ImageDestroy($img);
 exit();
}
// all seems to be OK so start the processing
// create the new image using the tnsize parameter
$tnimage = ImageCreate($tnsize, $tnsize);
// allocate the background colour for the thumbnail
$darkblue = ImageColorAllocate($tnimage, 0, 0, 127);
// set the background as transparent
ImageColorTransparent($tnimage,$darkblue);
// get the parameters of the big image
$sz = GetImageSize($image);
// load our internal variables
$x = $sz[0]; // big image width
$y = $sz[1]; // big image height
// find the larger dimension
if ($x>$y) { // if it is the width then
 $dx = 0;     // the left side of the new image
 $w = $tnsize;    // the width of the new image
 $h = ($y / $x) * $tnsize; // the height of the new image
 $dy = ($tnsize - $h) / 2; // the top of the new image
}else{ // if the height is larger then
 $dy = 0;     // the top of the new image
 $h = $tnsize;    // the height of the new image
 $w = ($x / $y) * $tnsize; // the width of the new image
 $dx = ($tnsize - $w) / 2; // the left edgeof the new image
}
// copy the resized version into the thumbnal image
ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);
// generate the PNG image
ImagePNG($tnimage);
// release the memory used by the thumbnail image
ImageDestroy($tnimage);
// release the memory used by the original big image
ImageDestroy($bigimage);
// all done!
?>
--
-
 Best Regards
 --------------------------------------
 L'informatica oggi, non
 riguarda solo il computer,
 ma č un modo di vevere!
 * Nicholas Negroponte *
 ---------------------------------------
 WEB:  
www.froid.it
 ICQ:    25876612
 Fax:    178 220 4409
 -------------------------------------

 
Avevo pronto uno script per questo.
Questa è la parte finale, controlla se lo script è chimato con parametri 
w (=largezza) e h(=altezza) del tipo immagine.php?w=100&h=200
Se non viene passato nulla si assume che w=399 e h=215. L'immagine viene 
comunque riscalata (anche con il default).
Sono necessarie le GD2 (o almeno io ho queste installate).
*********************************
if (!isset($_REQUEST['w']))
   $w=399;
else $w=$_REQUEST['w'];
if (!isset($_REQUEST['h']))
   $h=215;
else $h=$_REQUEST['h'];
$imout=@imagecreatetruecolor ($w,$h);
imagecopyresampled ($imout,$appoImage,0,0,0,0,$w,$h,399,215);
//output finale
imagepng ($imout);
********************************

 // creates a thumbnail PNG of a given JPEG image //
Grazie, sono riuscito ad implementarlo per benino nel mio script! :D
-- 
Message sent from: 
http://www.fanwars.it
 
IP ADDRESS: 80.182.249.92 
TIME: 18-04-2003 17:16