Kod: Tümünü seç
<?php
function fileext($path) {
$info = pathinfo($path);
return $info["extension"];
}
function mkdir_if_not_exists($path) {
if (file_exists($path) && is_dir($path))
return true;
else
return @mkdir($path);
}
function find_picture($path, $base_name) {
if (empty($base_name))
return false;
if (file_exists($path) && is_dir($path)) {
$base_file = $path . "/" . $base_name;
if (file_exists($base_file))
return $base_file;
$pic_ext = array(".jpeg", ".jpg", ".png", ".gif",".JPEG", ".JPG", ".PNG", ".GIF", ".SWF", ".swf");
foreach ($pic_ext as $extension) {
$fn = $base_file . $extension;
if (file_exists($fn))
return $fn;
}
}
return false;
}
// Upload file as given in form field $FIELD_NAME into directory $BASE_DIR.
// The original file name is kept if $BASE_NAME is null.
function handle_picture_upload($base_dir, $field_name, $base_name = null) {
$tmp_file = $_FILES[$field_name]["tmp_name"];
if (!is_uploaded_file($tmp_file))
return false;
if (empty($base_name)) {
$pic_name = $_FILES[$field_name]["name"];
// No need to remove the old file if it has the same name.
$old_pic = false;
} else {
$path_info = pathinfo($_FILES[$field_name]["name"]);
$pic_name = $base_name . "." . $path_info["extension"];
$old_pic = find_picture($base_dir, $base_name);
}
if (mkdir_if_not_exists($base_dir)) {
if ($old_pic)
@unlink($old_pic);
$name = $base_dir . "/" . $pic_name;
if (!move_uploaded_file($tmp_file, $name))
return false;
// Check to see if uploaded file is really a picture
// 2006-08-02 Felix Plesoianu <felix@diffstudios.com>
if (@getimagesize($name) !== false) {
return true;
} else {
@unlink($name);
}
}
return false;
}
function resize_picture($picture_file, $max_width, $max_height) {
if (!function_exists("imagecreatefromjpeg"))
return false;
$pic_info = @getimagesize($picture_file);
if ($pic_info === false)
return false;
list($width, $height, $type, $size_string) = $pic_info;
$old_image = "";
switch($type) {
case 1:
$old_image = @imagecreatefromgif($picture_file);
break;
case 2:
$old_image = @imagecreatefromjpeg($picture_file);
break;
case 3:
$old_image = @imagecreatefrompng($picture_file);
break;
case 4:
$old_image = @imagecreatefromswf($picture_file);
break;
}
if (empty($old_image))
return false;
// If image is rectangular, use larger dimension
// as base for new size, else fit to the minimum
// of $MAX_WIDTH and $MAX_HEIGHT.
if ($width > $height) {
$new_width = $max_width;
$new_height = round($height * ($new_width / $width));
} else if ($width < $height) {
$new_height = $max_height;
$new_width = round($width * ($new_height / $height));
} else if ($max_width <= $max_height) {
$new_width = $new_height = $max_width;
} else {
$new_width = $new_height = $max_height;
}
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled(
$new_image, $old_image, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
switch($type) {
case 1:
$success = imagegif($new_image, $picture_file);
break;
case 2:
$success = imagejpeg($new_image, $picture_file);
break;
case 3:
$success = imagepng($new_image, $picture_file);
break;
case 4:
$success = imageswf($new_image, $picture_file);
break;
}
imagedestroy($old_image);
imagedestroy($new_image);
return $success;
}
?>