<?php
define(”op_to_file”, 1); // output to file
define(”op_output”, 2); // output to browser
define(”op_not_keep_scale”, 4); // free scale
define(”op_best_resize_width”, 8); // scale to width
define(”op_best_resize_height”, 16); // scale to height
define(”cm_default”,0); // clipping method: default
define(”cm_left_or_top”,1); // clipping method: left or top
define(”cm_middle”,2); // clipping method: middle
define(”cm_right_or_bottom”,3); // clipping method: right or bottom
/**
* vxresize
*
* @param string $srcfile source file
* @param string $srcfile destination file
* @param int $dstw width of destination file (pixel)
* @param int $dsth height of destination file (pixel)
* @param int $option options, you add multiple options like 1+2(or 1|2), this utilize function 1 & 2
* 1: default,output to file 2: output to browser stream 4: free scale
* 8:scale to width 16:scale to height
* @param int $cutmode clipping method 0: default 1: left or top 2: middle 3: right or bottom
* @param int $startx start x axis (pixel)
* @param int $starty start y axis (pixel)
* @return array return[0]=0: ok; return[0] error code return[1] string: error description
*/function vxresize($srcfile, $dstfile) {
$option=op_to_file ;
$cutmode=cm_left_or_top ;
$dstw=22; //输出文件宽
$dsth=22; //输出文件高
$startx=0;
$starty=0;
$img_type = array(1=>”gif”, 2=>”jpeg”, 3=>”png”);
$type_idx = array(”gif”=>1, “jpg”=>2, “jpeg”=>2, “jpe”=>2, “png”=>3);if (!file_exists($srcfile)) {
return array(-1, “source file not exists: $srcfile.”);
}$path_parts = @pathinfo($dstfile);
$ext = strtolower ($path_parts["extension"]);
if ($ext == “”) {
return array(-5, “can’t detect output image’s type.”);
}
$func_output = “image” . $img_type[$type_idx[$ext]];
if (!function_exists ($func_output)) {
return array(-2, “function not exists for output:$func_output.”);
}
$data = @getimagesize($srcfile);
$func_create = “imagecreatefrom” . $img_type[$data[2]];
if (!function_exists ($func_create)) {
return array(-3, “function not exists for create:$func_create.”);
}
$im = @$func_create($srcfile);
$srcw = @imagesx($im);
$srch = @imagesy($im);
$srcx = 0;
$srcy = 0;
$dstx = 0;
$dsty = 0;
if ($option & op_best_resize_width) {
$dsth = round($dstw * $srch / $srcw);
}
if ($option & op_best_resize_height) {
$dstw = round($dsth * $srcw / $srch);
}
$fdstw = $dstw;
$fdsth = $dsth;
if ($cutmode != cm_default) { // clipping method 1: left or top 2: middle 3: right or bottom
$srcw -= $startx;
$srch -= $starty;
if ($srcw*$dsth > $srch*$dstw) {
$testw = round($dstw * $srch / $dsth);
$testh = $srch;
} else {
$testh = round($dsth * $srcw / $dstw);
$testw = $srcw;
}
switch ($cutmode) {
case cm_left_or_top: $srcx = 0; $srcy = 0; break;
case cm_middle: $srcx = round(($srcw - $testw) / 2);
$srcy = round(($srch - $testh) / 2); break;
case cm_right_or_bottom: $srcx = $srcw - $testw;
$srcy = $srch - $testh;
}
$srcw = $testw;
$srch = $testh;
$srcx += $startx;
$srcy += $starty;
} else {
if (!($option & op_not_keep_scale)) {
if ($srcw*$dsth>$srch*$dstw) {
$fdsth=round($srch*$dstw/$srcw);
$dsty=floor(($dsth-$fdsth)/2);
$fdstw=$dstw;
} else {
$fdstw=round($srcw*$dsth/$srch);
$dstx=floor(($dstw-$fdstw)/2);
$fdsth=$dsth;
}
$dstx=($dstx<0)?0:$dstx;
$dsty=($dstx<0)?0:$dsty;
$dstx=($dstx>($dstw/2))?floor($dstw/2):$dstx;
$dsty=($dsty>($dsth/2))?floor($dsth/s):$dsty;
}
}
if( function_exists(”imagecopyresampled”) and
function_exists(”imagecreatetruecolor”) ){
$func_create = “imagecreatetruecolor”
$func_resize = “imagecopyresampled”
} else {
$func_create = “imagecreate”
$func_resize = “imagecopyresized”
}
$newim = @$func_create($dstw,$dsth);
$black = @imagecolorallocate($newim, 0,0,0);
$back = @imagecolortransparent($newim, $black);
@imagefilledrectangle($newim,0,0,$dstw,$dsth,$black);
@$func_resize($newim,$im,$dstx,$dsty,$srcx,$srcy,$fdstw,$fdsth,$srcw,$srch);
if ($option & op_to_file) {
switch ($type_idx[$ext]) {
case 1:
case 3:
@$func_output($newim,$dstfile);
break;
case 2:
@$func_output($newim,$dstfile,100);
break;
}
}
if ($option & op_output) {
if (function_exists(”headers_sent”)) {
if (headers_sent()) {
return array(-4, “http already sent, can’t output image to browser.”);
}
}
header(”content-type: image/” . $img_type[$type_idx[$ext]]);
@$func_output($newim);
}
@imagedestroy($im);
@imagedestroy($newim);
return array(0, “ok”);
}
?>