我使用以下代码将图像上传到文件夹。我的文件夹名称是ADS。请帮我将该图像上传到我的ADS目录。
root - localhost / dc / upload.php (代码在此位置)
应将图像添加到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;
}
当我使用此代码时,图像显示在Image1中。我需要它像在Image2中没有任何图像更改,我需要在图像上有文字或图像水印,如Image3。
答案 0 :(得分:0)
PHP函数imagepng imagegif和imagejpeg都有一个参数来传递文件名。我重构了你的代码以显示它。
<?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;
}