Zend - The PHP Company




Thumbnails

Add Code


Simple Resizing Function  

Type: application
Added by: igoehr
Entered: 18/02/2004
Last modified: 03/12/2003
Rating: - (fewer than 3 votes)
Views: 7068
Simple Image Resizer 2 Methods available: 1.) percentual Resizing 2.) Just giving new sizes


<?php
/*
####################################################################################
# Image Resizer Function -  resize.func.php
# use resize($image,$thumb_path,$mode);
#
# vers 1.0 2004-02-18    written by Ingmar G�hr, ig@studiored.de, www.studio-red.de.
####################################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#####################################################################################
# Config:
#
*/
$w=100// Width of Thumbnail
$h=100// Height of Thumbnail
/*
#
#####################################################################################
*/
function percent($p$w)
    {
    return (real)(
100 * ($p $w));
    }

function 
unpercent($percent$whole)
    {
    return (real)((
$percent $whole) / 100);
    }


function 
resize($img,$dest_path,$mode){

// Initialization

SetType($mode,   'integer');
SetType($w,      'integer');
SetType($h,     'integer');
SetType($img,     'string' );
SetType($dest_path,    'string');
SetType($error,        'boolean');

global 
$w,$h;

// Make sure the file exists...
if (!file_exists($img))
    {
    return 
$error;
    exit();
    }

// If the user defined a type to use.
if (!isset($type))
    {
    
$ext explode('.'$img);
    
$ext $ext[count($ext)-1];
        switch(
strtolower($ext))
            {
            case 
'jpeg'  :
                
$type 'jpg';
            break;
            default :
                
$type $ext;
            break;
            }
    }

// Create the image...
switch (strtolower($type))
    {
    case 
'jpg':
        
$tmpe imagecreatefromjpeg($img);
    break;

    case 
'gif':
        
$tmpe = @imagecreatefromgif($img);
    break;

    case 
'png':
        
$tmpe = @imagecreatefrompng($img);
    break;

    default:
        return 
$error;
        exit();
    break;
    }

if (
$tmpe)
    {
    
// Resize it

    
$ow  imagesx ($tmpe);    // Original image width
    
$oh  imagesy ($tmpe);    // Original image height

    
if ($mode)
        {
        
// Just smash it up to fit the dimensions
        
$nw $w;
        
$nh $h;
        }
    else
        {
        
// Make it proportional.
        
if ($ow $oh)
            {
            
$nw  $w;
            
$nh unpercent(percent($nw$ow), $oh);
            }
        else if (
$oh $ow)
            {
            
$nh $h;
            
$nw unpercent(percent($nh$oh), $ow);
            }
        else
            {
            
$nh $h;
            
$oh $w;
            }
        }

    
$out1 imagecreate($nw$nh);
    
imagecopyresized($out1$tmpe0000$nw$nh$ow$oh);
    
imagedestroy($tmpe);
    }
else
    {
    return 
$error;
    exit;
    }


if (
$out1)
    {
    switch (
strtolower($type))
        {
        case 
'jpg':
            
ImageJPEG($out1,$dest_path."/tn_".$img,90);
        break;

        case 
'gif':

            
ImageGIF($out1,$dest_path."/tn_".$img,90);
        break;

        case 
'png':
            
ImagePNG($out1,$dest_path."/tn_".$img,90);
        break;
        }
    
imagedestroy($out1);

    }
else
    {
    return 
$error;
    }
 }
?>


Usage Example


<?php
require("resize.func.php");
resize("test.jpg",getcwd(),0);
?>


Rate This Script





Search



This Category All Categories