phpBB2 : Redimensionnement des images !

Discussion dans 'Web, design' créé par L@rgo_XIII, 22 Septembre 2003.

Statut de la discussion:
Fermée.
  1. Offline
    Script PHP tiré du site PHP.net, une correction par rapport au code original est l'utilisation de imageCreateTrueColor au lieu de imagecreate....

    Attention, PHP doit être au moins de la version 4.3 je crois (il faut appeler la fonction gd_info dans un script et voir si elle répond et ce qu'elle répond !....


    Code:
    function image_resize_jpeg( $img_source, $img_dest, $img_quality, $size_limit )
    {
      if ( !file_exists( $img_source ) )
      {
        return (1); //file not found return 1
      }
    
      // increase the $size_limit variable by 1 as it is going to be chopped by 1 below
      $size_limit++;
    
      //load the original image and put its hieght/width in $img_info
      $img_info = getimagesize ( $img_source ); 
    
      //$img_info ARRAY KEY
      //0 = width
      //1 = hieght
      //2 = image type
      //3 = hight and width string
      $orig_height = $img_info[1]; //source hieght from $img_info
      $orig_width = $img_info[0]; //source width from $img_info
    
      $jpegQuality = $img_quality; //quality of the JPG
    
      if(($orig_width > $size_limit) || ($orig_height > $size_limit)) //make sure the image isnt already resized
      { 
        if ( $orig_height > $orig_width )
          $scaledown = $orig_height;
        else
          $scaledown = $orig_width;
    
        $newscale = $size_limit / $scaledown; //set the new scale size
    
        //calculate the new aspect ratio
        $new_w = (int)abs($orig_width * $newscale);
        $new_h = (int)abs($orig_height * $newscale);
    
        //create the blank limited-palette image
        $base_image = imageCreatetruecolor($new_w, $new_h);
    
        //convert and save it to temp.jpg
        imagejpeg($base_image, 'temp.jpg');
        imagedestroy($base_image);
    
        //get the image pointer to the temp jpeg
        $image = imageCreateFromJpeg('temp.jpg');
    
        // get the image pointer to the original image
    //    $imageToResize = imageCreateFromJpeg("./$img_source");
        $imageToResize = imageCreateFromJpeg("$img_source");
    
        //resize the original image over temp.jpg
        // NOTE: of you have GD2, you could also use imageCopyResampled
        //imageCopyResized($image, $imageToResize, 0, 0, 0, 0, $new_w, $new_h, $orig_width, $orig_height);
    	imageCopyResampled($image, $imageToResize, 0, 0, 0, 0, $new_w, $new_h, $orig_width, $orig_height);
    
        $new_image = imageCreatetruecolor($new_w - 1, $new_h - 1);
        imagecopy ($new_image, $image, 0, 0, 0, 0, $new_w - 1, $new_h - 1);
    
        //create the resized image
        imageJpeg($new_image, "$img_dest", $jpegQuality); //image destination
    
        imagedestroy($image);
        imagedestroy($new_image);
        imagedestroy($imageToResize);
    
        unlink('temp.jpg'); //del the temp image file
    
        return (2); //completed successfully
      }
      else
      {
        return(3); //errors occured
      }
    }
    TheFornicator, 26 Septembre 2003
    #21
Statut de la discussion:
Fermée.