What's the best way to automatically resize all images in the server to 100 x 100?
no matter what I want the images to be 100 x 100 pixels.

any suggestion?

PS: downloading images to local hard drive and resizing with windows program is not an option

thank you

    I have been curious about PHP's image support for a while, but I have no need to know that stuff yet. I suppose you know about: http://se2.php.net/manual/en/ref.image.php (search for resize)

    I would first read the directory where the images are, put them in a array. Then run the array. I would guess that you to open each image and run the PHP function that resizes the images. (Given that images work the same as "ordinary" files)

    Anyway, don't now this helps you, but I needed to get this off of my chest.

    PS. Sounds like you want to create a thumbnail function: http://www.icant.co.uk/articles/phpthumbnails/ (I felt lucky)

      I just wrote a tiny class for that... you'll have fun reading it, it's coded in French ! 😛

      <?php
      class NZAU_Redimensionner_Image {
      	function __construct($intL, $intH, $strFormat_Destination = 'png', $bolContour_Vide = true, $arrFond_Destination = array(0, 0, 0), $bolTransparente = false) {
      		$this->arrDimensions			= array($intL, $intH);
      		$this->bolContour_Vide			= $bolContour_Vide;
      		$this->strFormat_Destination	= $strFormat_Destination;
      		$this->arrFond_Destination		= $arrFond_Destination;
      		$this->bolTransparente			= $bolTransparente;
      	}
      	function retournerErreur($strFonction, $strErreur){
      		echo __CLASS__ . '-&gt;' . $strFonction . '() : ' . $strErreur . '.<br />' . "\n";
      		return false;
      	}
      	function retournerRedimensionner($strFichier_Origine, $strFichier_Destination) {
      		if(!is_readable($strFichier_Origine)) {
      			return $this->retournerErreur(__FUNCTION__, 'impossible de lire le fichier d\'origine');
      		} elseif(!is_writable(dirname($strFichier_Destination))) {
      			return $this->retournerErreur(__FUNCTION__, 'impossible d\'&eacute;crire dans le r&eacute;pertoire de destination');
      		}
       		switch(exif_imagetype($strFichier_Origine)) {
       			case IMAGETYPE_GIF:
      				$resImage_Origine	= imagecreatefromgif($strFichier_Origine);
      				break;
      			case IMAGETYPE_JPEG:
      				$resImage_Origine	= imagecreatefromjpeg($strFichier_Origine);
      				break;
      			case IMAGETYPE_PNG:
      				$resImage_Origine	= imagecreatefrompng($strFichier_Origine);
       				break;
       			case IMAGETYPE_WBMP:
      				$resImage_Origine	= imagecreatefromwbmp($strFichier_Origine);
       				break;
       			case IMAGETYPE_XBM:
       				$resImage_Origine	= imagecreatefromxbm($strFichier_Origine);
       				break;
      			default:
      				return $this->retournerErreur(__FUNCTION__, 'le type de l\'image d\'origine n\'est pas valide.');
      				break;
      		}
      		$intRatio	= max($this->arrDimensions[0], $this->arrDimensions[1]) / max(imagesx($resImage_Origine), imagesy($resImage_Origine));
      		$arrDimensions_Destination	= array(round(imagesx($resImage_Origine) * $intRatio), round(imagesy($resImage_Origine) * $intRatio));
      		$resImage_Destination		= imagecreate((($this->bolContour_Vide) ? $arrDimensions_Destination[0] : 100), (($this->bolContour_Vide) ? $arrDimensions_Destination[1] : 100));
      		$resCouleur					= imagecolorallocate($resImage_Destination, $this->arrFond_Destination[0], $this->arrFond_Destination[1], $this->arrFond_Destination[2]);
      		if($this->bolTransparente) {
      			imagecolortransparent($resImage_Destination, $resCouleur);
      		}
      		if(!@imagecopyresampled($resImage_Destination, $resImage_Origine, (($this->bolContour_Vide) ? 0 : (round(($this->arrDimensions[0] - $arrDimensions_Destination[0]) / 2))), (($this->bolContour_Vide) ? 0 : (round(($this->arrDimensions[1] - $arrDimensions_Destination[1]) / 2))), 0, 0, $arrDimensions_Destination[0], $arrDimensions_Destination[1], imagesx($resImage_Origine), imagesy($resImage_Origine))) {
      			return $this->retournerErreur(__FUNCTION__, 'impossible de redimensionner l\'image');
      		} else {
      			switch($this->strFormat_Destination) {
      				case 'gd':
      					return imagegd($resImage_Destination, $strFichier_Destination);
      					break;
      				case 'gd2':
      					return imagegd2($resImage_Destination, $strFichier_Destination);
      					break;
      				case 'jpeg':
      					return imagejpeg($resImage_Destination, $strFichier_Destination, 100);
      					break;
      				case 'png':
      					return imagepng($resImage_Destination, $strFichier_Destination);
      					break;
      				case 'wbmp':
      					return imagewbmp($resImage_Destination, $strFichier_Destination);
      					break;
      				case 'xbm':
      					return imagexbm($resImage_Destination, $strFichier_Destination);
      					break;
      				default:
      					return $this->retournerErreur(__FUNCTION__, 'format de destination inconnu');
      					break;
      			}
      		}
      	}
      }
      $objNZAU_RI	= new NZAU_Redimensionner_Image(100, 100);
      $objNZAU_RI->retournerRedimensionner('C:/Temp/moi4.jpg', 'C:/Temp/moi4_redim.png');
      ?>

      When you initiate the class, you have to/can define these parameters :

      $intL : the (maximum) width of the thumbnail
      $intH : the (maximum) height of the thumbnail
      $strFormat_Destination = 'png' : the format of the thumbnail
      $bolContour_Vide = true : if true, the thumbnail won't be exactly W x H, either the width or the height will be smaller, if false, the thumbnail will have a width and a height you specified with $intL and $intH
      $arrFond_Destination = array(0, 0, 0) : the background color
      $bolTransparente = false : if true, the image will be transparent (if you use the png format)

      Then, call the retournerRedimensionner() function with two parameters : the first, the "origin image", the second, "the destination image".

      That's it.

        hi.. thank you...
        i will take a look tomorrow morning in the office.

        regards 🙂

          ManWithNoName wrote:

          I have been curious about PHP's image support for a while, but I have no need to know that stuff yet. I suppose you know about: http://se2.php.net/manual/en/ref.image.php (search for resize)

          I would first read the directory where the images are, put them in a array. Then run the array. I would guess that you to open each image and run the PHP function that resizes the images. (Given that images work the same as "ordinary" files)

          Anyway, don't now this helps you, but I needed to get this off of my chest.

          PS. Sounds like you want to create a thumbnail function: http://www.icant.co.uk/articles/phpthumbnails/ (I felt lucky)

          nice ! thank you

            What is the images aren't square to start with? I.e. most images are landscape or portrait, do you really want to squash them to 100x100?

            I'd highly recommend using Imagemagick (if your server supports it), especially the mogrify function:
            http://www.imagemagick.org/Usage/basics/#mogrify

            I use imagemagick with the command:

            		system("convert -quality 80 -thumbnail 54x img_in.jpg img_out.jpg);
            

              (my class doesn't square them... have a look at the $intRatio variable...)

                Write a Reply...