我是php新手,我设法将图片上传到我的网络服务器。 现在,我正在寻找一种方法,将原始图像的较小重复版本上传到我的网络服务器。
例如,上传的原始图像是image1_timestamp.jpg 我也想要相同的图像,最大宽度为700px,image1_timestamp_700w.jpg而不会丢失图像/高比率。
这是我目前的代码
if ($_FILES["file"]["error"] > 0){
echo "Error Code: " . $_FILES["file"]["error"] . "<br />";
} else {
echo "Uploaded file: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kilobytes<br />";
if (file_exists("/files/".$_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],"/var/www/mydomain.com/docs/files/images/".$_FILES["file"]["name"]);
echo "Done";
}
}
谢谢!
答案 0 :(得分:0)
我为你写了一些东西:
请继续使用:DIRECTORY_SEPARATOR const
<?php
/**
* @brief Resize an image with keeping aspect ratio
*
* @param [in] $src_image_path :: Source image path => "C:\www\abstract.jpg"
* @param [in] $dst_image_path :: Destination image path => "C:\www\abstract2.jpg"
* @param [in] $new_width :: Width aspect ratio => "700"
* @return image
*
*/
function resizeImage($src_image_path, $dst_image_path, $new_width) {
$mime = getimagesize($src_image_path);
if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($src_image_path); }
if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($src_image_path); }
if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($src_image_path); }
if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($src_image_path); }
$aspect = $mime[1] / $mime[0];
$new_height = $aspect * $new_width;
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
$dst_img = ImageCreateTrueColor($new_width,$new_height);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_width,$new_height,$old_x,$old_y);
// Quality compression : From 0 to 100
$jpg_quality = 90;
// Quality compression : From 0 to 9
$png_quality = 8;
if($mime['mime']=='image/png'){ $result = imagepng($dst_img, $dst_image_path, $png_quality); }
if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img, $dst_image_path, $jpg_quality); }
if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img, $dst_image_path, $jpg_quality); }
if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img, $dst_image_path, $jpg_quality); }
imagedestroy($dst_img);
imagedestroy($src_img);
return $result;
}
$currentPath = dirname(__FILE__) . DIRECTORY_SEPARATOR;
echo $currentPath;
resizeImage($currentPath . 'abstract.jpg', $currentPath . 'abstract2.jpg', 700);
?>
编辑对于您的情况,这可能会有效:
<?php
$newWidth = 700;
$savePath = "/var/www/mydomain.com/docs/files/images/" .$_FILES["file"]["name"];
if($_FILES["file"]["error"] > 0) {
echo "Error Code: " . $_FILES["file"]["error"] . "<br />";
}else {
echo "Uploaded file: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kilobytes<br />";
if(file_exists($savePath)) {
echo $_FILES["file"]["name"] . " already exists";
}else {
resizeImage($_FILES["file"]["tmp_name"], $savePath, $newWidth);
echo "Done";
}
}
?>