2014-01-20 77 views
-1

我想上傳圖片,並擁有它的縮略圖版本150像素和400像素的最大高度的最大寬度,並保存爲縮略圖文件名縮略圖PHP - 上傳圖像與具有最大寬度和高度

if(isset($_FILES['image'])){ 
$errors= array(); 
$file_name = $_FILES['image']['name']; 
$file_size =$_FILES['image']['size']; 
$file_tmp =$_FILES['image']['tmp_name']; 
$file_type=$_FILES['image']['type']; 
$tempext = explode('.',$_FILES['image']['name']); 
$file_ext=strtolower(end($tempext)); 
$extensions = array("jpeg","jpg","png","gif","");  
if(in_array($file_ext,$extensions)=== false){ 
$errors[]="extension not allowed, please choose a different file."; 
} 
if($file_size > 419430400){ 
$errors[]='Maximum file size is 400mb'; 
}    
if(empty($errors)==true){ 
    move_uploaded_file($file_tmp,"../large_format/a0/".$file_name); 
    echo '<script>function myFunction(){alert("Success!");} myFunction();</script>'; 
}else{ 
    print_r($errors); 
} 

所以會有一個大的圖像文件和它的縮略圖版本。 我真的很感激建議代碼的一個例子。

在此先感謝

回答

0

嘗試getimagesize()功能Link

//upload orginal size photo  
    // Capture the original size of the uploaded image 
      list($width,$height)=getimagesize($uploadedfile); 
       if ($width > 150){ 
        $newwidth=150; 
        $newheight=($height/$width)*600; 
       }else{ 
           $newwidth=$width; 
       } 
       if($height>400) 
       { 
        $newheight=400 

       } 
       else { 
        $newheight=$height; 
        } 

      $tmp=imagecreatetruecolor($newwidth,$newheight); 
0

試試這個代碼

你並不需要更改這些功能

function resizeImage($image,$width,$height,$scale) { 
    list($imagewidth, $imageheight, $imageType) = getimagesize($image); 
    $imageType = image_type_to_mime_type($imageType); 
    $newImageWidth = ceil($width * $scale); 
    $newImageHeight = ceil($height * $scale); 
    $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
    switch($imageType) { 
     case "image/gif":$source=imagecreatefromgif($image); break; 
     case "image/pjpeg": 
     case "image/jpeg": 
     case "image/jpg":$source=imagecreatefromjpeg($image);break; 
     case "image/png": 
     case "image/x-png":$source=imagecreatefrompng($image); break; 
    } 
    $size_fit=$_SESSION['size_fit']; 
    if($size_fit==1){ 
    if ($width > 140){ 
       $newwidth=140; 
       $newheight=($height/$width)*140; 
      }else {$newwidth=$width; 
       $newheight=$height;} 
    imagecopyresampled($newImage,$source,0,0,0,0,$newwidth,$newheight,$width,$height); 
    }else{ 
    imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); 
    } 
    switch($imageType) { 
     case "image/gif":imagegif($newImage,$image); break; 
     case "image/pjpeg": 
     case "image/jpeg": 
     case "image/jpg":imagejpeg($newImage,$image,90);break; 
     case "image/png": 
     case "image/x-png":imagepng($newImage,$image); break; 
    } 
    chmod($image, 0777); 
    return $image; 
} 
+0

嗨石埠托馬斯。你能告訴我如何使用這個? –

相關問題