Voting

: three plus three?
(Example: nine)

The Note You're Voting On

Warren
16 years ago
This is a function I wrote for making thumbnails - it will accept a source *image resource* and destination *path*, plus the max dimension and whether the thumbnail should be square.

I chose to accept a resource as the source to make it a bit more efficient to create multiple sizes. For example:

<?php
$src_im
=@imagecreatefromjpeg($pathtofile);
$large = resize($src_im,$destination_large,1024);
@
imagedestroy($src_im);
$medium = resize($large,$destination_medium,500);
@
imagedestroy($large);
$small = resize($medium,$destination_small,125);
@
imagedestroy($medium);
$square = resize($small,$destination_square,75,TRUE);
@
imagedestroy($small);
@
imagedestroy($square);

function
resize($src_im, $dpath, $maxd, $square=false) {
$src_width = imagesx($src_im);
$src_height = imagesy($src_im);
$src_w=$src_width;
$src_h=$src_height;
$src_x=0;
$src_y=0;
if(
$square){
$dst_w = $maxd;
$dst_h = $maxd;
if(
$src_width>$src_height){
$src_x = ceil(($src_width-$src_height)/2);
$src_w=$src_height;
$src_h=$src_height;
}else{
$src_y = ceil(($src_height-$src_width)/2);
$src_w=$src_width;
$src_h=$src_width;
}
}else{
if(
$src_width>$src_height){
$dst_w=$maxd;
$dst_h=floor($src_height*($dst_w/$src_width));
}else{
$dst_h=$maxd;
$dst_w=floor($src_width*($dst_h/$src_height));
}
}
$dst_im=@imagecreatetruecolor($dst_w,$dst_h);
@
imagecopyresampled($dst_im, $src_im, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
@
imagejpeg($dst_im,$dpath);
return
$dst_im;
}
?>

<< Back to user notes page

To Top