Zend - The PHP Company




Resizing

Add Code


Another Resize Script with Border Option  

Type: code fragment
Added by: liquidkernel
Entered: 09/05/2002
Last modified: 05/12/2001
Rating: **** (7 votes)
Views: 12945
This script will open an image ($image), resize it to the values you specify ($max_height and $max_width), then if you want, add a border ($addborder). It uses ImageCopyResampled from GD 2 as ImageCopyResized looks too pixelated. Set $image_quality to anything between 0 and 100.


<?php
// Set a few variables
$image "/home/web/images/original.jpg";
$newimage "/home/web/images/new.jpg";
$image_quality 80;
$addborder 1;
$max_height 200;
$max_width 300;

// Main code
$src_img ImageCreateFromJpeg($image);
$orig_x ImageSX($src_img); 
$orig_y ImageSY($src_img);

$new_y $max_height;
$new_x $orig_x/($orig_y/$max_height);  

if (
$new_x $max_width) {
    
$new_x $max_width;
    
$new_y $orig_y/($orig_x/$max_width);
}

$dst_img ImageCreateTrueColor($new_x,$new_y); 
ImageCopyResampled($dst_img$src_img0000$new_x$new_y$orig_x$orig_y); 

if (
$addborder == 1) {
    
// Add border
    
$black ImageColorAllocate($dst_img000); 
    
ImageSetThickness($dst_img1);
    
ImageLine($dst_img00$new_x0$black);
    
ImageLine($dst_img000$new_y$black);
    
ImageLine($dst_img$new_x-10$new_x-1$new_y$black);
    
ImageLine($dst_img0$new_y-1$new_x$new_y-1$black);
}
    
ImageJpeg($dst_img$newimage$image_quality); 
ImageDestroy($src_img); 
ImageDestroy($dst_img); 
?>


Usage Example




Rate This Script





Search



This Category All Categories