本文共有两个文件 Upload.php
<? session_start(); require_once("../inc/resizeToFile.func.php"); $iconurl=$_FILES["userfile"]["name"]; $picture=basename($iconurl); $arr=explode(".",$picture); $ext=""; ini_set("display_errors",0); ?> <style type="text/css"> body,p,td{ font-size:9pt; } </style> <body bgcolor="#eeeeee"> <? if(isset($arr[count($arr)-1])) { $ext=$arr[count($arr)-1]; $ext=strtolower($ext); if($ext=="gif" or $ext=="jpg" or $ext=="jpeg") { //echo "你上传的文件格式是".$ext; }else{ echo "图片格式不正确<a href=\"javascript:history.back(-1);\">重来</a>"; exit(); } }else{ echo "图片格式不正确,<a href=\"javascript:history.back(-1);\">重来</a>"; exit(); } if(file_exists("../images/upload/small_".$picture)) { echo "请另选一张图片!<a href=\"javascript:history.back(-1);\">重来</a>"; exit(); } if(move_uploaded_file($_FILES["userfile"]["tmp_name"],"./".$picture)) { echo "上传成功! "; $_SESSION["uploaded_picture"]=$picture; if($ext=="jpg" or $ext=="jpeg"){ $im=imagecreatefromjpeg("./".$picture); }elseif($ext=="gif") { $im=imagecreatefromgif("./".$picture); }else{ exit(); } $width=imagesx($im); $height=imagesy($im); if($width>$height){ $small_width=80; $small_height=(80/$width)*$height;
$medium_width=120; $medium_height=(120/$width)*$height; }else{ $small_height=80; $small_width=(80/$height)*$width;
$medium_height=120; $medium_width=(120/$height)*$width; } if($width>500 || $height>500) { if($width>$height) {$large_width=500; $large_height=(500/$width)*$height;
}else{ $large_height=500; $large_width=(500/$height)*$width; } }else{ $large_width=$width; $large_height=$height; } //echo $width.",".$height; $toDir="../images/upload"; resizeToFile2($picture,$small_width,$small_height,$toDir."/small_".$picture,100); resizeToFile2($picture,$medium_width,$medium_height,$toDir."/medium_".$picture,100); resizeToFile2($picture,$large_width,$large_height,$toDir."/large_".$picture,100); unlink($picture); echo $picture." <a href=\"javascript:history.back(-1);\">更换图片</a>"; }
?> </body>
../inc/resizeToFile.func.php
<?php /* resizeToFile resizes a picture and writes it to the harddisk * * $sourcefile = the filename of the picture that is going to be resized * $dest_x = X-Size of the target picture in pixels * $dest_y = Y-Size of the target picture in pixels * $targetfile = The name under which the resized picture will be stored * $jpegqual = The Compression-Rate that is to be used */
function resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual) {
/* Get the dimensions of the source picture */ $picsize=getimagesize("$sourcefile");
$source_x = $picsize[0]; $source_y = $picsize[1]; $source_id = imageCreateFromJPEG("$sourcefile");
/* Create a new image object (not neccessarily true colour) */ $target_id=imagecreatetruecolor($dest_x, $dest_y);
/* Resize the original picture and copy it into the just created image object. Because of the lack of space I had to wrap the parameters to several lines. I recommend putting them in one line in order keep your code clean and readable */
$target_pic=imagecopyresampled($target_id,$source_id, 0,0,0,0, $dest_x,$dest_y, $source_x,$source_y);
/* Create a jpeg with the quality of "$jpegqual" out of the image object "$target_pic". This will be saved as $targetfile */
imagejpeg ($target_id,"$targetfile",$jpegqual);
return true;
}
function resizeToFile2 ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual) {
$picsize=getimagesize("$sourcefile"); $source_x = $picsize[0]; $source_y = $picsize[1]; $arr=explode(".",$sourcefile); $ext=""; if(isset($arr[count($arr)-1])) { $ext=$arr[count($arr)-1]; $ext=strtolower($ext); } if($ext=="jpg" or $ext=="jpeg"){ $source_id = imageCreateFromJPEG("$sourcefile"); }elseif($ext=="gif"){ $source_id =imagecreatefromgif("$sourcefile"); } $target_id=imagecreatetruecolor($dest_x, $dest_y); $target_pic=imagecopyresampled($target_id,$source_id, 0,0,0,0, $dest_x,$dest_y, $source_x,$source_y);
imagejpeg ($target_id,"$targetfile",$jpegqual); return true; } ?>
|