resize images on the fly using this image resizer

Let us talk about scripts, HTML, Perl, PHP, apache, etc.
User avatar
Stevyn
SysOp
Posts:1773
Joined:Mon Nov 09, 2009 10:03 am
Location:Japan
Contact:
resize images on the fly using this image resizer

Post by Stevyn » Mon Dec 21, 2009 6:24 am

source:

http://www.thepixelboxhomepage.com/image_resize.php

(page seems to be down...)

Image resizer

Proportionally resize images on the fly using this image resizer.

to use the image resizer use the following code.

// image = the root to the image file.
// w and h are max width and max height

<img src="image_resize.php?image=http://www.example.com/image.jpg&w=60&h=60" alt="image"/>
Example output
Normal 40x 40 pixels avatar
Resized to 60 x 60 pixels avatar
Resized to 20 x 20 pixels avatar

Allowed file types for resizing are .jpg, .jpeg, .gif and .png.
Code - image_resize.php

Code: Select all

<?php

//get the image variables from the url

//$image_source - the source of the image

$image_source = $_GET['image'];

//$max_width and $max_height are the maximum width and height of the image

$max_width    = $_GET['w'];
$max_height   = $_GET['h'];

//if there is an image

if($image_source!=""){

        //the parameters for the image

    $image_params = image_type($image_source);
    
      //creat the image resource
    
    $im = $image_params[1]($image_source);
    
      //width and height of the image
    
    $width = imagesx($im);
    $height = imagesy($im);
    
      //ratio to maximum width and height
      //the larger ratio is the one that needs to be resized
    
    $w_ratio = $width/$max_width;
    $h_ratio = $height/$max_height;
    
      //if the width ratio is larger
    
    if($h_ratio<$w_ratio){
      
        //resize the width to the max width
    
      $wid = $max_width;
      
        //resize the height proportionately
      
      $heig = floor($height / $w_ratio);
      $chng=1;
    }
    
        //if the height ratio is greater
    
    elseif($h_ratio>$w_ratio){
    
        //resize the height to the max height
    
      $heig = $max_height;
      
        //resize the width proportionately
      
      $wid = floor($width / $h_ratio);
      $chng=1;
    }
    
    //if the ratios are the same and the image needs to be smaller
    
    elseif(($h_ratio==$w_ratio) && ($height>$max_height)){
    
        //height is max height
    
      $heig = $max_height;
      
        //set width as a proportion using the height ratio
      
      $wid = floor($width / $h_ratio);
      $chng=1;
    
    
    }
    
    //if the ratios are the same and the image needs to be larger
    
    elseif(($h_ratio==$w_ratio) && ($height<$max_height)){
    
      //set the height and width as the max height and max width
    
      $heig = $max_height;
      $wid = $max_width;
      $chng=1;
    
    }
    
        //if the sizes needed resizing
    
    if($chng==1){
    
          //create a new image resource
    
      $resize_img = imagecreatetruecolor($wid,$heig);
      
          //fill the image with white
      $white = imagecolorallocate($resize_img,255,255,255);
      imagefilledrectangle($resize_img,0,0,$wid,$heig,$white);
      
          //copy the original image to the new image resource with the new size

      imagecopyresampled($resize_img,$im,0,0,0,0,$wid,$heig,$width,$height);
      
        //headers
      
      header("$image_params[0]");
      
        //send the image
      
      $image_params[2]($resize_img);
      
        //destroy the image
      
      image_destroy();
    }
    
    //if the image does not need resizing
    
    else{
    
        //headers
    
      header("$image_params[0]");
      
        //send the image
      
      $image_params[2]($im);
      
        //destroy the image
      
      image_destroy();
    }
}


/*
    Creates an array of image values used above
    $image_params[0] - the image headers
    $image_params[1] - Creates the image
    $image_params[2] - displays the image 
*/

function image_type($v){

        //find the filetype

    $type = strtolower(strrchr($v,"."));

        //depending what image file is used - different params are needed

    switch($type){
  
        case'.jpg':
        $image_params = array("Content-type:image/jpeg",
                              "imagecreatefromjpeg","imagejpeg");
        break;
        
        case'.jpeg':
        $image_params = array("Content-type:image/jpeg",
                              "imagecreatefromjpeg","imagejpeg");
        break;

        case'.gif':
        $image_params = array("Content-type:image/gif",
                              "imagecreatefromgif","imagegif");
        break;

        case'.png':
        $image_params = array("Content-type:image/png",
                              "imagecreatefrompng","imagepng");
        break;

    }
    
    //return the image paramaters array
    
    return($image_params);
}

?> 


Post Reply