Php 图像上传与调整大小
我有一个上传脚本,工作正常,但我想它的上传两次,一个原始格式和一个thubm大小 我在谷歌和stackoverflow上做了allready搜索,我尝试了allready的一些东西,但我没有成功 我的上传脚本Php 图像上传与调整大小,php,image,upload,resize,Php,Image,Upload,Resize,我有一个上传脚本,工作正常,但我想它的上传两次,一个原始格式和一个thubm大小 我在谷歌和stackoverflow上做了allready搜索,我尝试了allready的一些东西,但我没有成功 我的上传脚本 // If you want to ignore the uploaded files, // set $demo_mode to true; $demo_mode = false; $upload_dir = 'uploads/'; $allowed_ext = ar
// If you want to ignore the uploaded files,
// set $demo_mode to true;
$demo_mode = false;
$upload_dir = 'uploads/';
$allowed_ext = array('jpg','jpeg','png','gif');
include('./../includes/core.php');
if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
exit_status('Error! Wrong HTTP method!');
}
if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){
$pic = $_FILES['pic'];
if(!in_array(get_extension($pic['name']),$allowed_ext)){
exit_status('Alleen '.implode(',',$allowed_ext).' bestanden zijn toegestaan');
}
if($demo_mode){
// File uploads are ignored. We only log them.
$line = implode(' ', array( date('r'), $_SERVER['REMOTE_ADDR'], $pic['size'], $pic['name']));
file_put_contents('log.txt', $line.PHP_EOL, FILE_APPEND);
exit_status('Uploads are ignored in demo mode.');
}
// Move the uploaded file from the temporary
// directory to the uploads folder:
$name = $pic['name'];
$sname = hashing($name);
$datum = date("d-m-Y");
if(move_uploaded_file($pic['tmp_name'], $upload_dir.$sname)){
mysql_query("INSERT INTO foto VALUES ('','".$pic['name']."','".$sname."', '".$datum."', '0')");
exit_status('Bestand succesvol geupload');
}
}
exit_status('Er is iets mis gegaan!');
// Helper functions
function exit_status($str){
echo json_encode(array('status'=>$str));
exit;
}
function hashing($naam){
$info = pathinfo($naam);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$hash = basename($naam, $ext);
$hash = $hash . genRandomstring();
$hash = md5($hash);
$hash = $hash . '-' . genRandomstring();
return $hash . $ext;
}
function genRandomString() {
$length = 5;
$string = "";
$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+!@"; // change to whatever characters you want
while ($length > 0) {
$string .= $characters[mt_rand(0,strlen($characters)-1)];
$length -= 1;
}
return $string;
}
function get_extension($file_name){
$ext = explode('.', $file_name);
$ext = array_pop($ext);
return strtolower($ext);
}
?>
如果有人能帮我?我会很高兴的,因为我已经准备好尝试一周了,我可以把它拿出来
谢谢,克里斯
您只需要上传一次图像,然后使用该图像创建第二个缩略图文件。
试着看一下桌子。
如前所述,您无需上传两次,上传图像后复制并调整图像大小,如下所示:
$pathToImages = "path/to/images"
$pathToThumbs = "path/to/thumbs"
$fname = "image-file-name";
$new_fname = "thumb-file-name";
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
$thumbWidth = //someValue//;
$thumbHeight = //someValue//;
// calculate thumbnail size
$new_height = floor($height * ($thumbWidth/$width));
$new_width = $thumbWidth;
// create a new temporary image
$tmp_img = imagecreatetruecolor($thumbWidth, $thumbHeight);
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$new_fname}" );
StackOverflow不是这个问题的合适位置。我们不会为您编写代码。你需要自己编写代码,如果你不确定为什么有些东西不能按预期工作,那么在发布代码时解释一下你期望它做什么,以及它实际在做什么,包括所有错误消息。请参阅。在表单和php中复制与pic相关的代码并再次添加,将“pic”替换为“thumb”,您是否正确键入了图像和拇指的路径?路径的开头不应该有斜杠,例如/upload/is error upload/is correct是的,我有,你有skype吗?