当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to upload a image to directory

问题描述:

I used the following code to upload an image to a folder. My folder name is ADS. Please help me to upload that image to my ADS directory.

root - localhost/dc/upload.php

(the code is in this location)

The image should be added to the folder in location localhost/dc/ADS

if (isset($_POST["submit"])) {

if (is_array($_FILES)) {

$file = $_FILES['myImage']['tmp_name'];

$source_properties = getimagesize($file);

$image_type = $source_properties[2];

if ($image_type == IMAGETYPE_JPEG ) {

$image_resource_id = imagecreatefromjpeg($file);

echo $target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);

echo imagejpeg($target_layer,$_FILES['myImage']['name']);

} elseif ($image_type == IMAGETYPE_GIF ) {

$image_resource_id = imagecreatefromgif($file);

echo$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);

echo imagegif($target_layer,$_FILES['myImage']['name'] );

} elseif ($image_type == IMAGETYPE_PNG ) {

$image_resource_id = imagecreatefrompng($file);

echo$target_layer = fn_resize($image_resource_id,$source_properties[0],$source_properties[1]);

echo imagepng($target_layer,$_FILES['myImage']['name']);

}

}

}

function fn_resize($image_resource_id,$width,$height)

{

$target_width =540;

$target_height =400;

$target_layer=imagecreatetruecolor($target_width,$target_height);

imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$target_width,$target_height, $width,$height);

return $target_layer;

}

When I use this code the image is displayed as in the Image1. I need it be like in Image2 without any change to the image and also I need to have a text or image watermark on the image as in the Image3.

网友答案:

The PHP functions imagepng imagegif and imagejpeg all have an argument to pass a file name to. I refactored your code to show this.

<?php

if (isset($_POST["submit"])) {
    if (is_array($_FILES)) {
        $file = $_FILES['myImage']['tmp_name'];
        $source_properties = getimagesize($file);
        $image_type = $source_properties[2];

        if ($image_type == IMAGETYPE_JPEG) {
            $image_resource_id = imagecreatefromjpeg($file);
            echo $target_layer = fn_resize($image_resource_id, $source_properties[0], $source_properties[1]);
            imagejpeg($target_layer, "localhost/dc/ADS/" . $_FILES['myImage']['name']);
        }
        elseif($image_type == IMAGETYPE_GIF) {
            $image_resource_id = imagecreatefromgif($file);
            echo$target_layer = fn_resize($image_resource_id, $source_properties[0], $source_properties[1]);
            imagegif($target_layer, "localhost/dc/ADS/" . $_FILES['myImage']['name']);
        }
        elseif($image_type == IMAGETYPE_PNG) {
            $image_resource_id = imagecreatefrompng($file);
            echo$target_layer = fn_resize($image_resource_id, $source_properties[0], $source_properties[1]);
            imagepng($target_layer, "localhost/dc/ADS/" . $_FILES['myImage']['name']);
        }
    }
}

function fn_resize($image_resource_id, $width, $height) {
    $target_width = 540;
    $target_height = 400;
    $target_layer = imagecreatetruecolor($target_width, $target_height);
    imagecopyresampled($target_layer, $image_resource_id, 0, 0, 0, 0, $target_width, $target_height, $width, $height);
    return $target_layer;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: