Zend - The PHP Company




Resizing

Add Code


Image Resize (Uses ImageSource)  

Type: class
Added by: dhiggins
Entered: 18/04/2001
Last modified: 04/12/2000
Rating: - (fewer than 3 votes)
Views: 12322
This is a function that resizes an image using imagemagick.


<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  bol createThumbnail_ImageMagick($width, $height, $image_name, [$thumb_name], [$error])
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Creates a thumbnail of desired image.  JPEG only right now. Uses the image magick program.
//    Maintains aspect ration.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Parameter:
//        $width - Desired Image Width
//        $height - Desired image height
//        $image_name - Name of filename.  Must be absolute path.
//        $thumb_name - Name of thumbnail to be created.  Must be absolute.  Is optional, if it isn't set, new filename name is thumb_oldfilename.
//        &$error - Reference to error message.  Doesn't need to be returned since variable passed will be
//              changed in event of error.  Optional.  
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Return value:
//        true - upon successful thumbnail
//        false - upon failed thumbnail
//    
////////////////////////////////////////////////////////////////////////////////////////////////////////
function createThumbnail_ImageMagick($width$height$image_name$thumb_name=""$error="") {
    
// read image height and width
    
$info getimagesize($image_name);

    
// get images new size
    
list($new_width$new_height) = newSize($width$height$info[0], $info[1]);

    
// check if thumbnail name is set, if not prefix image name with thumb_
    
if ($thumb_name == "") {
        
$thumb_name "thumb_" basename($image_name);
    }

    
// check if the directory is attached to filename, if so we need to prefix thumb_name with a /
    
if (dirname($image_name) != "") {
        
$copy_str "cp $image_name " dirname($image_name) . "/$thumb_name";
    } else {
        
$copy_str "cp $image_name " dirname($image_name) . "$thumb_name";
    }

    
// copy image to thumbnail (Also escaping shells just to be sure)
    
exec(EscapeShellCmd($copy_str));

    
// call image magick (Also escaping shells just to be sure)
    
exec(EscapeShellCmd("mogrify -geometry " $new_height "x" $new_width "! $thumb_name"));

    return 
true;
}
?>


Usage Example




Rate This Script





Search



This Category All Categories