00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 function kjwimage_create_thumbnail($infile, $outfile, $outx, $outy,
00021 &$characteristics = array()) {
00022 assert('strrpos($outfile, ".") !== false');
00023 $outext = strtolower(substr($outfile, strrpos($outfile, '.') + 1));
00024 if ($outext == 'jpeg')
00025 $outext = 'jpg';
00026 assert('$outext == "gif" || $outext == "jpg" || $outext == "png"');
00027
00028
00029 $characteristics = array();
00030
00031
00032 if (($inimg = @imagecreatefrompng($infile))) {
00033 $inext = 'png';
00034 } elseif (($inimg = @imagecreatefromjpeg($infile))) {
00035 $inext = 'jpg';
00036 } elseif (($inimg = @imagecreatefromgif($infile))) {
00037 $inext = 'gif';
00038 } else {
00039 return null;
00040 }
00041
00042
00043 $characteristics['input.extension'] = $inext;
00044
00045
00046 $inx = imagesx($inimg);
00047 $iny = imagesy($inimg);
00048 $inscale = (float)$inx / (float)$iny;
00049 if ($outx !== null && $outy !== null) {
00050 $outscale = (float)$outx / (float)$outy;
00051 } elseif ($outx !== null && $outy === null) {
00052 $outscale = $inscale;
00053 $outy = $outx / $outscale;
00054 } elseif ($outx === null && $outy !== null) {
00055 $outscale = $inscale;
00056 $outx = $outy * $outscale;
00057 } else {
00058 assert(0);
00059 }
00060
00061
00062 if (!($outimg = @imagecreatetruecolor($outx, $outy))) {
00063 imagedestroy($inimg);
00064 return null;
00065 }
00066
00067
00068 if ($outext == 'png') {
00069 imagesavealpha($outimg, true);
00070 imagealphablending($outimg, false);
00071 imagefill($outimg, 0, 0, imagecolorallocatealpha($outimg, 0xff, 0x00, 0xff, 127));
00072 } else {
00073 imagefill($outimg, 0, 0, imagecolorallocate($outimg, 0xff, 0xff, 0xff));
00074 }
00075
00076
00077 $characteristics['input.width'] = $inx;
00078 $characteristics['input.height'] = $iny;
00079
00080
00081 if (abs($outscale - $inscale) < 0.1) {
00082 $success = imagecopyresampled($outimg, $inimg, 0, 0, 0, 0, $outx, $outy, $inx, $iny);
00083
00084 } elseif ($outscale < $inscale) {
00085 $newy = (int)((float)$outx / $inscale);
00086 $success = imagecopyresampled($outimg, $inimg, 0, (int)(($outy - $newy) / 2), 0, 0, $outx, $newy, $inx, $iny);
00087
00088 } else {
00089 $newx = (int)((float)$outy * $inscale);
00090 $success = imagecopyresampled($outimg, $inimg, (int)(($outx - $newx) / 2), 0, 0, 0, $newx, $outy, $inx, $iny);
00091 }
00092
00093 imagedestroy($inimg);
00094
00095
00096 if ($success) {
00097 if ($outext == 'jpg') {
00098 $success = imagejpeg($outimg, $outfile);
00099 } elseif ($outext == 'png') {
00100 $success = imagepng($outimg, $outfile);
00101 } elseif ($outext == 'gif') {
00102 $success = imagegif($outimg, $outfile);
00103 } else {
00104 assert('false');
00105 }
00106 }
00107
00108 imagedestroy($outimg);
00109 return $success;
00110 }
00111
00112 ?>