Zend - The PHP Company




Resizing

Add Code


Resize Image on the fly  

Type: code fragment
Added by: nkapetanakos
Entered: 09/11/2006
Last modified: 02/11/2005
Rating: - (fewer than 3 votes)
Views: 6849
PHP script that resizes on the fly an image file (jpg, gif, png). Two parameters (image name and size).


<?php
//resizeimage.php
//PHP script that resizes on the fly an image file
$filename $_REQUEST['image'];
$size $_REQUEST['size'];
//Get the file's extension (e.g. jpg, gif, png)
$ext substr($filename, -33);

if (
$ext == 'jpg' || $ext == 'JPG')
{
//$percent = 0.10;
// Content type
header('Content-type: image/jpeg');

// Get new sizes
if ($size == 'small')
{
  
$width 90;
  
$height 47;
}
elseif (
$size == 'medium')
{
  
$width 221;
  
$height 116;
}

list(
$width$height) = getimagesize($filename);

if (
$size == 'small')
{
  
$newwidth 90;
  
$newheight 47;
}
elseif (
$size == 'medium')
{
  
$newwidth 221;
  
$newheight 116;
}


// Load
$thumb imagecreatetruecolor($newwidth$newheight);
$source imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb$source0000$newwidth$newheight$width$height);

// Output
imagejpeg($thumb);
}
elseif (
$ext == 'gif' || $ext == 'GIF')
{
//$percent = 0.10;
// Content type
header('Content-type: image/gif');

// Get new sizes
if ($size == 'small')
{
  
$width 90;
  
$height 47;
}
elseif (
$size == 'medium')
{
  
$width 221;
  
$height 116;
}

list(
$width$height) = getimagesize($filename);

if (
$size == 'small')
{
  
$newwidth 90;
  
$newheight 47;
}
elseif (
$size == 'medium')
{
  
$newwidth 221;
  
$newheight 116;
}

// Load
$thumb imagecreatetruecolor($newwidth$newheight);
$source imagecreatefromgif($filename);

// Resize
imagecopyresized($thumb$source0000$newwidth$newheight$width$height);

// Output
imagegif($thumb);

}
elseif (
$ext == 'png' || $ext=='PNG')
{
//$percent = 0.10;
// Content type
header('Content-type: image/png');

// Get new sizes
if ($size == 'small')
{
  
$width 90;
  
$height 47;
}
elseif (
$size == 'medium')
{
  
$width 221;
  
$height 116;
}

list(
$width$height) = getimagesize($filename);

if (
$size == 'small')
{
  
$newwidth 90;
  
$newheight 47;
}
elseif (
$size == 'medium')
{
  
$newwidth 221;
  
$newheight 116;
}

// Load
$thumb imagecreatetruecolor($newwidth$newheight);
$source imagecreatefrompng($filename);

// Resize
imagecopyresized($thumb$source0000$newwidth$newheight$width$height);

// Output
imagepng($thumb);

}


?> 


Usage Example


See the example


Rate This Script





Search



This Category All Categories