上传带缩略图和原始图片的图片

时间:2012-08-18 14:01:45

标签: php file-upload image-resizing

我一直在使用这个脚本将图片上传到我的网站上一年了,我终于意识到必须要做出改变。我一直在网上看,但还没找到任何合适的解决方案,所以我来找你。

  • 此脚本目前正在创建拇指图像并将其移动到文件夹'/ images'中,并带有111x111的优点。
  • 我希望它还能上传原始图片,我该怎么做?

    if(isset($_POST['submit'])) {
    if (($_FILES["image"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg" || $_FILES["image"]["type"] == "image/gif" || $_FILES["image"]["type"] == "image/x-png") && ($_FILES["image"]["size"] < 4000000))
    $current_img=$_FILES['image']['name'];
    $extension = substr(strrchr($current_img, '.'), 1);
    date_default_timezone_set("Europe/Stockholm");
    $time = date("fYhis");
    $new_image = uniqid() . $time;
    $destination   = "images/".$new_image . "-thumb" . "." . $extension;
    $action = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
    
    $max_upload_width = 111;
    $max_upload_height = 111;
    if($_FILES["image"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg"){
        $image_source = imagecreatefromjpeg($destination) ;
    } 
    if($_FILES["image"]["type"] == "image/gif"){    
        $image_source = imagecreatefromgif($_FILES["image"]["tmp_name"]);
    }
    if($_FILES["image"]["type"] == "image/bmp"){    
        $image_source = imagecreatefromwbmp($_FILES["image"]["tmp_name"]);
    }
    if($_FILES["image"]["type"] == "image/x-png"){
        $image_source = imagecreatefrompng($_FILES["image"]["tmp_name"]);
    }
    
    imagejpeg($image_source,$destination,100);
    chmod($destination,0644);
    
    list($image_width, $image_height) = getimagesize($destination);
    
    if($image_width>$max_upload_width || $image_height >$max_upload_height){
        $proportions = 1;
    
        if($image_width>$image_height){
            $new_width  = $max_upload_width;
            $new_height = round($max_upload_width/$proportions);
        }       
        else{
            $new_height = $max_upload_height;
            $new_width  = round($max_upload_height*$proportions);
        }       
    
    
        $new_image = imagecreatetruecolor($new_width , $new_height);
        $image_source = imagecreatefromjpeg($destination);
    
        imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
        imagejpeg($new_image, $destination, 100); // save
        imagedestroy($new_image);
    }
    

1 个答案:

答案 0 :(得分:0)

只需使用以下代码

将文件上传到其他目录即可
$originalImage  = "images/".$new_image . "-ImageName" . "." . $extension;

$action = move_uploaded_file($_FILES['image']['tmp_name'], $originalImage)

在创建缩略图时使用$ originalImage而不是$ _FILES ['image'] ['tmp_name']。

通过应用以下代码进行检查

if(isset($_POST['submit'])) {
if (($_FILES["image"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg" || $_FILES["image"]["type"] == "image/gif" || $_FILES["image"]["type"] == "image/x-png") && ($_FILES["image"]["size"] < 4000000))
$current_img=$_FILES['image']['name'];
$extension = substr(strrchr($current_img, '.'), 1);
date_default_timezone_set("Europe/Stockholm");
$time = date("fYhis");
$new_image = uniqid() . $time;

$originalImage  = "images/".$new_image . "-ImageName" . "." . $extension;
$destination   = "images/".$new_image . "-thumb" . "." . $extension;

$action = move_uploaded_file($_FILES['image']['tmp_name'], $originalImage);

$max_upload_width = 111;
$max_upload_height = 111;
if($_FILES["image"]["type"] == "image/jpeg" || $_FILES["image"]["type"] == "image/pjpeg"){
    $image_source = imagecreatefromjpeg($originalImage) ;
} 
if($_FILES["image"]["type"] == "image/gif"){    
    $image_source = imagecreatefromgif($originalImage);
}
if($_FILES["image"]["type"] == "image/bmp"){    
    $image_source = imagecreatefromwbmp($originalImage);
}
if($_FILES["image"]["type"] == "image/x-png"){
    $image_source = imagecreatefrompng($originalImage);
}

imagejpeg($image_source,$destination,100);
chmod($destination,0644);

list($image_width, $image_height) = getimagesize($destination);

if($image_width>$max_upload_width || $image_height >$max_upload_height){
    $proportions = 1;

    if($image_width>$image_height){
        $new_width  = $max_upload_width;
        $new_height = round($max_upload_width/$proportions);
    }       
    else{
        $new_height = $max_upload_height;
        $new_width  = round($max_upload_height*$proportions);
    }       


    $new_image = imagecreatetruecolor($new_width , $new_height);
    $image_source = imagecreatefromjpeg($destination);

    imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
    imagejpeg($new_image, $destination, 100); // save
    imagedestroy($new_image);
}